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 | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/FastISel.h" |
| 34 | #include "llvm/CodeGen/GCStrategy.h" |
| 35 | #include "llvm/CodeGen/GCMetadata.h" |
| 36 | #include "llvm/CodeGen/MachineFunction.h" |
| 37 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 38 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 39 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 40 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 41 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/SelectionDAG.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetRegisterInfo.h" |
| 46 | #include "llvm/Target/TargetData.h" |
| 47 | #include "llvm/Target/TargetFrameInfo.h" |
| 48 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 49 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 50 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 51 | #include "llvm/Target/TargetOptions.h" |
| 52 | #include "llvm/Support/Compiler.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 53 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 55 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 56 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 57 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 58 | #include <algorithm> |
| 59 | using namespace llvm; |
| 60 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 61 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 62 | /// some float libcalls (6, 8 or 12 bits). |
| 63 | static unsigned LimitFloatPrecision; |
| 64 | |
| 65 | static cl::opt<unsigned, true> |
| 66 | LimitFPPrecision("limit-float-precision", |
| 67 | cl::desc("Generate low-precision inline sequences " |
| 68 | "for some float libcalls"), |
| 69 | cl::location(LimitFloatPrecision), |
| 70 | cl::init(0)); |
| 71 | |
Dan Gohman | f9bd450 | 2009-11-23 17:46:23 +0000 | [diff] [blame] | 72 | namespace { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 73 | /// RegsForValue - This struct represents the registers (physical or virtual) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 74 | /// that a particular set of values is assigned, and the type information |
| 75 | /// about the value. The most common situation is to represent one value at a |
| 76 | /// time, but struct or array values are handled element-wise as multiple |
| 77 | /// values. The splitting of aggregates is performed recursively, so that we |
| 78 | /// never have aggregate-typed registers. The values at this point do not |
| 79 | /// necessarily have legal types, so each value may require one or more |
| 80 | /// registers of some legal type. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 81 | /// |
Dan Gohman | f9bd450 | 2009-11-23 17:46:23 +0000 | [diff] [blame] | 82 | struct RegsForValue { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 83 | /// TLI - The TargetLowering object. |
| 84 | /// |
| 85 | const TargetLowering *TLI; |
| 86 | |
| 87 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 88 | /// may need be promoted or synthesized from one or more registers. |
| 89 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 90 | SmallVector<EVT, 4> ValueVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 91 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 92 | /// RegVTs - The value types of the registers. This is the same size as |
| 93 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 94 | /// register or registers are. (Individual values are never synthesized |
| 95 | /// from more than one type of register.) |
| 96 | /// |
| 97 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 98 | /// getRegisterType member function, however when with physical registers |
| 99 | /// it is necessary to have a separate record of the types. |
| 100 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 101 | SmallVector<EVT, 4> RegVTs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 102 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 103 | /// Regs - This list holds the registers assigned to the values. |
| 104 | /// Each legal or promoted value requires one register, and each |
| 105 | /// expanded value requires multiple registers. |
| 106 | /// |
| 107 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 108 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 109 | RegsForValue() : TLI(0) {} |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 110 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 111 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 112 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 113 | EVT regvt, EVT valuevt) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 114 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 115 | RegsForValue(const TargetLowering &tli, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 116 | const SmallVector<unsigned, 4> ®s, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 117 | const SmallVector<EVT, 4> ®vts, |
| 118 | const SmallVector<EVT, 4> &valuevts) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 119 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 120 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 121 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 122 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 123 | |
| 124 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 125 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 126 | unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT); |
| 127 | EVT RegisterVT = TLI->getRegisterType(Context, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 128 | for (unsigned i = 0; i != NumRegs; ++i) |
| 129 | Regs.push_back(Reg + i); |
| 130 | RegVTs.push_back(RegisterVT); |
| 131 | Reg += NumRegs; |
| 132 | } |
| 133 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 134 | |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 135 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 136 | bool areValueTypesLegal() { |
| 137 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 138 | EVT RegisterVT = RegVTs[Value]; |
| 139 | if (!TLI->isTypeLegal(RegisterVT)) |
| 140 | return false; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 146 | /// append - Add the specified values to this one. |
| 147 | void append(const RegsForValue &RHS) { |
| 148 | TLI = RHS.TLI; |
| 149 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 150 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 151 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 152 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 153 | |
| 154 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 155 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 156 | /// this value and returns the result as a ValueVTs value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 157 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 158 | /// If the Flag pointer is NULL, no flag is used. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 159 | SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 160 | SDValue &Chain, SDValue *Flag) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 161 | |
| 162 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 163 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 164 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 165 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 166 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 167 | SDValue &Chain, SDValue *Flag) const; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 168 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 169 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 170 | /// operand list. This adds the code marker, matching input operand index |
| 171 | /// (if applicable), and includes the number of values added into it. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 172 | void AddInlineAsmOperands(unsigned Kind, |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 173 | bool HasMatching, unsigned MatchingIdx, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 174 | SelectionDAG &DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 175 | std::vector<SDValue> &Ops) const; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 176 | }; |
| 177 | } |
| 178 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 179 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 180 | /// combined into the value they represent. If the parts combine to a type |
| 181 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 182 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 183 | /// (ISD::AssertSext). |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 184 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 185 | const SDValue *Parts, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 186 | unsigned NumParts, EVT PartVT, EVT ValueVT, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 187 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 188 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 189 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 190 | SDValue Val = Parts[0]; |
| 191 | |
| 192 | if (NumParts > 1) { |
| 193 | // Assemble the value from multiple parts. |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 194 | if (!ValueVT.isVector() && ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 195 | unsigned PartBits = PartVT.getSizeInBits(); |
| 196 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 197 | |
| 198 | // Assemble the power of 2 part. |
| 199 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 200 | 1 << Log2_32(NumParts) : NumParts; |
| 201 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 202 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 203 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 204 | SDValue Lo, Hi; |
| 205 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 206 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 207 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 208 | if (RoundParts > 2) { |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 209 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 210 | PartVT, HalfVT); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 211 | Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 212 | RoundParts / 2, PartVT, HalfVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 213 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 214 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 215 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 216 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 217 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 218 | if (TLI.isBigEndian()) |
| 219 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 220 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 221 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 222 | |
| 223 | if (RoundParts < NumParts) { |
| 224 | // Assemble the trailing non-power-of-2 part. |
| 225 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 226 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 227 | Hi = getCopyFromParts(DAG, dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 228 | Parts + RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 229 | |
| 230 | // Combine the round and odd parts. |
| 231 | Lo = Val; |
| 232 | if (TLI.isBigEndian()) |
| 233 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 234 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 235 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 236 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 237 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 238 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 239 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 240 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 241 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 242 | } else if (ValueVT.isVector()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 243 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 244 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 245 | unsigned NumIntermediates; |
| 246 | unsigned NumRegs = |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 247 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 248 | NumIntermediates, RegisterVT); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 249 | assert(NumRegs == NumParts |
| 250 | && "Part count doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 251 | NumParts = NumRegs; // Silence a compiler warning. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 252 | assert(RegisterVT == PartVT |
| 253 | && "Part type doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 254 | assert(RegisterVT == Parts[0].getValueType() && |
| 255 | "Part type doesn't match part!"); |
| 256 | |
| 257 | // Assemble the parts into intermediate operands. |
| 258 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 259 | if (NumIntermediates == NumParts) { |
| 260 | // If the register was not expanded, truncate or copy the value, |
| 261 | // as appropriate. |
| 262 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 263 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 264 | PartVT, IntermediateVT); |
| 265 | } else if (NumParts > 0) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 266 | // If the intermediate type was expanded, build the intermediate |
| 267 | // operands from the parts. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 268 | assert(NumParts % NumIntermediates == 0 && |
| 269 | "Must expand into a divisible number of parts!"); |
| 270 | unsigned Factor = NumParts / NumIntermediates; |
| 271 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 272 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 273 | PartVT, IntermediateVT); |
| 274 | } |
| 275 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 276 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 277 | // intermediate operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 278 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 279 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 280 | ValueVT, &Ops[0], NumIntermediates); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 281 | } else if (PartVT.isFloatingPoint()) { |
| 282 | // FP split into multiple FP parts (for ppcf128) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 283 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 284 | "Unexpected split"); |
| 285 | SDValue Lo, Hi; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 286 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]); |
| 287 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 288 | if (TLI.isBigEndian()) |
| 289 | std::swap(Lo, Hi); |
| 290 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi); |
| 291 | } else { |
| 292 | // FP split into integer parts (soft fp) |
| 293 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 294 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 295 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 296 | Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 301 | PartVT = Val.getValueType(); |
| 302 | |
| 303 | if (PartVT == ValueVT) |
| 304 | return Val; |
| 305 | |
| 306 | if (PartVT.isVector()) { |
| 307 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 308 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if (ValueVT.isVector()) { |
| 312 | assert(ValueVT.getVectorElementType() == PartVT && |
| 313 | ValueVT.getVectorNumElements() == 1 && |
| 314 | "Only trivial scalar-to-vector conversions should get here!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 315 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | if (PartVT.isInteger() && |
| 319 | ValueVT.isInteger()) { |
| 320 | if (ValueVT.bitsLT(PartVT)) { |
| 321 | // For a truncate, see if we have any information to |
| 322 | // indicate whether the truncated bits will always be |
| 323 | // zero or sign-extension. |
| 324 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 325 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 326 | DAG.getValueType(ValueVT)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 327 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 328 | } else { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 329 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 334 | if (ValueVT.bitsLT(Val.getValueType())) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 335 | // FP_ROUND's are always exact here. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 336 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
| 337 | DAG.getIntPtrConstant(1)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 340 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 343 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
| 344 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 345 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 346 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 347 | return SDValue(); |
| 348 | } |
| 349 | |
| 350 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 351 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 352 | /// 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] | 353 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 354 | SDValue Val, SDValue *Parts, unsigned NumParts, |
| 355 | EVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 356 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 357 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 358 | EVT PtrVT = TLI.getPointerTy(); |
| 359 | EVT ValueVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 360 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 361 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 362 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 363 | |
| 364 | if (!NumParts) |
| 365 | return; |
| 366 | |
| 367 | if (!ValueVT.isVector()) { |
| 368 | if (PartVT == ValueVT) { |
| 369 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 370 | Parts[0] = Val; |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 375 | // If the parts cover more bits than the value has, promote the value. |
| 376 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 377 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 378 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 379 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 380 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 381 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 382 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 383 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 384 | } |
| 385 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 386 | // Different types of the same size. |
| 387 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 388 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 389 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 390 | // If the parts cover less bits than value has, truncate the value. |
| 391 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 392 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 393 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 394 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 395 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | // The value may have changed - recompute ValueVT. |
| 400 | ValueVT = Val.getValueType(); |
| 401 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 402 | "Failed to tile the value with PartVT!"); |
| 403 | |
| 404 | if (NumParts == 1) { |
| 405 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 406 | Parts[0] = Val; |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | // Expand the value into multiple parts. |
| 411 | if (NumParts & (NumParts - 1)) { |
| 412 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 413 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 414 | "Do not know what to expand to!"); |
| 415 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 416 | unsigned RoundBits = RoundParts * PartBits; |
| 417 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 418 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 419 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 420 | TLI.getPointerTy())); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 421 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 422 | OddParts, PartVT); |
| 423 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 424 | if (TLI.isBigEndian()) |
| 425 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 426 | std::reverse(Parts + RoundParts, Parts + NumParts); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 427 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 428 | NumParts = RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 429 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 430 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 434 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 435 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 436 | EVT::getIntegerVT(*DAG.getContext(), |
| 437 | ValueVT.getSizeInBits()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 438 | Val); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 439 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 440 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 441 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 442 | unsigned ThisBits = StepSize * PartBits / 2; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 443 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 444 | SDValue &Part0 = Parts[i]; |
| 445 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 446 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 447 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 448 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 449 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 450 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 451 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 452 | DAG.getConstant(0, PtrVT)); |
| 453 | |
| 454 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 455 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 456 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 457 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 458 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 464 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 465 | |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | // Vector ValueVT. |
| 470 | if (NumParts == 1) { |
| 471 | if (PartVT != ValueVT) { |
Bob Wilson | 5afffae | 2009-12-18 01:03:29 +0000 | [diff] [blame] | 472 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 473 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 474 | } else { |
| 475 | assert(ValueVT.getVectorElementType() == PartVT && |
| 476 | ValueVT.getVectorNumElements() == 1 && |
| 477 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 478 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 479 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 480 | DAG.getConstant(0, PtrVT)); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | Parts[0] = Val; |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 489 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 490 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 491 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
| 492 | IntermediateVT, NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 493 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 494 | |
| 495 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 496 | NumParts = NumRegs; // Silence a compiler warning. |
| 497 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 498 | |
| 499 | // Split the vector into intermediate operands. |
| 500 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 501 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 502 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 503 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 504 | IntermediateVT, Val, |
| 505 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 506 | PtrVT)); |
| 507 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 508 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 509 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 510 | DAG.getConstant(i, PtrVT)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 513 | // Split the intermediate operands into legal parts. |
| 514 | if (NumParts == NumIntermediates) { |
| 515 | // If the register was not expanded, promote or copy the value, |
| 516 | // as appropriate. |
| 517 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 518 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 519 | } else if (NumParts > 0) { |
| 520 | // If the intermediate type was expanded, split each the value into |
| 521 | // legal parts. |
| 522 | assert(NumParts % NumIntermediates == 0 && |
| 523 | "Must expand into a divisible number of parts!"); |
| 524 | unsigned Factor = NumParts / NumIntermediates; |
| 525 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 526 | getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 531 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 532 | AA = &aa; |
| 533 | GFI = gfi; |
| 534 | TD = DAG.getTarget().getTargetData(); |
| 535 | } |
| 536 | |
| 537 | /// clear - Clear out the curret SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 538 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 539 | /// for a new block. This doesn't clear out information about |
| 540 | /// additional blocks that are needed to complete switch lowering |
| 541 | /// or PHI node updating; that information is cleared out as it is |
| 542 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 543 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 544 | NodeMap.clear(); |
| 545 | PendingLoads.clear(); |
| 546 | PendingExports.clear(); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 547 | EdgeMapping.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 548 | DAG.clear(); |
Chris Lattner | a4f2bb0 | 2010-04-02 20:17:23 +0000 | [diff] [blame] | 549 | CurDebugLoc = DebugLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 550 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 554 | /// flushing any PendingLoad items. This must be done before emitting |
| 555 | /// a store or any other node that may need to be ordered after any |
| 556 | /// prior load instructions. |
| 557 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 558 | SDValue SelectionDAGBuilder::getRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 559 | if (PendingLoads.empty()) |
| 560 | return DAG.getRoot(); |
| 561 | |
| 562 | if (PendingLoads.size() == 1) { |
| 563 | SDValue Root = PendingLoads[0]; |
| 564 | DAG.setRoot(Root); |
| 565 | PendingLoads.clear(); |
| 566 | return Root; |
| 567 | } |
| 568 | |
| 569 | // Otherwise, we have to make a token factor node. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 570 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 571 | &PendingLoads[0], PendingLoads.size()); |
| 572 | PendingLoads.clear(); |
| 573 | DAG.setRoot(Root); |
| 574 | return Root; |
| 575 | } |
| 576 | |
| 577 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 578 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 579 | /// to do this before emitting a terminator instruction. |
| 580 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 581 | SDValue SelectionDAGBuilder::getControlRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 582 | SDValue Root = DAG.getRoot(); |
| 583 | |
| 584 | if (PendingExports.empty()) |
| 585 | return Root; |
| 586 | |
| 587 | // Turn all of the CopyToReg chains into one factored node. |
| 588 | if (Root.getOpcode() != ISD::EntryToken) { |
| 589 | unsigned i = 0, e = PendingExports.size(); |
| 590 | for (; i != e; ++i) { |
| 591 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 592 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 593 | break; // Don't add the root if we already indirectly depend on it. |
| 594 | } |
| 595 | |
| 596 | if (i == e) |
| 597 | PendingExports.push_back(Root); |
| 598 | } |
| 599 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 600 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 601 | &PendingExports[0], |
| 602 | PendingExports.size()); |
| 603 | PendingExports.clear(); |
| 604 | DAG.setRoot(Root); |
| 605 | return Root; |
| 606 | } |
| 607 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 608 | void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) { |
| 609 | if (DAG.GetOrdering(Node) != 0) return; // Already has ordering. |
| 610 | DAG.AssignOrdering(Node, SDNodeOrder); |
| 611 | |
| 612 | for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) |
| 613 | AssignOrderingToNode(Node->getOperand(I).getNode()); |
| 614 | } |
| 615 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 616 | void SelectionDAGBuilder::visit(Instruction &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 617 | visit(I.getOpcode(), I); |
| 618 | } |
| 619 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 620 | void SelectionDAGBuilder::visit(unsigned Opcode, User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 621 | // Note: this doesn't use InstVisitor, because it has to work with |
| 622 | // ConstantExpr's in addition to instructions. |
| 623 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 624 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 625 | // Build the switch statement using the Instruction.def file. |
| 626 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 627 | case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 628 | #include "llvm/Instruction.def" |
| 629 | } |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 630 | |
| 631 | // Assign the ordering to the freshly created DAG nodes. |
| 632 | if (NodeMap.count(&I)) { |
| 633 | ++SDNodeOrder; |
| 634 | AssignOrderingToNode(getValue(&I).getNode()); |
| 635 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 636 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 637 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 638 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 639 | SDValue &N = NodeMap[V]; |
| 640 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 641 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 642 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 643 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 644 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 645 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 646 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 647 | |
| 648 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 649 | return N = DAG.getGlobalAddress(GV, VT); |
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 | if (isa<ConstantPointerNull>(C)) |
| 652 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 653 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 654 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 655 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 656 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 657 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 658 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 659 | |
| 660 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 661 | visit(CE->getOpcode(), *CE); |
| 662 | SDValue N1 = NodeMap[V]; |
| 663 | assert(N1.getNode() && "visit didn't populate the ValueMap!"); |
| 664 | return N1; |
| 665 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 666 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 667 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 668 | SmallVector<SDValue, 4> Constants; |
| 669 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 670 | OI != OE; ++OI) { |
| 671 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 672 | // If the operand is an empty aggregate, there are no values. |
| 673 | if (!Val) continue; |
| 674 | // Add each leaf value from the operand to the Constants list |
| 675 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 676 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 677 | Constants.push_back(SDValue(Val, i)); |
| 678 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 679 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 680 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 681 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 684 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 685 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 686 | "Unknown struct or array constant!"); |
| 687 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 688 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 689 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 690 | unsigned NumElts = ValueVTs.size(); |
| 691 | if (NumElts == 0) |
| 692 | return SDValue(); // empty struct |
| 693 | SmallVector<SDValue, 4> Constants(NumElts); |
| 694 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 695 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 696 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 697 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 698 | else if (EltVT.isFloatingPoint()) |
| 699 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 700 | else |
| 701 | Constants[i] = DAG.getConstant(0, EltVT); |
| 702 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 703 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 704 | return DAG.getMergeValues(&Constants[0], NumElts, |
| 705 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 708 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 709 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 710 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 711 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 712 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 713 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 714 | // Now that we know the number and type of the elements, get that number of |
| 715 | // elements into the Ops array based on what kind of constant it is. |
| 716 | SmallVector<SDValue, 16> Ops; |
| 717 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 718 | for (unsigned i = 0; i != NumElements; ++i) |
| 719 | Ops.push_back(getValue(CP->getOperand(i))); |
| 720 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 721 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 722 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 723 | |
| 724 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 725 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 726 | Op = DAG.getConstantFP(0, EltVT); |
| 727 | else |
| 728 | Op = DAG.getConstant(0, EltVT); |
| 729 | Ops.assign(NumElements, Op); |
| 730 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 731 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 732 | // Create a BUILD_VECTOR node. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 733 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 734 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 735 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 736 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 737 | // If this is a static alloca, generate it as the frameindex instead of |
| 738 | // computation. |
| 739 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 740 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 741 | FuncInfo.StaticAllocaMap.find(AI); |
| 742 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 743 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 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 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 747 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 748 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 749 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 750 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 751 | return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 754 | /// Get the EVTs and ArgFlags collections that represent the legalized return |
| 755 | /// type of the given function. This does not require a DAG or a return value, |
| 756 | /// 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] | 757 | static void getReturnInfo(const Type* ReturnType, |
| 758 | Attributes attr, SmallVectorImpl<EVT> &OutVTs, |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 759 | SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags, |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 760 | TargetLowering &TLI, |
| 761 | SmallVectorImpl<uint64_t> *Offsets = 0) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 762 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 763 | ComputeValueVTs(TLI, ReturnType, ValueVTs); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 764 | unsigned NumValues = ValueVTs.size(); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 765 | if (NumValues == 0) return; |
| 766 | unsigned Offset = 0; |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 767 | |
| 768 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 769 | EVT VT = ValueVTs[j]; |
| 770 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 771 | |
| 772 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 773 | ExtendKind = ISD::SIGN_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 774 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 775 | ExtendKind = ISD::ZERO_EXTEND; |
| 776 | |
| 777 | // FIXME: C calling convention requires the return type to be promoted to |
| 778 | // at least 32-bit. But this is not necessary for non-C calling |
| 779 | // conventions. The frontend should mark functions whose return values |
| 780 | // require promoting with signext or zeroext attributes. |
| 781 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 782 | EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 783 | if (VT.bitsLT(MinVT)) |
| 784 | VT = MinVT; |
| 785 | } |
| 786 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 787 | unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT); |
| 788 | EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 789 | unsigned PartSize = TLI.getTargetData()->getTypeAllocSize( |
| 790 | PartVT.getTypeForEVT(ReturnType->getContext())); |
| 791 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 792 | // 'inreg' on function refers to return value |
| 793 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 794 | if (attr & Attribute::InReg) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 795 | Flags.setInReg(); |
| 796 | |
| 797 | // Propagate extension type if any |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 798 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 799 | Flags.setSExt(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 800 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 801 | Flags.setZExt(); |
| 802 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 803 | for (unsigned i = 0; i < NumParts; ++i) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 804 | OutVTs.push_back(PartVT); |
| 805 | OutFlags.push_back(Flags); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 806 | if (Offsets) |
| 807 | { |
| 808 | Offsets->push_back(Offset); |
| 809 | Offset += PartSize; |
| 810 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 814 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 815 | void SelectionDAGBuilder::visitRet(ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 816 | SDValue Chain = getControlRoot(); |
| 817 | SmallVector<ISD::OutputArg, 8> Outs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 818 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 819 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 820 | if (!FLI.CanLowerReturn) { |
| 821 | unsigned DemoteReg = FLI.DemoteRegister; |
| 822 | const Function *F = I.getParent()->getParent(); |
| 823 | |
| 824 | // Emit a store of the return value through the virtual register. |
| 825 | // Leave Outs empty so that LowerReturn won't try to load return |
| 826 | // registers the usual way. |
| 827 | SmallVector<EVT, 1> PtrValueVTs; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 828 | ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 829 | PtrValueVTs); |
| 830 | |
| 831 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 832 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 833 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 834 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 835 | SmallVector<uint64_t, 4> Offsets; |
| 836 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 837 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 838 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 839 | SmallVector<SDValue, 4> Chains(NumValues); |
| 840 | EVT PtrVT = PtrValueVTs[0]; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 841 | for (unsigned i = 0; i != NumValues; ++i) { |
| 842 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr, |
| 843 | DAG.getConstant(Offsets[i], PtrVT)); |
| 844 | Chains[i] = |
| 845 | DAG.getStore(Chain, getCurDebugLoc(), |
| 846 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 847 | Add, NULL, Offsets[i], false, false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 850 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 851 | MVT::Other, &Chains[0], NumValues); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 852 | } else if (I.getNumOperands() != 0) { |
| 853 | SmallVector<EVT, 4> ValueVTs; |
| 854 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs); |
| 855 | unsigned NumValues = ValueVTs.size(); |
| 856 | if (NumValues) { |
| 857 | SDValue RetOp = getValue(I.getOperand(0)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 858 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 859 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 860 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 861 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 862 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 863 | const Function *F = I.getParent()->getParent(); |
| 864 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 865 | ExtendKind = ISD::SIGN_EXTEND; |
| 866 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 867 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 868 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 869 | // FIXME: C calling convention requires the return type to be promoted |
| 870 | // 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] | 871 | // conventions. The frontend should mark functions whose return values |
| 872 | // require promoting with signext or zeroext attributes. |
| 873 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 874 | EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32); |
| 875 | if (VT.bitsLT(MinVT)) |
| 876 | VT = MinVT; |
| 877 | } |
| 878 | |
| 879 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
| 880 | EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
| 881 | SmallVector<SDValue, 4> Parts(NumParts); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 882 | getCopyToParts(DAG, getCurDebugLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 883 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
| 884 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 885 | |
| 886 | // 'inreg' on function refers to return value |
| 887 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 888 | if (F->paramHasAttr(0, Attribute::InReg)) |
| 889 | Flags.setInReg(); |
| 890 | |
| 891 | // Propagate extension type if any |
| 892 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 893 | Flags.setSExt(); |
| 894 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 895 | Flags.setZExt(); |
| 896 | |
| 897 | for (unsigned i = 0; i < NumParts; ++i) |
| 898 | Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true)); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 899 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 902 | |
| 903 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 904 | CallingConv::ID CallConv = |
| 905 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 906 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
| 907 | Outs, getCurDebugLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 908 | |
| 909 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 910 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 911 | "LowerReturn didn't return a valid chain!"); |
| 912 | |
| 913 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 914 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 917 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 918 | /// created for it, emit nodes to copy the value into the virtual |
| 919 | /// registers. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 920 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) { |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 921 | if (!V->use_empty()) { |
| 922 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 923 | if (VMI != FuncInfo.ValueMap.end()) |
| 924 | CopyValueToVirtualRegister(V, VMI->second); |
| 925 | } |
| 926 | } |
| 927 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 928 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 929 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 930 | /// CopyTo/FromReg. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 931 | void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 932 | // No need to export constants. |
| 933 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 934 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 935 | // Already exported? |
| 936 | if (FuncInfo.isExportedInst(V)) return; |
| 937 | |
| 938 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 939 | CopyValueToVirtualRegister(V, Reg); |
| 940 | } |
| 941 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 942 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V, |
| 943 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 944 | // The operands of the setcc have to be in this block. We don't know |
| 945 | // how to export them from some other block. |
| 946 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 947 | // Can export from current BB. |
| 948 | if (VI->getParent() == FromBB) |
| 949 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 950 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 951 | // Is already exported, noop. |
| 952 | return FuncInfo.isExportedInst(V); |
| 953 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 954 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 955 | // If this is an argument, we can export it if the BB is the entry block or |
| 956 | // if it is already exported. |
| 957 | if (isa<Argument>(V)) { |
| 958 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 959 | return true; |
| 960 | |
| 961 | // Otherwise, can only export this if it is already exported. |
| 962 | return FuncInfo.isExportedInst(V); |
| 963 | } |
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 | // Otherwise, constants can always be exported. |
| 966 | return true; |
| 967 | } |
| 968 | |
| 969 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 970 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 971 | return I->getParent() == BB; |
| 972 | return true; |
| 973 | } |
| 974 | |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 975 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 976 | /// the given LLVM IR floating-point condition code. This includes |
| 977 | /// consideration of global floating-point math flags. |
| 978 | /// |
| 979 | static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) { |
| 980 | ISD::CondCode FPC, FOC; |
| 981 | switch (Pred) { |
| 982 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 983 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 984 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 985 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 986 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 987 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 988 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 989 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 990 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 991 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 992 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 993 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 994 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 995 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 996 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 997 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 998 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 999 | llvm_unreachable("Invalid FCmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1000 | FOC = FPC = ISD::SETFALSE; |
| 1001 | break; |
| 1002 | } |
| 1003 | if (FiniteOnlyFPMath()) |
| 1004 | return FOC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1005 | else |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1006 | return FPC; |
| 1007 | } |
| 1008 | |
| 1009 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 1010 | /// the given LLVM IR integer condition code. |
| 1011 | /// |
| 1012 | static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) { |
| 1013 | switch (Pred) { |
| 1014 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 1015 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 1016 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 1017 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 1018 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 1019 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 1020 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 1021 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 1022 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 1023 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 1024 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1025 | llvm_unreachable("Invalid ICmp predicate opcode!"); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1026 | return ISD::SETNE; |
| 1027 | } |
| 1028 | } |
| 1029 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1030 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1031 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1032 | /// AND operator tree. |
| 1033 | /// |
| 1034 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1035 | SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond, |
| 1036 | MachineBasicBlock *TBB, |
| 1037 | MachineBasicBlock *FBB, |
| 1038 | MachineBasicBlock *CurBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1039 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1040 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1041 | // If the leaf of the tree is a comparison, merge the condition into |
| 1042 | // the caseblock. |
| 1043 | if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
| 1044 | // The operands of the cmp have to be in this block. We don't know |
| 1045 | // how to export them from some other block. If this is the first block |
| 1046 | // of the sequence, no exporting is needed. |
| 1047 | if (CurBB == CurMBB || |
| 1048 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1049 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1050 | ISD::CondCode Condition; |
| 1051 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1052 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1053 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1054 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1055 | } else { |
| 1056 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1057 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1058 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1059 | |
| 1060 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1061 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1062 | SwitchCases.push_back(CB); |
| 1063 | return; |
| 1064 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1068 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1069 | NULL, TBB, FBB, CurBB); |
| 1070 | SwitchCases.push_back(CB); |
| 1071 | } |
| 1072 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1073 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1074 | void SelectionDAGBuilder::FindMergedConditions(Value *Cond, |
| 1075 | MachineBasicBlock *TBB, |
| 1076 | MachineBasicBlock *FBB, |
| 1077 | MachineBasicBlock *CurBB, |
| 1078 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1079 | // If this node is not part of the or/and tree, emit it as a branch. |
| 1080 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1081 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1082 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1083 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1084 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1085 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
| 1086 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1087 | return; |
| 1088 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1089 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1090 | // Create TmpBB after CurBB. |
| 1091 | MachineFunction::iterator BBI = CurBB; |
| 1092 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1093 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1094 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1095 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1096 | if (Opc == Instruction::Or) { |
| 1097 | // Codegen X | Y as: |
| 1098 | // jmp_if_X TBB |
| 1099 | // jmp TmpBB |
| 1100 | // TmpBB: |
| 1101 | // jmp_if_Y TBB |
| 1102 | // jmp FBB |
| 1103 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1104 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1105 | // Emit the LHS condition. |
| 1106 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1107 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1108 | // Emit the RHS condition into TmpBB. |
| 1109 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1110 | } else { |
| 1111 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1112 | // Codegen X & Y as: |
| 1113 | // jmp_if_X TmpBB |
| 1114 | // jmp FBB |
| 1115 | // TmpBB: |
| 1116 | // jmp_if_Y TBB |
| 1117 | // jmp FBB |
| 1118 | // |
| 1119 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1120 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1121 | // Emit the LHS condition. |
| 1122 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1123 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1124 | // Emit the RHS condition into TmpBB. |
| 1125 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1130 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1131 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1132 | bool |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1133 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1134 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1135 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1136 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1137 | // will get folded into a single comparison, so don't emit two blocks. |
| 1138 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1139 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1140 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1141 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1142 | return false; |
| 1143 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1145 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1146 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1147 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1148 | Cases[0].CC == Cases[1].CC && |
| 1149 | isa<Constant>(Cases[0].CmpRHS) && |
| 1150 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1151 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1152 | return false; |
| 1153 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1154 | return false; |
| 1155 | } |
| 1156 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1157 | return true; |
| 1158 | } |
| 1159 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1160 | void SelectionDAGBuilder::visitBr(BranchInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1161 | // Update machine-CFG edges. |
| 1162 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1163 | |
| 1164 | // Figure out which block is immediately after the current one. |
| 1165 | MachineBasicBlock *NextBlock = 0; |
| 1166 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1167 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1168 | NextBlock = BBI; |
| 1169 | |
| 1170 | if (I.isUnconditional()) { |
| 1171 | // Update machine-CFG edges. |
| 1172 | CurMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1173 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1174 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1175 | if (Succ0MBB != NextBlock) |
| 1176 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1177 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1178 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1180 | return; |
| 1181 | } |
| 1182 | |
| 1183 | // If this condition is one of the special cases we handle, do special stuff |
| 1184 | // now. |
| 1185 | Value *CondVal = I.getCondition(); |
| 1186 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1187 | |
| 1188 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1189 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1190 | // For example, instead of something like: |
| 1191 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1192 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1193 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1194 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1195 | // or C, F |
| 1196 | // jnz foo |
| 1197 | // Emit: |
| 1198 | // cmp A, B |
| 1199 | // je foo |
| 1200 | // cmp D, E |
| 1201 | // jle foo |
| 1202 | // |
| 1203 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1204 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1205 | (BOp->getOpcode() == Instruction::And || |
| 1206 | BOp->getOpcode() == Instruction::Or)) { |
| 1207 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
| 1208 | // If the compares in later blocks need to use values not currently |
| 1209 | // exported from this block, export them now. This block should always |
| 1210 | // be the first entry. |
| 1211 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1212 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1213 | // Allow some cases to be rejected. |
| 1214 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1215 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1216 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1217 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1218 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1219 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1220 | // Emit the branch for this block. |
| 1221 | visitSwitchCase(SwitchCases[0]); |
| 1222 | SwitchCases.erase(SwitchCases.begin()); |
| 1223 | return; |
| 1224 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1225 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1226 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1227 | // SwitchCases. |
| 1228 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1229 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1230 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1231 | SwitchCases.clear(); |
| 1232 | } |
| 1233 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1234 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1235 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1236 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1237 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1238 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1239 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1240 | // cond branch. |
| 1241 | visitSwitchCase(CB); |
| 1242 | } |
| 1243 | |
| 1244 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1245 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1246 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1247 | SDValue Cond; |
| 1248 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1249 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1250 | |
| 1251 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1252 | if (CB.CmpMHS == NULL) { |
| 1253 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1254 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1255 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1256 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1257 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1258 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1259 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1260 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1261 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1262 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1263 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1264 | } else { |
| 1265 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1266 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1267 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1268 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1269 | |
| 1270 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1271 | EVT VT = CmpOp.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1274 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1275 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1276 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1277 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1278 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1279 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1280 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1281 | } |
| 1282 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1283 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1284 | // Update successor info |
| 1285 | CurMBB->addSuccessor(CB.TrueBB); |
| 1286 | CurMBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1287 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1288 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1289 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1290 | MachineBasicBlock *NextBlock = 0; |
| 1291 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1292 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1293 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1294 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1295 | // If the lhs block is the next block, invert the condition so that we can |
| 1296 | // fall through to the lhs instead of the rhs block. |
| 1297 | if (CB.TrueBB == NextBlock) { |
| 1298 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1299 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1300 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1301 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1302 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1303 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1304 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1305 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1306 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1307 | // If the branch was constant folded, fix up the CFG. |
| 1308 | if (BrCond.getOpcode() == ISD::BR) { |
| 1309 | CurMBB->removeSuccessor(CB.FalseBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1310 | } else { |
| 1311 | // Otherwise, go ahead and insert the false branch. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1312 | if (BrCond == getControlRoot()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1313 | CurMBB->removeSuccessor(CB.TrueBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1314 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1315 | if (CB.FalseBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1316 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1317 | DAG.getBasicBlock(CB.FalseBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1318 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1319 | |
| 1320 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1324 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1325 | // Emit the code for the jump table |
| 1326 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1327 | EVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1328 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1329 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1330 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1331 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
| 1332 | MVT::Other, Index.getValue(1), |
| 1333 | Table, Index); |
| 1334 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1338 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1339 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
| 1340 | JumpTableHeader &JTH) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1341 | // Subtract the lowest switch case value from the value being switched on and |
| 1342 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1343 | // difference between smallest and largest cases. |
| 1344 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1345 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1346 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1347 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1348 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1349 | // 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] | 1350 | // 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] | 1351 | // can be used as an index into the jump table in a subsequent basic block. |
| 1352 | // This value may be smaller or larger than the target's pointer type, and |
| 1353 | // therefore require extension or truncating. |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1354 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1355 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1356 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1357 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1358 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1359 | JT.Reg = JumpTableReg; |
| 1360 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1361 | // Emit the range check for the jump table, and branch to the default block |
| 1362 | // for the switch statement if the value being switched on exceeds the largest |
| 1363 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1364 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1365 | TLI.getSetCCResultType(Sub.getValueType()), Sub, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1366 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1367 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1368 | |
| 1369 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1370 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1371 | MachineBasicBlock *NextBlock = 0; |
| 1372 | MachineFunction::iterator BBI = CurMBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1373 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1374 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1375 | NextBlock = BBI; |
| 1376 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1377 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1378 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1379 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1380 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1381 | if (JT.MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1382 | BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
| 1383 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1384 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1385 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1389 | /// suitable for "bit tests" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1390 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1391 | // Subtract the minimum value |
| 1392 | SDValue SwitchOp = getValue(B.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1393 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1394 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1395 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1396 | |
| 1397 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1398 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1399 | TLI.getSetCCResultType(Sub.getValueType()), |
| 1400 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1401 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1402 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1403 | SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), |
| 1404 | TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1405 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1406 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1407 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1408 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1411 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1412 | MachineBasicBlock *NextBlock = 0; |
| 1413 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1414 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1415 | NextBlock = BBI; |
| 1416 | |
| 1417 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1418 | |
| 1419 | CurMBB->addSuccessor(B.Default); |
| 1420 | CurMBB->addSuccessor(MBB); |
| 1421 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1422 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1423 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1424 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1425 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1426 | if (MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1427 | BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
| 1428 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1429 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1430 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /// visitBitTestCase - this function produces one "bit test" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1434 | void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1435 | unsigned Reg, |
| 1436 | BitTestCase &B) { |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1437 | // Make desired shift |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1438 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1439 | TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1440 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1441 | TLI.getPointerTy(), |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1442 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1443 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1444 | |
Anton Korobeynikov | 36c826a | 2009-01-26 19:26:01 +0000 | [diff] [blame] | 1445 | // Emit bit tests and jumps |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1446 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1447 | TLI.getPointerTy(), SwitchVal, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1448 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1449 | SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(), |
| 1450 | TLI.getSetCCResultType(AndOp.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 1451 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1452 | ISD::SETNE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1453 | |
| 1454 | CurMBB->addSuccessor(B.TargetBB); |
| 1455 | CurMBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1456 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1457 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1458 | MVT::Other, getControlRoot(), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1459 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1460 | |
| 1461 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1462 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1463 | MachineBasicBlock *NextBlock = 0; |
| 1464 | MachineFunction::iterator BBI = CurMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1465 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1466 | NextBlock = BBI; |
| 1467 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1468 | if (NextMBB != NextBlock) |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1469 | BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
| 1470 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1471 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1472 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1475 | void SelectionDAGBuilder::visitInvoke(InvokeInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1476 | // Retrieve successors. |
| 1477 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1478 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1479 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1480 | const Value *Callee(I.getCalledValue()); |
| 1481 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1482 | visitInlineAsm(&I); |
| 1483 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1484 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1485 | |
| 1486 | // If the value of the invoke is used outside of its defining block, make it |
| 1487 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1488 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1489 | |
| 1490 | // Update successor info |
| 1491 | CurMBB->addSuccessor(Return); |
| 1492 | CurMBB->addSuccessor(LandingPad); |
| 1493 | |
| 1494 | // Drop into normal successor. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1495 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1496 | MVT::Other, getControlRoot(), |
| 1497 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1500 | void SelectionDAGBuilder::visitUnwind(UnwindInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1504 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1505 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1506 | CaseRecVector& WorkList, |
| 1507 | Value* SV, |
| 1508 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1509 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1510 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1511 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1512 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1513 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1514 | return false; |
| 1515 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1516 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1517 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1518 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1519 | |
| 1520 | // Figure out which block is immediately after the current one. |
| 1521 | MachineBasicBlock *NextBlock = 0; |
| 1522 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1523 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1524 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1525 | NextBlock = BBI; |
| 1526 | |
| 1527 | // TODO: If any two of the cases has the same destination, and if one value |
| 1528 | // is the same as the other, but has one bit unset that the other has set, |
| 1529 | // use bit manipulation to do two compares at once. For example: |
| 1530 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1531 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1532 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1533 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1534 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1535 | // branches in this order. See if rearranging a case value would help. |
| 1536 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1537 | if (I->BB == NextBlock) { |
| 1538 | std::swap(*I, BackCase); |
| 1539 | break; |
| 1540 | } |
| 1541 | } |
| 1542 | } |
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 | // Create a CaseBlock record representing a conditional branch to |
| 1545 | // the Case's target mbb if the value being switched on SV is equal |
| 1546 | // to C. |
| 1547 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1548 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1549 | MachineBasicBlock *FallThrough; |
| 1550 | if (I != E-1) { |
| 1551 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1552 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1553 | |
| 1554 | // Put SV in a virtual register to make it available from the new blocks. |
| 1555 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1556 | } else { |
| 1557 | // If the last case doesn't match, go to the default block. |
| 1558 | FallThrough = Default; |
| 1559 | } |
| 1560 | |
| 1561 | Value *RHS, *LHS, *MHS; |
| 1562 | ISD::CondCode CC; |
| 1563 | if (I->High == I->Low) { |
| 1564 | // This is just small small case range :) containing exactly 1 case |
| 1565 | CC = ISD::SETEQ; |
| 1566 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1567 | } else { |
| 1568 | CC = ISD::SETLE; |
| 1569 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1570 | } |
| 1571 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1572 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1573 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1574 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1575 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1576 | // before the next MBB. |
| 1577 | if (CurBlock == CurMBB) |
| 1578 | visitSwitchCase(CB); |
| 1579 | else |
| 1580 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1581 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1582 | CurBlock = FallThrough; |
| 1583 | } |
| 1584 | |
| 1585 | return true; |
| 1586 | } |
| 1587 | |
| 1588 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1589 | return !DisableJumpTables && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1590 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1591 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1592 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1593 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1594 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1595 | APInt LastExt(Last), FirstExt(First); |
| 1596 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1597 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1598 | return (LastExt - FirstExt + 1ULL); |
| 1599 | } |
| 1600 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1601 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1602 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR, |
| 1603 | CaseRecVector& WorkList, |
| 1604 | Value* SV, |
| 1605 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1606 | Case& FrontCase = *CR.Range.first; |
| 1607 | Case& BackCase = *(CR.Range.second-1); |
| 1608 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1609 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1610 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1611 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1612 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1613 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1614 | I!=E; ++I) |
| 1615 | TSize += I->size(); |
| 1616 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1617 | if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4))) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1618 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1619 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1620 | APInt Range = ComputeRange(First, Last); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1621 | double Density = TSize.roundToDouble() / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1622 | if (Density < 0.4) |
| 1623 | return false; |
| 1624 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1625 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1626 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1627 | << "Range: " << Range |
| 1628 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1629 | |
| 1630 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1631 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1632 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1633 | |
| 1634 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1635 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1636 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1637 | |
| 1638 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1639 | |
| 1640 | // Create a new basic block to hold the code for loading the address |
| 1641 | // of the jump table, and jumping to it. Update successor information; |
| 1642 | // we will either branch to the default case for the switch, or the jump |
| 1643 | // table. |
| 1644 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1645 | CurMF->insert(BBI, JumpTableBB); |
| 1646 | CR.CaseBB->addSuccessor(Default); |
| 1647 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1648 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1649 | // Build a vector of destination BBs, corresponding to each target |
| 1650 | // of the jump table. If the value of the jump table slot corresponds to |
| 1651 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1652 | // the default BB. |
| 1653 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1654 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1655 | 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] | 1656 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1657 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1658 | |
| 1659 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1660 | DestBBs.push_back(I->BB); |
| 1661 | if (TEI==High) |
| 1662 | ++I; |
| 1663 | } else { |
| 1664 | DestBBs.push_back(Default); |
| 1665 | } |
| 1666 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1667 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1668 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1669 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1670 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1671 | E = DestBBs.end(); I != E; ++I) { |
| 1672 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1673 | SuccsHandled[(*I)->getNumber()] = true; |
| 1674 | JumpTableBB->addSuccessor(*I); |
| 1675 | } |
| 1676 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1677 | |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 1678 | // Create a jump table index for this jump table. |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1679 | unsigned JTEncoding = TLI.getJumpTableEncoding(); |
| 1680 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 1681 | ->createJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1682 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1683 | // Set the jump table information so that we can codegen it as a second |
| 1684 | // MachineBasicBlock |
| 1685 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
| 1686 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB)); |
| 1687 | if (CR.CaseBB == CurMBB) |
| 1688 | visitJumpTableHeader(JT, JTH); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1689 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1690 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1691 | |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
| 1695 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1696 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1697 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 1698 | CaseRecVector& WorkList, |
| 1699 | Value* SV, |
| 1700 | MachineBasicBlock* Default) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1701 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1702 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1703 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1704 | |
| 1705 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1706 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1707 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1708 | |
| 1709 | Case& FrontCase = *CR.Range.first; |
| 1710 | Case& BackCase = *(CR.Range.second-1); |
| 1711 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1712 | |
| 1713 | // Size is the number of Cases represented by this range. |
| 1714 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1715 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1716 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1717 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1718 | double FMetric = 0; |
| 1719 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1720 | |
| 1721 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1722 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1723 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1724 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1725 | I!=E; ++I) |
| 1726 | TSize += I->size(); |
| 1727 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1728 | APInt LSize = FrontCase.size(); |
| 1729 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1730 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1731 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1732 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1733 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1734 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1735 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1736 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1737 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1738 | assert((Range - 2ULL).isNonNegative() && |
| 1739 | "Invalid case distance"); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1740 | double LDensity = (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1741 | (LEnd - First + 1ULL).roundToDouble(); |
| 1742 | double RDensity = (double)RSize.roundToDouble() / |
| 1743 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1744 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1745 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1746 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1747 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1748 | << "LDensity: " << LDensity |
| 1749 | << ", RDensity: " << RDensity << '\n' |
| 1750 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1751 | if (FMetric < Metric) { |
| 1752 | Pivot = J; |
| 1753 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1754 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | LSize += J->size(); |
| 1758 | RSize -= J->size(); |
| 1759 | } |
| 1760 | if (areJTsAllowed(TLI)) { |
| 1761 | // If our case is dense we *really* should handle it earlier! |
| 1762 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1763 | } else { |
| 1764 | Pivot = CR.Range.first + Size/2; |
| 1765 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1766 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1767 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1768 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1769 | Constant *C = Pivot->Low; |
| 1770 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1771 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1772 | // We know that we branch to the LHS if the Value being switched on is |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1773 | // less than the Pivot value, C. We use this to optimize our binary |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1774 | // tree a bit, by recognizing that if SV is greater than or equal to the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1775 | // LHS's Case Value, and that Case Value is exactly one less than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1776 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1777 | // rather than creating a leaf node for it. |
| 1778 | if ((LHSR.second - LHSR.first) == 1 && |
| 1779 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1780 | cast<ConstantInt>(C)->getValue() == |
| 1781 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1782 | TrueBB = LHSR.first->BB; |
| 1783 | } else { |
| 1784 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1785 | CurMF->insert(BBI, TrueBB); |
| 1786 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1787 | |
| 1788 | // Put SV in a virtual register to make it available from the new blocks. |
| 1789 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1790 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1791 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1792 | // Similar to the optimization above, if the Value being switched on is |
| 1793 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1794 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1795 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1796 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1797 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1798 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1799 | FalseBB = RHSR.first->BB; |
| 1800 | } else { |
| 1801 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1802 | CurMF->insert(BBI, FalseBB); |
| 1803 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1804 | |
| 1805 | // Put SV in a virtual register to make it available from the new blocks. |
| 1806 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1810 | // the LHS node if the value being switched on SV is less than C. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1811 | // Otherwise, branch to LHS. |
| 1812 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1813 | |
| 1814 | if (CR.CaseBB == CurMBB) |
| 1815 | visitSwitchCase(CB); |
| 1816 | else |
| 1817 | SwitchCases.push_back(CB); |
| 1818 | |
| 1819 | return true; |
| 1820 | } |
| 1821 | |
| 1822 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1823 | /// range span less, than machine word bitwidth, encode case range into series |
| 1824 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1825 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 1826 | CaseRecVector& WorkList, |
| 1827 | Value* SV, |
| 1828 | MachineBasicBlock* Default){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1829 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 1830 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1831 | |
| 1832 | Case& FrontCase = *CR.Range.first; |
| 1833 | Case& BackCase = *(CR.Range.second-1); |
| 1834 | |
| 1835 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1836 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1837 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1838 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 1839 | // If target does not have legal shift left, do not emit bit tests at all. |
| 1840 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 1841 | return false; |
| 1842 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1843 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1844 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1845 | I!=E; ++I) { |
| 1846 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1847 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1848 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1849 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1850 | // Count unique destinations |
| 1851 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1852 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1853 | Dests.insert(I->BB); |
| 1854 | if (Dests.size() > 3) |
| 1855 | // Don't bother the code below, if there are too much unique destinations |
| 1856 | return false; |
| 1857 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1858 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1859 | << Dests.size() << '\n' |
| 1860 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1861 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1862 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1863 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1864 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1865 | APInt cmpRange = maxValue - minValue; |
| 1866 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1867 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1868 | << "Low bound: " << minValue << '\n' |
| 1869 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1870 | |
| 1871 | if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1872 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 1873 | !(Dests.size() == 2 && numCmps >= 5) && |
| 1874 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 1875 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1876 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1877 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1878 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 1879 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1880 | // Optimize the case where all the case values fit in a |
| 1881 | // word without having to subtract minValue. In this case, |
| 1882 | // we can optimize away the subtraction. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1883 | if (minValue.isNonNegative() && |
| 1884 | maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) { |
| 1885 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1886 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1887 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1888 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1889 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1890 | CaseBitsVector CasesBits; |
| 1891 | unsigned i, count = 0; |
| 1892 | |
| 1893 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1894 | MachineBasicBlock* Dest = I->BB; |
| 1895 | for (i = 0; i < count; ++i) |
| 1896 | if (Dest == CasesBits[i].BB) |
| 1897 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1898 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1899 | if (i == count) { |
| 1900 | assert((count < 3) && "Too much destinations to test!"); |
| 1901 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 1902 | count++; |
| 1903 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1904 | |
| 1905 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 1906 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 1907 | |
| 1908 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 1909 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 1910 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1911 | for (uint64_t j = lo; j <= hi; j++) { |
| 1912 | CasesBits[i].Mask |= 1ULL << j; |
| 1913 | CasesBits[i].Bits++; |
| 1914 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1915 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1916 | } |
| 1917 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1918 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1919 | BitTestInfo BTC; |
| 1920 | |
| 1921 | // Figure out which block is immediately after the current one. |
| 1922 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1923 | ++BBI; |
| 1924 | |
| 1925 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1926 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1927 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1928 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1929 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1930 | << ", Bits: " << CasesBits[i].Bits |
| 1931 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1932 | |
| 1933 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1934 | CurMF->insert(BBI, CaseBB); |
| 1935 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 1936 | CaseBB, |
| 1937 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Put SV in a virtual register to make it available from the new blocks. |
| 1940 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1941 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1942 | |
| 1943 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1944 | -1U, (CR.CaseBB == CurMBB), |
| 1945 | CR.CaseBB, Default, BTC); |
| 1946 | |
| 1947 | if (CR.CaseBB == CurMBB) |
| 1948 | visitBitTestHeader(BTB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1949 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1950 | BitTestCases.push_back(BTB); |
| 1951 | |
| 1952 | return true; |
| 1953 | } |
| 1954 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1955 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1956 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 1957 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1958 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1961 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1962 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 1963 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 1964 | SI.getSuccessorValue(i), |
| 1965 | SMBB)); |
| 1966 | } |
| 1967 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 1968 | |
| 1969 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1970 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1971 | // Must recompute end() each iteration because it may be |
| 1972 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1973 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 1974 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 1975 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1976 | MachineBasicBlock* nextBB = J->BB; |
| 1977 | MachineBasicBlock* currentBB = I->BB; |
| 1978 | |
| 1979 | // If the two neighboring cases go to the same destination, merge them |
| 1980 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1981 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1982 | I->High = J->High; |
| 1983 | J = Cases.erase(J); |
| 1984 | } else { |
| 1985 | I = J++; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 1990 | if (I->Low != I->High) |
| 1991 | // A range counts double, since it requires two compares. |
| 1992 | ++numCmps; |
| 1993 | } |
| 1994 | |
| 1995 | return numCmps; |
| 1996 | } |
| 1997 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1998 | void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1999 | // Figure out which block is immediately after the current one. |
| 2000 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2001 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2002 | |
| 2003 | // If there is only the default destination, branch to it if it is not the |
| 2004 | // next basic block. Otherwise, just fall through. |
| 2005 | if (SI.getNumOperands() == 2) { |
| 2006 | // Update machine-CFG edges. |
| 2007 | |
| 2008 | // If this is not a fall-through branch, emit the branch. |
| 2009 | CurMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2010 | if (Default != NextBlock) |
| 2011 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 2012 | MVT::Other, getControlRoot(), |
| 2013 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2014 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2015 | return; |
| 2016 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2017 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2018 | // If there are any non-default case statements, create a vector of Cases |
| 2019 | // representing each one, and sort the vector so that we can efficiently |
| 2020 | // create a binary search tree from them. |
| 2021 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2022 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2023 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2024 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2025 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2026 | |
| 2027 | // Get the Value to be switched on and default basic blocks, which will be |
| 2028 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2029 | // search tree. |
| 2030 | Value *SV = SI.getOperand(0); |
| 2031 | |
| 2032 | // Push the initial CaseRec onto the worklist |
| 2033 | CaseRecVector WorkList; |
| 2034 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 2035 | |
| 2036 | while (!WorkList.empty()) { |
| 2037 | // Grab a record representing a case range to process off the worklist |
| 2038 | CaseRec CR = WorkList.back(); |
| 2039 | WorkList.pop_back(); |
| 2040 | |
| 2041 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 2042 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2043 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2044 | // If the range has few cases (two or less) emit a series of specific |
| 2045 | // tests. |
| 2046 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 2047 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2048 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2049 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2050 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2051 | // lowering the switch to a binary tree of conditional branches. |
| 2052 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 2053 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2054 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2055 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2056 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 2057 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
| 2058 | } |
| 2059 | } |
| 2060 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2061 | void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) { |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2062 | // Update machine-CFG edges with unique successors. |
Jakob Stoklund Olesen | b5b90ed | 2010-02-11 18:06:56 +0000 | [diff] [blame] | 2063 | SmallVector<BasicBlock*, 32> succs; |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2064 | succs.reserve(I.getNumSuccessors()); |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2065 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2066 | succs.push_back(I.getSuccessor(i)); |
Jakob Stoklund Olesen | b5b90ed | 2010-02-11 18:06:56 +0000 | [diff] [blame] | 2067 | array_pod_sort(succs.begin(), succs.end()); |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2068 | succs.erase(std::unique(succs.begin(), succs.end()), succs.end()); |
| 2069 | for (unsigned i = 0, e = succs.size(); i != e; ++i) |
| 2070 | CurMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]); |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2071 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2072 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2073 | MVT::Other, getControlRoot(), |
| 2074 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2075 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2076 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2077 | void SelectionDAGBuilder::visitFSub(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2078 | // -0.0 - X --> fneg |
| 2079 | const Type *Ty = I.getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2080 | if (Ty->isVectorTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2081 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2082 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2083 | const Type *ElTy = DestTy->getElementType(); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2084 | unsigned VL = DestTy->getNumElements(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2085 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2086 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2087 | if (CV == CNZ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2088 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2089 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2090 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2091 | return; |
| 2092 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2093 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2094 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2095 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2096 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2097 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2098 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2099 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2100 | Op2.getValueType(), Op2)); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2101 | return; |
| 2102 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2103 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2104 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2107 | void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2108 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2109 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2110 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
| 2111 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2114 | void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2115 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2116 | SDValue Op2 = getValue(I.getOperand(1)); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2117 | if (!I.getType()->isVectorTy() && |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2118 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2119 | // If the operand is smaller than the shift count type, promote it. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2120 | EVT PTy = TLI.getPointerTy(); |
| 2121 | EVT STy = TLI.getShiftAmountTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2122 | if (STy.bitsGT(Op2.getValueType())) |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2123 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2124 | TLI.getShiftAmountTy(), Op2); |
| 2125 | // If the operand is larger than the shift count type but the shift |
| 2126 | // count type has enough bits to represent any shift value, truncate |
| 2127 | // it now. This is a common case and it exposes the truncate to |
| 2128 | // optimization early. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2129 | else if (STy.getSizeInBits() >= |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2130 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2131 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2132 | TLI.getShiftAmountTy(), Op2); |
| 2133 | // Otherwise we'll need to temporarily settle for some other |
| 2134 | // convenient type; type legalization will make adjustments as |
| 2135 | // needed. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2136 | else if (PTy.bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2137 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2138 | TLI.getPointerTy(), Op2); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2139 | else if (PTy.bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2140 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2141 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2142 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2143 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2144 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
| 2145 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2148 | void SelectionDAGBuilder::visitICmp(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2149 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2150 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2151 | predicate = IC->getPredicate(); |
| 2152 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2153 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2154 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2155 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2156 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2157 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2158 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2159 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2162 | void SelectionDAGBuilder::visitFCmp(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2163 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2164 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2165 | predicate = FC->getPredicate(); |
| 2166 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2167 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2168 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2169 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2170 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2171 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2172 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2175 | void SelectionDAGBuilder::visitSelect(User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2176 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2177 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2178 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2179 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2180 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2181 | SmallVector<SDValue, 4> Values(NumValues); |
| 2182 | SDValue Cond = getValue(I.getOperand(0)); |
| 2183 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2184 | SDValue FalseVal = getValue(I.getOperand(2)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2185 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2186 | for (unsigned i = 0; i != NumValues; ++i) |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2187 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
Chris Lattner | b3e87b2 | 2010-03-12 07:15:36 +0000 | [diff] [blame] | 2188 | TrueVal.getNode()->getValueType(TrueVal.getResNo()+i), |
| 2189 | Cond, |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2190 | SDValue(TrueVal.getNode(), |
| 2191 | TrueVal.getResNo() + i), |
| 2192 | SDValue(FalseVal.getNode(), |
| 2193 | FalseVal.getResNo() + i)); |
| 2194 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2195 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2196 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2197 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2198 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2199 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2200 | void SelectionDAGBuilder::visitTrunc(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2201 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2202 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2203 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2204 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2207 | void SelectionDAGBuilder::visitZExt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2208 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2209 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2210 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2211 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2212 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2215 | void SelectionDAGBuilder::visitSExt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2216 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2217 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 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::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2223 | void SelectionDAGBuilder::visitFPTrunc(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2224 | // FPTrunc 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_ROUND, getCurDebugLoc(), |
| 2228 | DestVT, N, DAG.getIntPtrConstant(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2231 | void SelectionDAGBuilder::visitFPExt(User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2232 | // FPTrunc is never a no-op cast, no need to check |
| 2233 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2234 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2235 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2238 | void SelectionDAGBuilder::visitFPToUI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2239 | // FPToUI is never a no-op cast, no need to check |
| 2240 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2241 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2242 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2245 | void SelectionDAGBuilder::visitFPToSI(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2246 | // FPToSI is never a no-op cast, no need to check |
| 2247 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2248 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2249 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2252 | void SelectionDAGBuilder::visitUIToFP(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2253 | // UIToFP is never a no-op cast, no need to check |
| 2254 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2255 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2256 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2259 | void SelectionDAGBuilder::visitSIToFP(User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2260 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2261 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2262 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2263 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2266 | void SelectionDAGBuilder::visitPtrToInt(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2267 | // What to do depends on the size of the integer and the size of the pointer. |
| 2268 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2269 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2270 | EVT SrcVT = N.getValueType(); |
| 2271 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2272 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2275 | void SelectionDAGBuilder::visitIntToPtr(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2276 | // What to do depends on the size of the integer and the size of the pointer. |
| 2277 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2278 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2279 | EVT SrcVT = N.getValueType(); |
| 2280 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2281 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2284 | void SelectionDAGBuilder::visitBitCast(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2285 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2286 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2287 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2288 | // BitCast assures us that source and destination are the same size so this is |
| 2289 | // either a BIT_CONVERT or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2290 | if (DestVT != N.getValueType()) |
| 2291 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 2292 | DestVT, N)); // convert types. |
| 2293 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2294 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2297 | void SelectionDAGBuilder::visitInsertElement(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2298 | SDValue InVec = getValue(I.getOperand(0)); |
| 2299 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2300 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2301 | TLI.getPointerTy(), |
| 2302 | getValue(I.getOperand(2))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2303 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
| 2304 | TLI.getValueType(I.getType()), |
| 2305 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2308 | void SelectionDAGBuilder::visitExtractElement(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2309 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2310 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2311 | TLI.getPointerTy(), |
| 2312 | getValue(I.getOperand(1))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2313 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2314 | TLI.getValueType(I.getType()), InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2317 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2318 | // from SIndx and increasing to the element length (undefs are allowed). |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2319 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2320 | unsigned MaskNumElts = Mask.size(); |
| 2321 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2322 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2323 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2324 | return true; |
| 2325 | } |
| 2326 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2327 | void SelectionDAGBuilder::visitShuffleVector(User &I) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2328 | SmallVector<int, 8> Mask; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2329 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2330 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2331 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2332 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2333 | // representing undef values. |
| 2334 | SmallVector<Constant*, 8> MaskElts; |
Chris Lattner | b29d596 | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 2335 | cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2336 | unsigned MaskNumElts = MaskElts.size(); |
| 2337 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2338 | if (isa<UndefValue>(MaskElts[i])) |
| 2339 | Mask.push_back(-1); |
| 2340 | else |
| 2341 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2342 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2343 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2344 | EVT VT = TLI.getValueType(I.getType()); |
| 2345 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2346 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2347 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2348 | if (SrcNumElts == MaskNumElts) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2349 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2350 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2351 | return; |
| 2352 | } |
| 2353 | |
| 2354 | // 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] | 2355 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2356 | // Mask is longer than the source vectors and is a multiple of the source |
| 2357 | // 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] | 2358 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2359 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2360 | // The shuffle is concatenating two vectors together. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2361 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
| 2362 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2363 | return; |
| 2364 | } |
| 2365 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2366 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2367 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2368 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2369 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2370 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2371 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2372 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2373 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2374 | MOps1[0] = Src1; |
| 2375 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2376 | |
| 2377 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2378 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2379 | &MOps1[0], NumConcat); |
| 2380 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2381 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2382 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2383 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2384 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2385 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2386 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2387 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2388 | if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2389 | MappedOps.push_back(Idx); |
| 2390 | else |
| 2391 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2392 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2393 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2394 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2395 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2396 | return; |
| 2397 | } |
| 2398 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2399 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2400 | // Analyze the access pattern of the vector to see if we can extract |
| 2401 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2402 | // the range of elements the mask access on both vectors. |
| 2403 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2404 | int MaxRange[2] = {-1, -1}; |
| 2405 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2406 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2407 | int Idx = Mask[i]; |
| 2408 | int Input = 0; |
| 2409 | if (Idx < 0) |
| 2410 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2411 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2412 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2413 | Input = 1; |
| 2414 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2415 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2416 | if (Idx > MaxRange[Input]) |
| 2417 | MaxRange[Input] = Idx; |
| 2418 | if (Idx < MinRange[Input]) |
| 2419 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2420 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2421 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2422 | // Check if the access is smaller than the vector size and can we find |
| 2423 | // a reasonable extract index. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2424 | int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not |
| 2425 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2426 | int StartIdx[2]; // StartIdx to extract from |
| 2427 | for (int Input=0; Input < 2; ++Input) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2428 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2429 | RangeUse[Input] = 0; // Unused |
| 2430 | StartIdx[Input] = 0; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2431 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2432 | // 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] | 2433 | // start index that is a multiple of the mask length. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2434 | if (MaxRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2435 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2436 | StartIdx[Input] = 0; |
| 2437 | } else { |
| 2438 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2439 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2440 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2441 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2442 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2443 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 2446 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2447 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2448 | return; |
| 2449 | } |
| 2450 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2451 | // Extract appropriate subvector and generate a vector shuffle |
| 2452 | for (int Input=0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2453 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2454 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2455 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2456 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2457 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2458 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2459 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2460 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2461 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2462 | SmallVector<int, 8> MappedOps; |
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 | int Idx = Mask[i]; |
| 2465 | if (Idx < 0) |
| 2466 | MappedOps.push_back(Idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2467 | else if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2468 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2469 | else |
| 2470 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2471 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2472 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2473 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2474 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2475 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2479 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2480 | // replacing the shuffle with extract and build vector. |
| 2481 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2482 | EVT EltVT = VT.getVectorElementType(); |
| 2483 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2484 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2485 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2486 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2487 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2488 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2489 | int Idx = Mask[i]; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2490 | SDValue Res; |
| 2491 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2492 | if (Idx < (int)SrcNumElts) |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2493 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2494 | EltVT, Src1, DAG.getConstant(Idx, PtrVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2495 | else |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2496 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2497 | EltVT, Src2, |
| 2498 | DAG.getConstant(Idx - SrcNumElts, PtrVT)); |
| 2499 | |
| 2500 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2501 | } |
| 2502 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2503 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2504 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2505 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2508 | void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2509 | const Value *Op0 = I.getOperand(0); |
| 2510 | const Value *Op1 = I.getOperand(1); |
| 2511 | const Type *AggTy = I.getType(); |
| 2512 | const Type *ValTy = Op1->getType(); |
| 2513 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2514 | bool FromUndef = isa<UndefValue>(Op1); |
| 2515 | |
| 2516 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2517 | I.idx_begin(), I.idx_end()); |
| 2518 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2519 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2520 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2521 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2522 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2523 | |
| 2524 | unsigned NumAggValues = AggValueVTs.size(); |
| 2525 | unsigned NumValValues = ValValueVTs.size(); |
| 2526 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2527 | |
| 2528 | SDValue Agg = getValue(Op0); |
| 2529 | SDValue Val = getValue(Op1); |
| 2530 | unsigned i = 0; |
| 2531 | // Copy the beginning value(s) from the original aggregate. |
| 2532 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2533 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2534 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2535 | // Copy values from the inserted value(s). |
| 2536 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2537 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2538 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2539 | // Copy remaining value(s) from the original aggregate. |
| 2540 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2541 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2542 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2543 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2544 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2545 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2546 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2549 | void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2550 | const Value *Op0 = I.getOperand(0); |
| 2551 | const Type *AggTy = Op0->getType(); |
| 2552 | const Type *ValTy = I.getType(); |
| 2553 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2554 | |
| 2555 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2556 | I.idx_begin(), I.idx_end()); |
| 2557 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2558 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2559 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2560 | |
| 2561 | unsigned NumValValues = ValValueVTs.size(); |
| 2562 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2563 | |
| 2564 | SDValue Agg = getValue(Op0); |
| 2565 | // Copy out the selected value(s). |
| 2566 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2567 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2568 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2569 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2570 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2571 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2572 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2573 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2574 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2577 | void SelectionDAGBuilder::visitGetElementPtr(User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2578 | SDValue N = getValue(I.getOperand(0)); |
| 2579 | const Type *Ty = I.getOperand(0)->getType(); |
| 2580 | |
| 2581 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2582 | OI != E; ++OI) { |
| 2583 | Value *Idx = *OI; |
| 2584 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2585 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2586 | if (Field) { |
| 2587 | // N = N + Offset |
| 2588 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2589 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2590 | DAG.getIntPtrConstant(Offset)); |
| 2591 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2592 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2593 | Ty = StTy->getElementType(Field); |
Chris Lattner | 93b122d | 2010-03-16 21:25:55 +0000 | [diff] [blame] | 2594 | } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) { |
| 2595 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2596 | |
| 2597 | // Offset canonically 0 for unions, but type changes |
| 2598 | Ty = UnTy->getElementType(Field); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2599 | } else { |
| 2600 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2601 | |
| 2602 | // If this is a constant subscript, handle it quickly. |
| 2603 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2604 | if (CI->getZExtValue() == 0) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2605 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2606 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2607 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2608 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2609 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2610 | if (PtrBits < 64) |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2611 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2612 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2613 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2614 | else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2615 | OffsVal = DAG.getIntPtrConstant(Offs); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2616 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2617 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2618 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2619 | continue; |
| 2620 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2621 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2622 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2623 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 2624 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2625 | SDValue IdxN = getValue(Idx); |
| 2626 | |
| 2627 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2628 | // it. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2629 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2630 | |
| 2631 | // If this is a multiply by a power of two, turn it into a shl |
| 2632 | // immediately. This is a very common case. |
| 2633 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2634 | if (ElementSize.isPowerOf2()) { |
| 2635 | unsigned Amt = ElementSize.logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2636 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2637 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2638 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2639 | } else { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2640 | SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2641 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2642 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2643 | } |
| 2644 | } |
| 2645 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2646 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2647 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2648 | } |
| 2649 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2650 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2651 | setValue(&I, N); |
| 2652 | } |
| 2653 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2654 | void SelectionDAGBuilder::visitAlloca(AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2655 | // If this is a fixed sized alloca in the entry block of the function, |
| 2656 | // allocate it statically on the stack. |
| 2657 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2658 | return; // getValue will auto-populate this. |
| 2659 | |
| 2660 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2661 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2662 | unsigned Align = |
| 2663 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2664 | I.getAlignment()); |
| 2665 | |
| 2666 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2667 | |
Chris Lattner | 0b18e59 | 2009-03-17 19:36:00 +0000 | [diff] [blame] | 2668 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(), |
| 2669 | AllocSize, |
| 2670 | DAG.getConstant(TySize, AllocSize.getValueType())); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2671 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2672 | EVT IntPtr = TLI.getPointerTy(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2673 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2674 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2675 | // Handle alignment. If the requested alignment is less than or equal to |
| 2676 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2677 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
| 2678 | unsigned StackAlign = |
| 2679 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 2680 | if (Align <= StackAlign) |
| 2681 | Align = 0; |
| 2682 | |
| 2683 | // Round the size of the allocation up to the stack alignment size |
| 2684 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2685 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2686 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2687 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2688 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2689 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2690 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2691 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2692 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2693 | |
| 2694 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2695 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2696 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2697 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2698 | setValue(&I, DSA); |
| 2699 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2700 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2701 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2702 | // object. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2703 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2706 | void SelectionDAGBuilder::visitLoad(LoadInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2707 | const Value *SV = I.getOperand(0); |
| 2708 | SDValue Ptr = getValue(SV); |
| 2709 | |
| 2710 | const Type *Ty = I.getType(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2711 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2712 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2713 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2714 | unsigned Alignment = I.getAlignment(); |
| 2715 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2716 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2717 | SmallVector<uint64_t, 4> Offsets; |
| 2718 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2719 | unsigned NumValues = ValueVTs.size(); |
| 2720 | if (NumValues == 0) |
| 2721 | return; |
| 2722 | |
| 2723 | SDValue Root; |
| 2724 | bool ConstantMemory = false; |
| 2725 | if (I.isVolatile()) |
| 2726 | // Serialize volatile loads with other side effects. |
| 2727 | Root = getRoot(); |
| 2728 | else if (AA->pointsToConstantMemory(SV)) { |
| 2729 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2730 | Root = DAG.getEntryNode(); |
| 2731 | ConstantMemory = true; |
| 2732 | } else { |
| 2733 | // Do not serialize non-volatile loads against each other. |
| 2734 | Root = DAG.getRoot(); |
| 2735 | } |
| 2736 | |
| 2737 | SmallVector<SDValue, 4> Values(NumValues); |
| 2738 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2739 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2740 | for (unsigned i = 0; i != NumValues; ++i) { |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2741 | SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2742 | PtrVT, Ptr, |
| 2743 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2744 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2745 | A, SV, Offsets[i], isVolatile, |
| 2746 | isNonTemporal, Alignment); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2747 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2748 | Values[i] = L; |
| 2749 | Chains[i] = L.getValue(1); |
| 2750 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2751 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2752 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2753 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2754 | MVT::Other, &Chains[0], NumValues); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2755 | if (isVolatile) |
| 2756 | DAG.setRoot(Chain); |
| 2757 | else |
| 2758 | PendingLoads.push_back(Chain); |
| 2759 | } |
| 2760 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2761 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2762 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2763 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2764 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2765 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2766 | void SelectionDAGBuilder::visitStore(StoreInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2767 | Value *SrcV = I.getOperand(0); |
| 2768 | Value *PtrV = I.getOperand(1); |
| 2769 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2770 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2771 | SmallVector<uint64_t, 4> Offsets; |
| 2772 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2773 | unsigned NumValues = ValueVTs.size(); |
| 2774 | if (NumValues == 0) |
| 2775 | return; |
| 2776 | |
| 2777 | // Get the lowered operands. Note that we do this after |
| 2778 | // checking if NumResults is zero, because with zero results |
| 2779 | // the operands won't have values in the map. |
| 2780 | SDValue Src = getValue(SrcV); |
| 2781 | SDValue Ptr = getValue(PtrV); |
| 2782 | |
| 2783 | SDValue Root = getRoot(); |
| 2784 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2785 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2786 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2787 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2788 | unsigned Alignment = I.getAlignment(); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2789 | |
| 2790 | for (unsigned i = 0; i != NumValues; ++i) { |
| 2791 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr, |
| 2792 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2793 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2794 | SDValue(Src.getNode(), Src.getResNo() + i), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2795 | Add, PtrV, Offsets[i], isVolatile, |
| 2796 | isNonTemporal, Alignment); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2799 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 2800 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2804 | /// node. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2805 | void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I, |
| 2806 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2807 | bool HasChain = !I.doesNotAccessMemory(); |
| 2808 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2809 | |
| 2810 | // Build the operand list. |
| 2811 | SmallVector<SDValue, 8> Ops; |
| 2812 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2813 | if (OnlyLoad) { |
| 2814 | // We don't need to serialize loads against other loads. |
| 2815 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2816 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2817 | Ops.push_back(getRoot()); |
| 2818 | } |
| 2819 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2820 | |
| 2821 | // Info is set by getTgtMemInstrinsic |
| 2822 | TargetLowering::IntrinsicInfo Info; |
| 2823 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2824 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2825 | // 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] | 2826 | if (!IsTgtIntrinsic) |
| 2827 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2828 | |
| 2829 | // Add all operands of the call to the operand list. |
| 2830 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2831 | SDValue Op = getValue(I.getOperand(i)); |
| 2832 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2833 | "Intrinsic uses a non-legal type?"); |
| 2834 | Ops.push_back(Op); |
| 2835 | } |
| 2836 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2837 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2838 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2839 | #ifndef NDEBUG |
| 2840 | for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) { |
| 2841 | assert(TLI.isTypeLegal(ValueVTs[Val]) && |
| 2842 | "Intrinsic uses a non-legal type?"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2843 | } |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2844 | #endif // NDEBUG |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2845 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2846 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2847 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2848 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2849 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2850 | |
| 2851 | // Create the node. |
| 2852 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2853 | if (IsTgtIntrinsic) { |
| 2854 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2855 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2856 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2857 | Info.memVT, Info.ptrVal, Info.offset, |
| 2858 | Info.align, Info.vol, |
| 2859 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2860 | } else if (!HasChain) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2861 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2862 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2863 | } else if (!I.getType()->isVoidTy()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2864 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2865 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2866 | } else { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2867 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2868 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2871 | if (HasChain) { |
| 2872 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2873 | if (OnlyLoad) |
| 2874 | PendingLoads.push_back(Chain); |
| 2875 | else |
| 2876 | DAG.setRoot(Chain); |
| 2877 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2878 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2879 | if (!I.getType()->isVoidTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2880 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2881 | EVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2882 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2883 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2884 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2885 | setValue(&I, Result); |
| 2886 | } |
| 2887 | } |
| 2888 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2889 | /// GetSignificand - Get the significand and build it into a floating-point |
| 2890 | /// number with exponent of 1: |
| 2891 | /// |
| 2892 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 2893 | /// |
| 2894 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2895 | static SDValue |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 2896 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2897 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2898 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 2899 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 2900 | DAG.getConstant(0x3f800000, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2901 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2904 | /// GetExponent - Get the exponent: |
| 2905 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 2906 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2907 | /// |
| 2908 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2909 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2910 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 2911 | DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2912 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 2913 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 2914 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2915 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2916 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 2917 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2918 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2921 | /// getF32Constant - Get 32-bit floating point constant. |
| 2922 | static SDValue |
| 2923 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2924 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2927 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2928 | /// visitIntrinsicCall: I is a call instruction |
| 2929 | /// Op is the associated NodeType for I |
| 2930 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2931 | SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2932 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2933 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2934 | DAG.getAtomic(Op, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2935 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2936 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2937 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 2938 | getValue(I.getOperand(2)), |
| 2939 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2940 | setValue(&I, L); |
| 2941 | DAG.setRoot(L.getValue(1)); |
| 2942 | return 0; |
| 2943 | } |
| 2944 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2945 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2946 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2947 | SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) { |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2948 | SDValue Op1 = getValue(I.getOperand(1)); |
| 2949 | SDValue Op2 = getValue(I.getOperand(2)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2950 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2951 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2952 | setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2)); |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 2953 | return 0; |
| 2954 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2955 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2956 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 2957 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2958 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2959 | SelectionDAGBuilder::visitExp(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2960 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2961 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2962 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2963 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2964 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 2965 | SDValue Op = getValue(I.getOperand(1)); |
| 2966 | |
| 2967 | // Put the exponent in the right bit position for later addition to the |
| 2968 | // final result: |
| 2969 | // |
| 2970 | // #define LOG2OFe 1.4426950f |
| 2971 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2972 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2973 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2974 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2975 | |
| 2976 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2977 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 2978 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2979 | |
| 2980 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2981 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2982 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2983 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2984 | if (LimitFloatPrecision <= 6) { |
| 2985 | // For floating-point precision of 6: |
| 2986 | // |
| 2987 | // TwoToFractionalPartOfX = |
| 2988 | // 0.997535578f + |
| 2989 | // (0.735607626f + 0.252464424f * x) * x; |
| 2990 | // |
| 2991 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2992 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2993 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2994 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2995 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2996 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 2997 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2998 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2999 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3000 | |
| 3001 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3002 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3003 | TwoToFracPartOfX, IntegerPartOfX); |
| 3004 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3005 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3006 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3007 | // For floating-point precision of 12: |
| 3008 | // |
| 3009 | // TwoToFractionalPartOfX = |
| 3010 | // 0.999892986f + |
| 3011 | // (0.696457318f + |
| 3012 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3013 | // |
| 3014 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3015 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3016 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3017 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3018 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3019 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3020 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3021 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3022 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3023 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3024 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3025 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3026 | |
| 3027 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3028 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3029 | TwoToFracPartOfX, IntegerPartOfX); |
| 3030 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3031 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3032 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3033 | // For floating-point precision of 18: |
| 3034 | // |
| 3035 | // TwoToFractionalPartOfX = |
| 3036 | // 0.999999982f + |
| 3037 | // (0.693148872f + |
| 3038 | // (0.240227044f + |
| 3039 | // (0.554906021e-1f + |
| 3040 | // (0.961591928e-2f + |
| 3041 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3042 | // |
| 3043 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3044 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3045 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3046 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3047 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3048 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3049 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3050 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3051 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3052 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3053 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3054 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3055 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3056 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3057 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3058 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3059 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3060 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3061 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3062 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3063 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3064 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3065 | |
| 3066 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3067 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3068 | TwoToFracPartOfX, IntegerPartOfX); |
| 3069 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3070 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3071 | } |
| 3072 | } else { |
| 3073 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3074 | result = DAG.getNode(ISD::FEXP, dl, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3075 | getValue(I.getOperand(1)).getValueType(), |
| 3076 | getValue(I.getOperand(1))); |
| 3077 | } |
| 3078 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3079 | setValue(&I, result); |
| 3080 | } |
| 3081 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3082 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3083 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3084 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3085 | SelectionDAGBuilder::visitLog(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3086 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3087 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3088 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3089 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3090 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3091 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3092 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3093 | |
| 3094 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3095 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3096 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3097 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3098 | |
| 3099 | // Get the significand and build it into a floating-point number with |
| 3100 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3101 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3102 | |
| 3103 | if (LimitFloatPrecision <= 6) { |
| 3104 | // For floating-point precision of 6: |
| 3105 | // |
| 3106 | // LogofMantissa = |
| 3107 | // -1.1609546f + |
| 3108 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3109 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3110 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3111 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3112 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3113 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3114 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3115 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3116 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3117 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3118 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3119 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3120 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3121 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3122 | // For floating-point precision of 12: |
| 3123 | // |
| 3124 | // LogOfMantissa = |
| 3125 | // -1.7417939f + |
| 3126 | // (2.8212026f + |
| 3127 | // (-1.4699568f + |
| 3128 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3129 | // |
| 3130 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3131 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3132 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3133 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3134 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3135 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3136 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3137 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3138 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3139 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3140 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3141 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3142 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3143 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3144 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3145 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3146 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3147 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3148 | // For floating-point precision of 18: |
| 3149 | // |
| 3150 | // LogOfMantissa = |
| 3151 | // -2.1072184f + |
| 3152 | // (4.2372794f + |
| 3153 | // (-3.7029485f + |
| 3154 | // (2.2781945f + |
| 3155 | // (-0.87823314f + |
| 3156 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3157 | // |
| 3158 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3159 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3160 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3161 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3162 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3163 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3164 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3165 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3166 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3167 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3168 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3169 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3170 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3171 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3172 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3173 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3174 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3175 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3176 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3177 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3178 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3179 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3180 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3181 | } |
| 3182 | } else { |
| 3183 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3184 | result = DAG.getNode(ISD::FLOG, dl, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3185 | getValue(I.getOperand(1)).getValueType(), |
| 3186 | getValue(I.getOperand(1))); |
| 3187 | } |
| 3188 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3189 | setValue(&I, result); |
| 3190 | } |
| 3191 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3192 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3193 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3194 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3195 | SelectionDAGBuilder::visitLog2(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3196 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3197 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3198 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3199 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3200 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3201 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3202 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3203 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3204 | // Get the exponent. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3205 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3206 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3207 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3208 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3209 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3210 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3211 | // Different possible minimax approximations of significand in |
| 3212 | // floating-point for various degrees of accuracy over [1,2]. |
| 3213 | if (LimitFloatPrecision <= 6) { |
| 3214 | // For floating-point precision of 6: |
| 3215 | // |
| 3216 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3217 | // |
| 3218 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3219 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3220 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3221 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3222 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3223 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3224 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3225 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3226 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3227 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3228 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3229 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3230 | // For floating-point precision of 12: |
| 3231 | // |
| 3232 | // Log2ofMantissa = |
| 3233 | // -2.51285454f + |
| 3234 | // (4.07009056f + |
| 3235 | // (-2.12067489f + |
| 3236 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3237 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3238 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3239 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3240 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3241 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3242 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3243 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3244 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3245 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3246 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3247 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3248 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3249 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3250 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3251 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3252 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3253 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3254 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3255 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3256 | // For floating-point precision of 18: |
| 3257 | // |
| 3258 | // Log2ofMantissa = |
| 3259 | // -3.0400495f + |
| 3260 | // (6.1129976f + |
| 3261 | // (-5.3420409f + |
| 3262 | // (3.2865683f + |
| 3263 | // (-1.2669343f + |
| 3264 | // (0.27515199f - |
| 3265 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3266 | // |
| 3267 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3268 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3269 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3270 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3271 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3272 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3273 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3274 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3275 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3276 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3277 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3278 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3279 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3280 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3281 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3282 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3283 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3284 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3285 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3286 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3287 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3288 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3289 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3290 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3291 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3292 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3293 | result = DAG.getNode(ISD::FLOG2, dl, |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3294 | getValue(I.getOperand(1)).getValueType(), |
| 3295 | getValue(I.getOperand(1))); |
| 3296 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3297 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3298 | setValue(&I, result); |
| 3299 | } |
| 3300 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3301 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3302 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3303 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3304 | SelectionDAGBuilder::visitLog10(CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3305 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3306 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3307 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3308 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3309 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3310 | SDValue Op = getValue(I.getOperand(1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3311 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3312 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3313 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3314 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3315 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3316 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3317 | |
| 3318 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3319 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3320 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3321 | |
| 3322 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3323 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3324 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3325 | // Log10ofMantissa = |
| 3326 | // -0.50419619f + |
| 3327 | // (0.60948995f - 0.10380950f * x) * x; |
| 3328 | // |
| 3329 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3330 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3331 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3332 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3333 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3334 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3335 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3336 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +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 if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3341 | // For floating-point precision of 12: |
| 3342 | // |
| 3343 | // Log10ofMantissa = |
| 3344 | // -0.64831180f + |
| 3345 | // (0.91751397f + |
| 3346 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3347 | // |
| 3348 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3349 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3350 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3351 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3352 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3353 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3354 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3355 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3356 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3357 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3358 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3359 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3360 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3361 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3362 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3363 | // For floating-point precision of 18: |
| 3364 | // |
| 3365 | // Log10ofMantissa = |
| 3366 | // -0.84299375f + |
| 3367 | // (1.5327582f + |
| 3368 | // (-1.0688956f + |
| 3369 | // (0.49102474f + |
| 3370 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3371 | // |
| 3372 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3373 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3374 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3375 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3376 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3377 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3378 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3379 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3380 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3381 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3382 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3383 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3384 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3385 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3386 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3387 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3388 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3389 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3390 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3391 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3392 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3393 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3394 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3395 | result = DAG.getNode(ISD::FLOG10, dl, |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3396 | getValue(I.getOperand(1)).getValueType(), |
| 3397 | getValue(I.getOperand(1))); |
| 3398 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3399 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3400 | setValue(&I, result); |
| 3401 | } |
| 3402 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3403 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3404 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3405 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3406 | SelectionDAGBuilder::visitExp2(CallInst &I) { |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3407 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3408 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3409 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3410 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3411 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3412 | SDValue Op = getValue(I.getOperand(1)); |
| 3413 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3414 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3415 | |
| 3416 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3417 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3418 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3419 | |
| 3420 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3421 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3422 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3423 | |
| 3424 | if (LimitFloatPrecision <= 6) { |
| 3425 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3426 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3427 | // TwoToFractionalPartOfX = |
| 3428 | // 0.997535578f + |
| 3429 | // (0.735607626f + 0.252464424f * x) * x; |
| 3430 | // |
| 3431 | // error 0.0144103317, which is 6 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, 0x3e814304)); |
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, 0x3f3c50c8)); |
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, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3439 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3440 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3441 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3442 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3443 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3444 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3445 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3446 | // For floating-point precision of 12: |
| 3447 | // |
| 3448 | // TwoToFractionalPartOfX = |
| 3449 | // 0.999892986f + |
| 3450 | // (0.696457318f + |
| 3451 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3452 | // |
| 3453 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3454 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3455 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3456 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3457 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3458 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3459 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3460 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3461 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3462 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3463 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3464 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3465 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3466 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3467 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3468 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3469 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3470 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3471 | // For floating-point precision of 18: |
| 3472 | // |
| 3473 | // TwoToFractionalPartOfX = |
| 3474 | // 0.999999982f + |
| 3475 | // (0.693148872f + |
| 3476 | // (0.240227044f + |
| 3477 | // (0.554906021e-1f + |
| 3478 | // (0.961591928e-2f + |
| 3479 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3480 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3481 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3482 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3483 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3484 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3485 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3486 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3487 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3488 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3489 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3490 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3491 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3492 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3493 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3494 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3495 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3496 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3497 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3498 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3499 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3500 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3501 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3502 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3503 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3504 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3505 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3506 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3507 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3508 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3509 | result = DAG.getNode(ISD::FEXP2, dl, |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3510 | getValue(I.getOperand(1)).getValueType(), |
| 3511 | getValue(I.getOperand(1))); |
| 3512 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3513 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3514 | setValue(&I, result); |
| 3515 | } |
| 3516 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3517 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3518 | /// limited-precision mode with x == 10.0f. |
| 3519 | void |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3520 | SelectionDAGBuilder::visitPow(CallInst &I) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3521 | SDValue result; |
| 3522 | Value *Val = I.getOperand(1); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3523 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3524 | bool IsExp10 = false; |
| 3525 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3526 | if (getValue(Val).getValueType() == MVT::f32 && |
| 3527 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3528 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3529 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3530 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3531 | APFloat Ten(10.0f); |
| 3532 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3538 | SDValue Op = getValue(I.getOperand(2)); |
| 3539 | |
| 3540 | // Put the exponent in the right bit position for later addition to the |
| 3541 | // final result: |
| 3542 | // |
| 3543 | // #define LOG2OF10 3.3219281f |
| 3544 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3545 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3546 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3547 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3548 | |
| 3549 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3550 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3551 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3552 | |
| 3553 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3554 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3555 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3556 | |
| 3557 | if (LimitFloatPrecision <= 6) { |
| 3558 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3559 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3560 | // twoToFractionalPartOfX = |
| 3561 | // 0.997535578f + |
| 3562 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3563 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3564 | // error 0.0144103317, which is 6 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, 0x3e814304)); |
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, 0x3f3c50c8)); |
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, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3572 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3573 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3574 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3575 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3576 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3577 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3578 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3579 | // For floating-point precision of 12: |
| 3580 | // |
| 3581 | // TwoToFractionalPartOfX = |
| 3582 | // 0.999892986f + |
| 3583 | // (0.696457318f + |
| 3584 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3585 | // |
| 3586 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3587 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3588 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3589 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3590 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3591 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3592 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3593 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3594 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3595 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3596 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3597 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3598 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3599 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3600 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3601 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3602 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3603 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3604 | // For floating-point precision of 18: |
| 3605 | // |
| 3606 | // TwoToFractionalPartOfX = |
| 3607 | // 0.999999982f + |
| 3608 | // (0.693148872f + |
| 3609 | // (0.240227044f + |
| 3610 | // (0.554906021e-1f + |
| 3611 | // (0.961591928e-2f + |
| 3612 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3613 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3614 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3615 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3616 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3617 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3618 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3619 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3620 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3621 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3622 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3623 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3624 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3625 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3626 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3627 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3628 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3629 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3630 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3631 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3632 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3633 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3634 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3635 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3636 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3637 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3638 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3639 | } |
| 3640 | } else { |
| 3641 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3642 | result = DAG.getNode(ISD::FPOW, dl, |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3643 | getValue(I.getOperand(1)).getValueType(), |
| 3644 | getValue(I.getOperand(1)), |
| 3645 | getValue(I.getOperand(2))); |
| 3646 | } |
| 3647 | |
| 3648 | setValue(&I, result); |
| 3649 | } |
| 3650 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3651 | |
| 3652 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
| 3653 | static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS, |
| 3654 | SelectionDAG &DAG) { |
| 3655 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 3656 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 3657 | // optimizing for size, we only want to do this if the expansion would produce |
| 3658 | // a small number of multiplies, otherwise we do the full expansion. |
| 3659 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 3660 | // Get the exponent as a positive value. |
| 3661 | unsigned Val = RHSC->getSExtValue(); |
| 3662 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3663 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3664 | // powi(x, 0) -> 1.0 |
| 3665 | if (Val == 0) |
| 3666 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 3667 | |
| 3668 | Function *F = DAG.getMachineFunction().getFunction(); |
| 3669 | if (!F->hasFnAttr(Attribute::OptimizeForSize) || |
| 3670 | // If optimizing for size, don't insert too many multiplies. This |
| 3671 | // inserts up to 5 multiplies. |
| 3672 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 3673 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3674 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3675 | // powi(x,15) generates one more multiply than it should), but this has |
| 3676 | // the benefit of being both really simple and much better than a libcall. |
| 3677 | SDValue Res; // Logically starts equal to 1.0 |
| 3678 | SDValue CurSquare = LHS; |
| 3679 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3680 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3681 | if (Res.getNode()) |
| 3682 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 3683 | else |
| 3684 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3685 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3686 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3687 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 3688 | CurSquare, CurSquare); |
| 3689 | Val >>= 1; |
| 3690 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3691 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3692 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 3693 | if (RHSC->getSExtValue() < 0) |
| 3694 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 3695 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 3696 | return Res; |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | // Otherwise, expand to a libcall. |
| 3701 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 3702 | } |
| 3703 | |
| 3704 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3705 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3706 | /// we want to emit this as a call to a named external function, return the name |
| 3707 | /// otherwise lower it and return null. |
| 3708 | const char * |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3709 | SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3710 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3711 | SDValue Res; |
| 3712 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3713 | switch (Intrinsic) { |
| 3714 | default: |
| 3715 | // By default, turn this into a target intrinsic node. |
| 3716 | visitTargetIntrinsic(I, Intrinsic); |
| 3717 | return 0; |
| 3718 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3719 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3720 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3721 | case Intrinsic::returnaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3722 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
| 3723 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3724 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3725 | case Intrinsic::frameaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3726 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
| 3727 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3728 | return 0; |
| 3729 | case Intrinsic::setjmp: |
| 3730 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3731 | case Intrinsic::longjmp: |
| 3732 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3733 | case Intrinsic::memcpy: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3734 | // Assert for address < 256 since we support only user defined address |
| 3735 | // spaces. |
| 3736 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
| 3737 | < 256 && |
| 3738 | cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace() |
| 3739 | < 256 && |
| 3740 | "Unknown address space"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3741 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3742 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3743 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3744 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3745 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
| 3746 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3747 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3748 | return 0; |
| 3749 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3750 | case Intrinsic::memset: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3751 | // Assert for address < 256 since we support only user defined address |
| 3752 | // spaces. |
| 3753 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
| 3754 | < 256 && |
| 3755 | "Unknown address space"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3756 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3757 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3758 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3759 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3760 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
| 3761 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3762 | I.getOperand(1), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3763 | return 0; |
| 3764 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3765 | case Intrinsic::memmove: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3766 | // Assert for address < 256 since we support only user defined address |
| 3767 | // spaces. |
| 3768 | assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace() |
| 3769 | < 256 && |
| 3770 | cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace() |
| 3771 | < 256 && |
| 3772 | "Unknown address space"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3773 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3774 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3775 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3776 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3777 | bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3778 | |
| 3779 | // If the source and destination are known to not be aliases, we can |
| 3780 | // lower memmove as memcpy. |
| 3781 | uint64_t Size = -1ULL; |
| 3782 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3783 | Size = C->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3784 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3785 | AliasAnalysis::NoAlias) { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3786 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
| 3787 | false, I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3788 | return 0; |
| 3789 | } |
| 3790 | |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3791 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3792 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3793 | return 0; |
| 3794 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3795 | case Intrinsic::dbg_declare: { |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3796 | // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None. |
| 3797 | // The real handling of this intrinsic is in FastISel. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3798 | if (OptLevel != CodeGenOpt::None) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3799 | // FIXME: Variable debug info is not supported here. |
| 3800 | return 0; |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3801 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Chris Lattner | bf0ca2b | 2009-12-29 09:32:19 +0000 | [diff] [blame] | 3802 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3803 | return 0; |
| 3804 | |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 3805 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3806 | Value *Address = DI.getAddress(); |
Dale Johannesen | 8ac38f2 | 2010-02-08 21:53:27 +0000 | [diff] [blame] | 3807 | if (!Address) |
| 3808 | return 0; |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3809 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 3810 | Address = BCI->getOperand(0); |
| 3811 | AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 3812 | // Don't handle byval struct arguments or VLAs, for example. |
| 3813 | if (!AI) |
| 3814 | return 0; |
Devang Patel | bd1d6a8 | 2009-09-05 00:34:14 +0000 | [diff] [blame] | 3815 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3816 | FuncInfo.StaticAllocaMap.find(AI); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3817 | if (SI == FuncInfo.StaticAllocaMap.end()) |
Devang Patel | bd1d6a8 | 2009-09-05 00:34:14 +0000 | [diff] [blame] | 3818 | return 0; // VLAs. |
| 3819 | int FI = SI->second; |
Devang Patel | 70d75ca | 2009-11-12 19:02:56 +0000 | [diff] [blame] | 3820 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3821 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 3822 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 3823 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3824 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3825 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3826 | case Intrinsic::dbg_value: { |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3827 | DbgValueInst &DI = cast<DbgValueInst>(I); |
| 3828 | if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None)) |
| 3829 | return 0; |
| 3830 | |
| 3831 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3832 | uint64_t Offset = DI.getOffset(); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3833 | Value *V = DI.getValue(); |
| 3834 | if (!V) |
| 3835 | return 0; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3836 | |
| 3837 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 3838 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 3839 | // absolute, but not relative, values are different depending on whether |
| 3840 | // debug info exists. |
| 3841 | ++SDNodeOrder; |
| 3842 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) { |
Evan Cheng | 31441b7 | 2010-03-29 20:48:30 +0000 | [diff] [blame] | 3843 | DAG.AddDbgValue(DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder)); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3844 | } else { |
| 3845 | SDValue &N = NodeMap[V]; |
Evan Cheng | 31441b7 | 2010-03-29 20:48:30 +0000 | [diff] [blame] | 3846 | if (N.getNode()) |
| 3847 | DAG.AddDbgValue(DAG.getDbgValue(Variable, N.getNode(), |
| 3848 | N.getResNo(), Offset, dl, SDNodeOrder), |
| 3849 | N.getNode()); |
| 3850 | else |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3851 | // We may expand this to cover more cases. One case where we have no |
| 3852 | // data available is an unreferenced parameter; we need this fallback. |
Evan Cheng | 31441b7 | 2010-03-29 20:48:30 +0000 | [diff] [blame] | 3853 | DAG.AddDbgValue(DAG.getDbgValue(Variable, |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3854 | UndefValue::get(V->getType()), |
Evan Cheng | 31441b7 | 2010-03-29 20:48:30 +0000 | [diff] [blame] | 3855 | Offset, dl, SDNodeOrder)); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | // Build a debug info table entry. |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3859 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
| 3860 | V = BCI->getOperand(0); |
| 3861 | AllocaInst *AI = dyn_cast<AllocaInst>(V); |
| 3862 | // Don't handle byval struct arguments or VLAs, for example. |
| 3863 | if (!AI) |
| 3864 | return 0; |
| 3865 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 3866 | FuncInfo.StaticAllocaMap.find(AI); |
| 3867 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 3868 | return 0; // VLAs. |
| 3869 | int FI = SI->second; |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 3870 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3871 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 3872 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 3873 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 3874 | return 0; |
| 3875 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3876 | case Intrinsic::eh_exception: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3877 | // Insert the EXCEPTIONADDR instruction. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 3878 | assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3879 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3880 | SDValue Ops[1]; |
| 3881 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3882 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3883 | setValue(&I, Op); |
| 3884 | DAG.setRoot(Op.getValue(1)); |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 3888 | case Intrinsic::eh_selector: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3889 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3890 | if (CurMBB->isLandingPad()) |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3891 | AddCatchInfo(I, &MMI, CurMBB); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3892 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3893 | #ifndef NDEBUG |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3894 | FuncInfo.CatchInfoLost.insert(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3895 | #endif |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3896 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 3897 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 3898 | if (Reg) CurMBB->addLiveIn(Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3899 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3900 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3901 | // Insert the EHSELECTION instruction. |
| 3902 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 3903 | SDValue Ops[2]; |
| 3904 | Ops[0] = getValue(I.getOperand(1)); |
| 3905 | Ops[1] = getRoot(); |
| 3906 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 3907 | DAG.setRoot(Op.getValue(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3908 | setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3909 | return 0; |
| 3910 | } |
| 3911 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 3912 | case Intrinsic::eh_typeid_for: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3913 | // Find the type id for the given typeinfo. |
| 3914 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
| 3915 | unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV); |
| 3916 | Res = DAG.getConstant(TypeID, MVT::i32); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3917 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3918 | return 0; |
| 3919 | } |
| 3920 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3921 | case Intrinsic::eh_return_i32: |
| 3922 | case Intrinsic::eh_return_i64: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3923 | DAG.getMachineFunction().getMMI().setCallsEHReturn(true); |
| 3924 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
| 3925 | MVT::Other, |
| 3926 | getControlRoot(), |
| 3927 | getValue(I.getOperand(1)), |
| 3928 | getValue(I.getOperand(2)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3929 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3930 | case Intrinsic::eh_unwind_init: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3931 | DAG.getMachineFunction().getMMI().setCallsUnwindInit(true); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3932 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3933 | case Intrinsic::eh_dwarf_cfa: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3934 | EVT VT = getValue(I.getOperand(1)).getValueType(); |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 3935 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl, |
| 3936 | TLI.getPointerTy()); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3937 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3938 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3939 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3940 | TLI.getPointerTy()), |
| 3941 | CfaArg); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3942 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3943 | TLI.getPointerTy(), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3944 | DAG.getConstant(0, TLI.getPointerTy())); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3945 | setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), |
| 3946 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3947 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3948 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 3949 | case Intrinsic::eh_sjlj_callsite: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3950 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 3951 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1)); |
| 3952 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3953 | assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 3954 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 3955 | MMI.setCurrentCallSite(CI->getZExtValue()); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 3956 | return 0; |
| 3957 | } |
| 3958 | |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 3959 | case Intrinsic::convertff: |
| 3960 | case Intrinsic::convertfsi: |
| 3961 | case Intrinsic::convertfui: |
| 3962 | case Intrinsic::convertsif: |
| 3963 | case Intrinsic::convertuif: |
| 3964 | case Intrinsic::convertss: |
| 3965 | case Intrinsic::convertsu: |
| 3966 | case Intrinsic::convertus: |
| 3967 | case Intrinsic::convertuu: { |
| 3968 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 3969 | switch (Intrinsic) { |
| 3970 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 3971 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 3972 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 3973 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 3974 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 3975 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 3976 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 3977 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 3978 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 3979 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3980 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3981 | Value *Op1 = I.getOperand(1); |
| 3982 | Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
| 3983 | DAG.getValueType(DestVT), |
| 3984 | DAG.getValueType(getValue(Op1).getValueType()), |
| 3985 | getValue(I.getOperand(2)), |
| 3986 | getValue(I.getOperand(3)), |
| 3987 | Code); |
| 3988 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 3989 | return 0; |
| 3990 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3991 | case Intrinsic::sqrt: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3992 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
| 3993 | getValue(I.getOperand(1)).getValueType(), |
| 3994 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3995 | return 0; |
| 3996 | case Intrinsic::powi: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3997 | setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)), |
| 3998 | getValue(I.getOperand(2)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3999 | return 0; |
| 4000 | case Intrinsic::sin: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4001 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
| 4002 | getValue(I.getOperand(1)).getValueType(), |
| 4003 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4004 | return 0; |
| 4005 | case Intrinsic::cos: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4006 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
| 4007 | getValue(I.getOperand(1)).getValueType(), |
| 4008 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4009 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4010 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4011 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4012 | return 0; |
| 4013 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4014 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4015 | return 0; |
| 4016 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4017 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4018 | return 0; |
| 4019 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4020 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4021 | return 0; |
| 4022 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4023 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4024 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4025 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4026 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4027 | return 0; |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4028 | case Intrinsic::convert_to_fp16: |
| 4029 | setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl, |
| 4030 | MVT::i16, getValue(I.getOperand(1)))); |
| 4031 | return 0; |
| 4032 | case Intrinsic::convert_from_fp16: |
| 4033 | setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl, |
| 4034 | MVT::f32, getValue(I.getOperand(1)))); |
| 4035 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4036 | case Intrinsic::pcmarker: { |
| 4037 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4038 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4039 | return 0; |
| 4040 | } |
| 4041 | case Intrinsic::readcyclecounter: { |
| 4042 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4043 | Res = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
| 4044 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4045 | &Op, 1); |
| 4046 | setValue(&I, Res); |
| 4047 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4048 | return 0; |
| 4049 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4050 | case Intrinsic::bswap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4051 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
| 4052 | getValue(I.getOperand(1)).getValueType(), |
| 4053 | getValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4054 | return 0; |
| 4055 | case Intrinsic::cttz: { |
| 4056 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4057 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4058 | setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4059 | return 0; |
| 4060 | } |
| 4061 | case Intrinsic::ctlz: { |
| 4062 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4063 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4064 | setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4065 | return 0; |
| 4066 | } |
| 4067 | case Intrinsic::ctpop: { |
| 4068 | SDValue Arg = getValue(I.getOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4069 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4070 | setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4071 | return 0; |
| 4072 | } |
| 4073 | case Intrinsic::stacksave: { |
| 4074 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4075 | Res = DAG.getNode(ISD::STACKSAVE, dl, |
| 4076 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
| 4077 | setValue(&I, Res); |
| 4078 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4079 | return 0; |
| 4080 | } |
| 4081 | case Intrinsic::stackrestore: { |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4082 | Res = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4083 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4084 | return 0; |
| 4085 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4086 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4087 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4088 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4089 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4090 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4091 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4092 | SDValue Src = getValue(I.getOperand(1)); // The guard's value. |
| 4093 | AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4094 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4095 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4096 | MFI->setStackProtectorIndex(FI); |
| 4097 | |
| 4098 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4099 | |
| 4100 | // Store the stack protector onto the stack. |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4101 | Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
| 4102 | PseudoSourceValue::getFixedStack(FI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4103 | 0, true, false, 0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4104 | setValue(&I, Res); |
| 4105 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4106 | return 0; |
| 4107 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4108 | case Intrinsic::objectsize: { |
| 4109 | // If we don't know by now, we're never going to know. |
| 4110 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2)); |
| 4111 | |
| 4112 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4113 | |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4114 | SDValue Arg = getValue(I.getOperand(0)); |
| 4115 | EVT Ty = Arg.getValueType(); |
| 4116 | |
Eric Christopher | d060b25 | 2009-12-23 02:51:48 +0000 | [diff] [blame] | 4117 | if (CI->getZExtValue() == 0) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4118 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4119 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4120 | Res = DAG.getConstant(0, Ty); |
| 4121 | |
| 4122 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4123 | return 0; |
| 4124 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4125 | case Intrinsic::var_annotation: |
| 4126 | // Discard annotate attributes |
| 4127 | return 0; |
| 4128 | |
| 4129 | case Intrinsic::init_trampoline: { |
| 4130 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 4131 | |
| 4132 | SDValue Ops[6]; |
| 4133 | Ops[0] = getRoot(); |
| 4134 | Ops[1] = getValue(I.getOperand(1)); |
| 4135 | Ops[2] = getValue(I.getOperand(2)); |
| 4136 | Ops[3] = getValue(I.getOperand(3)); |
| 4137 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 4138 | Ops[5] = DAG.getSrcValue(F); |
| 4139 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4140 | Res = DAG.getNode(ISD::TRAMPOLINE, dl, |
| 4141 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
| 4142 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4143 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4144 | setValue(&I, Res); |
| 4145 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4146 | return 0; |
| 4147 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4148 | case Intrinsic::gcroot: |
| 4149 | if (GFI) { |
| 4150 | Value *Alloca = I.getOperand(1); |
| 4151 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4152 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4153 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4154 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4155 | } |
| 4156 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4157 | case Intrinsic::gcread: |
| 4158 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4159 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4160 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4161 | case Intrinsic::flt_rounds: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4162 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4163 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4164 | case Intrinsic::trap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4165 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4166 | return 0; |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4167 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4168 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4169 | case Intrinsic::sadd_with_overflow: |
| 4170 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4171 | case Intrinsic::usub_with_overflow: |
| 4172 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4173 | case Intrinsic::ssub_with_overflow: |
| 4174 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4175 | case Intrinsic::umul_with_overflow: |
| 4176 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4177 | case Intrinsic::smul_with_overflow: |
| 4178 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4180 | case Intrinsic::prefetch: { |
| 4181 | SDValue Ops[4]; |
| 4182 | Ops[0] = getRoot(); |
| 4183 | Ops[1] = getValue(I.getOperand(1)); |
| 4184 | Ops[2] = getValue(I.getOperand(2)); |
| 4185 | Ops[3] = getValue(I.getOperand(3)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4186 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4187 | return 0; |
| 4188 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4189 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4190 | case Intrinsic::memory_barrier: { |
| 4191 | SDValue Ops[6]; |
| 4192 | Ops[0] = getRoot(); |
| 4193 | for (int x = 1; x < 6; ++x) |
| 4194 | Ops[x] = getValue(I.getOperand(x)); |
| 4195 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4196 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4197 | return 0; |
| 4198 | } |
| 4199 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4200 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4201 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4202 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4203 | getValue(I.getOperand(2)).getValueType().getSimpleVT(), |
| 4204 | Root, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4205 | getValue(I.getOperand(1)), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4206 | getValue(I.getOperand(2)), |
| 4207 | getValue(I.getOperand(3)), |
| 4208 | I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4209 | setValue(&I, L); |
| 4210 | DAG.setRoot(L.getValue(1)); |
| 4211 | return 0; |
| 4212 | } |
| 4213 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4214 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4215 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4216 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4217 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4218 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4219 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4220 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4221 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4222 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4223 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4224 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4225 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4226 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4227 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4228 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4229 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4230 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4231 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4232 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4233 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4234 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4235 | |
| 4236 | case Intrinsic::invariant_start: |
| 4237 | case Intrinsic::lifetime_start: |
| 4238 | // Discard region information. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4239 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4240 | return 0; |
| 4241 | case Intrinsic::invariant_end: |
| 4242 | case Intrinsic::lifetime_end: |
| 4243 | // Discard region information. |
| 4244 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4245 | } |
| 4246 | } |
| 4247 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4248 | /// Test if the given instruction is in a position to be optimized |
| 4249 | /// with a tail-call. This roughly means that it's in a block with |
| 4250 | /// a return and there's nothing that needs to be scheduled |
| 4251 | /// between it and the return. |
| 4252 | /// |
| 4253 | /// This function only tests target-independent requirements. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4254 | static bool |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4255 | isInTailCallPosition(CallSite CS, Attributes CalleeRetAttr, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4256 | const TargetLowering &TLI) { |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4257 | const Instruction *I = CS.getInstruction(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4258 | const BasicBlock *ExitBB = I->getParent(); |
| 4259 | const TerminatorInst *Term = ExitBB->getTerminator(); |
| 4260 | const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); |
| 4261 | const Function *F = ExitBB->getParent(); |
| 4262 | |
Dan Gohman | c2e93b2 | 2010-02-08 20:34:14 +0000 | [diff] [blame] | 4263 | // The block must end in a return statement or unreachable. |
| 4264 | // |
| 4265 | // FIXME: Decline tailcall if it's not guaranteed and if the block ends in |
| 4266 | // an unreachable, for now. The way tailcall optimization is currently |
| 4267 | // implemented means it will add an epilogue followed by a jump. That is |
| 4268 | // not profitable. Also, if the callee is a special function (e.g. |
| 4269 | // longjmp on x86), it can end up causing miscompilation that has not |
| 4270 | // been fully understood. |
| 4271 | if (!Ret && |
| 4272 | (!GuaranteedTailCallOpt || !isa<UnreachableInst>(Term))) return false; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4273 | |
| 4274 | // If I will have a chain, make sure no other instruction that will have a |
| 4275 | // chain interposes between I and the return. |
| 4276 | if (I->mayHaveSideEffects() || I->mayReadFromMemory() || |
| 4277 | !I->isSafeToSpeculativelyExecute()) |
| 4278 | for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; |
| 4279 | --BBI) { |
| 4280 | if (&*BBI == I) |
| 4281 | break; |
Devang Patel | 1ac2429 | 2010-03-18 16:41:16 +0000 | [diff] [blame] | 4282 | // Debug info intrinsics do not get in the way of tail call optimization. |
Devang Patel | c3188ce | 2010-03-17 23:52:37 +0000 | [diff] [blame] | 4283 | if (isa<DbgInfoIntrinsic>(BBI)) |
| 4284 | continue; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4285 | if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || |
| 4286 | !BBI->isSafeToSpeculativelyExecute()) |
| 4287 | return false; |
| 4288 | } |
| 4289 | |
| 4290 | // If the block ends with a void return or unreachable, it doesn't matter |
| 4291 | // what the call's return type is. |
| 4292 | if (!Ret || Ret->getNumOperands() == 0) return true; |
| 4293 | |
Dan Gohman | ed9bab3 | 2009-11-14 02:06:30 +0000 | [diff] [blame] | 4294 | // If the return value is undef, it doesn't matter what the call's |
| 4295 | // return type is. |
| 4296 | if (isa<UndefValue>(Ret->getOperand(0))) return true; |
| 4297 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4298 | // Conservatively require the attributes of the call to match those of |
Dan Gohman | 01205a8 | 2009-11-13 18:49:38 +0000 | [diff] [blame] | 4299 | // the return. Ignore noalias because it doesn't affect the call sequence. |
| 4300 | unsigned CallerRetAttr = F->getAttributes().getRetAttributes(); |
| 4301 | if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4302 | return false; |
| 4303 | |
Evan Cheng | 6fdce65 | 2010-02-04 19:07:06 +0000 | [diff] [blame] | 4304 | // It's not safe to eliminate the sign / zero extension of the return value. |
Evan Cheng | 446bc10 | 2010-02-04 02:45:02 +0000 | [diff] [blame] | 4305 | if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt)) |
| 4306 | return false; |
| 4307 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4308 | // Otherwise, make sure the unmodified return value of I is the return value. |
| 4309 | for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ; |
| 4310 | U = dyn_cast<Instruction>(U->getOperand(0))) { |
| 4311 | if (!U) |
| 4312 | return false; |
| 4313 | if (!U->hasOneUse()) |
| 4314 | return false; |
| 4315 | if (U == I) |
| 4316 | break; |
| 4317 | // Check for a truly no-op truncate. |
| 4318 | if (isa<TruncInst>(U) && |
| 4319 | TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType())) |
| 4320 | continue; |
| 4321 | // Check for a truly no-op bitcast. |
| 4322 | if (isa<BitCastInst>(U) && |
| 4323 | (U->getOperand(0)->getType() == U->getType() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4324 | (U->getOperand(0)->getType()->isPointerTy() && |
| 4325 | U->getType()->isPointerTy()))) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4326 | continue; |
| 4327 | // Otherwise it's not a true no-op. |
| 4328 | return false; |
| 4329 | } |
| 4330 | |
| 4331 | return true; |
| 4332 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4333 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4334 | void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee, |
| 4335 | bool isTailCall, |
| 4336 | MachineBasicBlock *LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4337 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4338 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4339 | const Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4340 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 4341 | MCSymbol *BeginLabel = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4342 | |
| 4343 | TargetLowering::ArgListTy Args; |
| 4344 | TargetLowering::ArgListEntry Entry; |
| 4345 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4346 | |
| 4347 | // Check whether the function can return without sret-demotion. |
| 4348 | SmallVector<EVT, 4> OutVTs; |
| 4349 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 4350 | SmallVector<uint64_t, 4> Offsets; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4351 | getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(), |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4352 | OutVTs, OutsFlags, TLI, &Offsets); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4353 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4354 | bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4355 | FTy->isVarArg(), OutVTs, OutsFlags, DAG); |
| 4356 | |
| 4357 | SDValue DemoteStackSlot; |
| 4358 | |
| 4359 | if (!CanLowerReturn) { |
| 4360 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize( |
| 4361 | FTy->getReturnType()); |
| 4362 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment( |
| 4363 | FTy->getReturnType()); |
| 4364 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 4365 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4366 | const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
| 4367 | |
| 4368 | DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4369 | Entry.Node = DemoteStackSlot; |
| 4370 | Entry.Ty = StackSlotPtrType; |
| 4371 | Entry.isSExt = false; |
| 4372 | Entry.isZExt = false; |
| 4373 | Entry.isInReg = false; |
| 4374 | Entry.isSRet = true; |
| 4375 | Entry.isNest = false; |
| 4376 | Entry.isByVal = false; |
| 4377 | Entry.Alignment = Align; |
| 4378 | Args.push_back(Entry); |
| 4379 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 4380 | } |
| 4381 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4382 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4383 | i != e; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4384 | SDValue ArgNode = getValue(*i); |
| 4385 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4386 | |
| 4387 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4388 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4389 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4390 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4391 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4392 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4393 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4394 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4395 | Args.push_back(Entry); |
| 4396 | } |
| 4397 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4398 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4399 | // Insert a label before the invoke call to mark the try range. This can be |
| 4400 | // used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4401 | BeginLabel = MMI.getContext().CreateTempSymbol(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 4402 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4403 | // For SjLj, keep track of which landing pads go with which invokes |
| 4404 | // so as to maintain the ordering of pads in the LSDA. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4405 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4406 | if (CallSiteIndex) { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4407 | MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4408 | // Now that the call site is handled, stop tracking it. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4409 | MMI.setCurrentCallSite(0); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4412 | // Both PendingLoads and PendingExports must be flushed here; |
| 4413 | // this call might not return. |
| 4414 | (void)getRoot(); |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 4415 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4418 | // Check if target-independent constraints permit a tail call here. |
| 4419 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
| 4420 | if (isTailCall && |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4421 | !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4422 | isTailCall = false; |
| 4423 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4424 | std::pair<SDValue,SDValue> Result = |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4425 | TLI.LowerCallTo(getRoot(), RetTy, |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4426 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4427 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 4428 | CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4429 | CS.getCallingConv(), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4430 | isTailCall, |
| 4431 | !CS.getInstruction()->use_empty(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4432 | Callee, Args, DAG, getCurDebugLoc()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4433 | assert((isTailCall || Result.second.getNode()) && |
| 4434 | "Non-null chain expected with non-tail call!"); |
| 4435 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 4436 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4437 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4438 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4439 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4440 | // The instruction result is the result of loading from the |
| 4441 | // hidden sret parameter. |
| 4442 | SmallVector<EVT, 1> PVTs; |
| 4443 | const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
| 4444 | |
| 4445 | ComputeValueVTs(TLI, PtrRetTy, PVTs); |
| 4446 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 4447 | EVT PtrVT = PVTs[0]; |
| 4448 | unsigned NumValues = OutVTs.size(); |
| 4449 | SmallVector<SDValue, 4> Values(NumValues); |
| 4450 | SmallVector<SDValue, 4> Chains(NumValues); |
| 4451 | |
| 4452 | for (unsigned i = 0; i < NumValues; ++i) { |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4453 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, |
| 4454 | DemoteStackSlot, |
| 4455 | DAG.getConstant(Offsets[i], PtrVT)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4456 | SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4457 | Add, NULL, Offsets[i], false, false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4458 | Values[i] = L; |
| 4459 | Chains[i] = L.getValue(1); |
| 4460 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4461 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4462 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 4463 | MVT::Other, &Chains[0], NumValues); |
| 4464 | PendingLoads.push_back(Chain); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4465 | |
| 4466 | // Collect the legal value parts into potentially illegal values |
| 4467 | // that correspond to the original function's return values. |
| 4468 | SmallVector<EVT, 4> RetTys; |
| 4469 | RetTy = FTy->getReturnType(); |
| 4470 | ComputeValueVTs(TLI, RetTy, RetTys); |
| 4471 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 4472 | SmallVector<SDValue, 4> ReturnValues; |
| 4473 | unsigned CurReg = 0; |
| 4474 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 4475 | EVT VT = RetTys[I]; |
| 4476 | EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT); |
| 4477 | unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT); |
| 4478 | |
| 4479 | SDValue ReturnValue = |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4480 | getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs, |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4481 | RegisterVT, VT, AssertOp); |
| 4482 | ReturnValues.push_back(ReturnValue); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4483 | CurReg += NumRegs; |
| 4484 | } |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4485 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4486 | setValue(CS.getInstruction(), |
| 4487 | DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 4488 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 4489 | &ReturnValues[0], ReturnValues.size())); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4490 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4491 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4492 | |
| 4493 | // As a special case, a null chain means that a tail call has been emitted and |
| 4494 | // the DAG root is already updated. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4495 | if (Result.second.getNode()) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4496 | DAG.setRoot(Result.second); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4497 | else |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4498 | HasTailCall = true; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4499 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4500 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4501 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4502 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4503 | MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol(); |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 4504 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4505 | |
| 4506 | // Inform MachineModuleInfo of range. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4507 | MMI.addInvoke(LandingPad, BeginLabel, EndLabel); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4508 | } |
| 4509 | } |
| 4510 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4511 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 4512 | /// value is equal or not-equal to zero. |
| 4513 | static bool IsOnlyUsedInZeroEqualityComparison(Value *V) { |
| 4514 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); |
| 4515 | UI != E; ++UI) { |
| 4516 | if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
| 4517 | if (IC->isEquality()) |
| 4518 | if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
| 4519 | if (C->isNullValue()) |
| 4520 | continue; |
| 4521 | // Unknown instruction. |
| 4522 | return false; |
| 4523 | } |
| 4524 | return true; |
| 4525 | } |
| 4526 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4527 | static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4528 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4529 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4530 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 4531 | // input is from a string literal. |
| 4532 | if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
| 4533 | // Cast pointer to the type we really want to load. |
| 4534 | LoadInput = ConstantExpr::getBitCast(LoadInput, |
| 4535 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4536 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4537 | if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD)) |
| 4538 | return Builder.getValue(LoadCst); |
| 4539 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4540 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4541 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 4542 | // still constant memory, the input chain can be the entry node. |
| 4543 | SDValue Root; |
| 4544 | bool ConstantMemory = false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4545 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4546 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 4547 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 4548 | Root = Builder.DAG.getEntryNode(); |
| 4549 | ConstantMemory = true; |
| 4550 | } else { |
| 4551 | // Do not serialize non-volatile loads against each other. |
| 4552 | Root = Builder.DAG.getRoot(); |
| 4553 | } |
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 | SDValue Ptr = Builder.getValue(PtrVal); |
| 4556 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root, |
| 4557 | Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4558 | false /*volatile*/, |
| 4559 | false /*nontemporal*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4560 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4561 | if (!ConstantMemory) |
| 4562 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 4563 | return LoadVal; |
| 4564 | } |
| 4565 | |
| 4566 | |
| 4567 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 4568 | /// If so, return true and lower it, otherwise return false and it will be |
| 4569 | /// lowered like a normal call. |
| 4570 | bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) { |
| 4571 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
| 4572 | if (I.getNumOperands() != 4) |
| 4573 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4574 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4575 | Value *LHS = I.getOperand(1), *RHS = I.getOperand(2); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4576 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
| 4577 | !I.getOperand(3)->getType()->isIntegerTy() || |
| 4578 | !I.getType()->isIntegerTy()) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4579 | return false; |
| 4580 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4581 | ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4582 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4583 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 4584 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4585 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 4586 | bool ActuallyDoIt = true; |
| 4587 | MVT LoadVT; |
| 4588 | const Type *LoadTy; |
| 4589 | switch (Size->getZExtValue()) { |
| 4590 | default: |
| 4591 | LoadVT = MVT::Other; |
| 4592 | LoadTy = 0; |
| 4593 | ActuallyDoIt = false; |
| 4594 | break; |
| 4595 | case 2: |
| 4596 | LoadVT = MVT::i16; |
| 4597 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 4598 | break; |
| 4599 | case 4: |
| 4600 | LoadVT = MVT::i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4601 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4602 | break; |
| 4603 | case 8: |
| 4604 | LoadVT = MVT::i64; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4605 | LoadTy = Type::getInt64Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4606 | break; |
| 4607 | /* |
| 4608 | case 16: |
| 4609 | LoadVT = MVT::v4i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4610 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4611 | LoadTy = VectorType::get(LoadTy, 4); |
| 4612 | break; |
| 4613 | */ |
| 4614 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4615 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4616 | // This turns into unaligned loads. We only do this if the target natively |
| 4617 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 4618 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4619 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4620 | // Require that we can find a legal MVT, and only do this if the target |
| 4621 | // supports unaligned loads of that type. Expanding into byte loads would |
| 4622 | // bloat the code. |
| 4623 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 4624 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 4625 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
| 4626 | if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT)) |
| 4627 | ActuallyDoIt = false; |
| 4628 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4629 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4630 | if (ActuallyDoIt) { |
| 4631 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 4632 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4633 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4634 | SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal, |
| 4635 | ISD::SETNE); |
| 4636 | EVT CallVT = TLI.getValueType(I.getType(), true); |
| 4637 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT)); |
| 4638 | return true; |
| 4639 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4640 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4641 | |
| 4642 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4643 | return false; |
| 4644 | } |
| 4645 | |
| 4646 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4647 | void SelectionDAGBuilder::visitCall(CallInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4648 | const char *RenameFn = 0; |
| 4649 | if (Function *F = I.getCalledFunction()) { |
| 4650 | if (F->isDeclaration()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4651 | const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo(); |
| 4652 | if (II) { |
| 4653 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4654 | RenameFn = visitIntrinsicCall(I, IID); |
| 4655 | if (!RenameFn) |
| 4656 | return; |
| 4657 | } |
| 4658 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4659 | if (unsigned IID = F->getIntrinsicID()) { |
| 4660 | RenameFn = visitIntrinsicCall(I, IID); |
| 4661 | if (!RenameFn) |
| 4662 | return; |
| 4663 | } |
| 4664 | } |
| 4665 | |
| 4666 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4667 | // can't be a library call. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4668 | if (!F->hasLocalLinkage() && F->hasName()) { |
| 4669 | StringRef Name = F->getName(); |
Duncan Sands | d2c817e | 2010-03-14 21:08:40 +0000 | [diff] [blame] | 4670 | if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4671 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4672 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4673 | I.getType() == I.getOperand(1)->getType() && |
| 4674 | I.getType() == I.getOperand(2)->getType()) { |
| 4675 | SDValue LHS = getValue(I.getOperand(1)); |
| 4676 | SDValue RHS = getValue(I.getOperand(2)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4677 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
| 4678 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4679 | return; |
| 4680 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4681 | } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4682 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4683 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4684 | I.getType() == I.getOperand(1)->getType()) { |
| 4685 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4686 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
| 4687 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4688 | return; |
| 4689 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4690 | } else if (Name == "sin" || Name == "sinf" || Name == "sinl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4691 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4692 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4693 | I.getType() == I.getOperand(1)->getType() && |
| 4694 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4695 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4696 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
| 4697 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4698 | return; |
| 4699 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4700 | } else if (Name == "cos" || Name == "cosf" || Name == "cosl") { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4701 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4702 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4703 | I.getType() == I.getOperand(1)->getType() && |
| 4704 | I.onlyReadsMemory()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4705 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4706 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
| 4707 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4708 | return; |
| 4709 | } |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4710 | } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") { |
| 4711 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4712 | I.getOperand(1)->getType()->isFloatingPointTy() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4713 | I.getType() == I.getOperand(1)->getType() && |
| 4714 | I.onlyReadsMemory()) { |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4715 | SDValue Tmp = getValue(I.getOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4716 | setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(), |
| 4717 | Tmp.getValueType(), Tmp)); |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4718 | return; |
| 4719 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4720 | } else if (Name == "memcmp") { |
| 4721 | if (visitMemCmpCall(I)) |
| 4722 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4723 | } |
| 4724 | } |
| 4725 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4726 | visitInlineAsm(&I); |
| 4727 | return; |
| 4728 | } |
| 4729 | |
| 4730 | SDValue Callee; |
| 4731 | if (!RenameFn) |
| 4732 | Callee = getValue(I.getOperand(0)); |
| 4733 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4734 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4735 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4736 | // Check if we can potentially perform a tail call. More detailed checking is |
| 4737 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 4738 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4741 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4742 | /// this value and returns the result as a ValueVT value. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4743 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4744 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4745 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4746 | SDValue &Chain, SDValue *Flag) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4747 | // Assemble the legal parts into the final values. |
| 4748 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4749 | SmallVector<SDValue, 8> Parts; |
| 4750 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4751 | // Copy the legal parts from the registers. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4752 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4753 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4754 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4755 | |
| 4756 | Parts.resize(NumRegs); |
| 4757 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4758 | SDValue P; |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4759 | if (Flag == 0) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4760 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4761 | } else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4762 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4763 | *Flag = P.getValue(2); |
| 4764 | } |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4765 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4766 | Chain = P.getValue(1); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4767 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4768 | // If the source register was virtual and if we know something about it, |
| 4769 | // add an assert node. |
| 4770 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4771 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4772 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4773 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4774 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4775 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4776 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4777 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4778 | unsigned NumSignBits = LOI.NumSignBits; |
| 4779 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4780 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4781 | // FIXME: We capture more information than the dag can represent. For |
| 4782 | // now, just use the tightest assertzext/assertsext possible. |
| 4783 | bool isSExt = true; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4784 | EVT FromVT(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4785 | if (NumSignBits == RegSize) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4786 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4787 | else if (NumZeroBits >= RegSize-1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4788 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4789 | else if (NumSignBits > RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4790 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4791 | else if (NumZeroBits >= RegSize-8) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4792 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4793 | else if (NumSignBits > RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4794 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4795 | else if (NumZeroBits >= RegSize-16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4796 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4797 | else if (NumSignBits > RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4798 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
Dan Gohman | 07c26ee | 2009-03-31 01:38:29 +0000 | [diff] [blame] | 4799 | else if (NumZeroBits >= RegSize-32) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4800 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4801 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4802 | if (FromVT != MVT::Other) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4803 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4804 | RegisterVT, P, DAG.getValueType(FromVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4805 | } |
| 4806 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4807 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4808 | Parts[i] = P; |
| 4809 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4810 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4811 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4812 | NumRegs, RegisterVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4813 | Part += NumRegs; |
| 4814 | Parts.clear(); |
| 4815 | } |
| 4816 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4817 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 4818 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4819 | &Values[0], ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4823 | /// specified value into the registers specified by this object. This uses |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4824 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4825 | /// If the Flag pointer is NULL, no flag is used. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4826 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4827 | SDValue &Chain, SDValue *Flag) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4828 | // Get the list of the values's legal parts. |
| 4829 | unsigned NumRegs = Regs.size(); |
| 4830 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4831 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4832 | EVT ValueVT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4833 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4834 | EVT RegisterVT = RegVTs[Value]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4835 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4836 | getCopyToParts(DAG, dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 4837 | Val.getValue(Val.getResNo() + Value), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4838 | &Parts[Part], NumParts, RegisterVT); |
| 4839 | Part += NumParts; |
| 4840 | } |
| 4841 | |
| 4842 | // Copy the parts into the registers. |
| 4843 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4844 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4845 | SDValue Part; |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4846 | if (Flag == 0) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4847 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4848 | } else { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 4849 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4850 | *Flag = Part.getValue(1); |
| 4851 | } |
Bill Wendling | ec72e32 | 2009-12-22 01:11:43 +0000 | [diff] [blame] | 4852 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4853 | Chains[i] = Part.getValue(0); |
| 4854 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4855 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4856 | if (NumRegs == 1 || Flag) |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4857 | // 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] | 4858 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4859 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4860 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4861 | // user as well as a successor (the TF operands are flagged to the user). |
| 4862 | // c1, f1 = CopyToReg |
| 4863 | // c2, f2 = CopyToReg |
| 4864 | // c3 = TokenFactor c1, c2 |
| 4865 | // ... |
| 4866 | // = op c3, ..., f2 |
| 4867 | Chain = Chains[NumRegs-1]; |
| 4868 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4869 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4873 | /// operand list. This adds the code marker and includes the number of |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4874 | /// values added into it. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 4875 | void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching, |
| 4876 | unsigned MatchingIdx, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4877 | SelectionDAG &DAG, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4878 | std::vector<SDValue> &Ops) const { |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 4879 | unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size()); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 4880 | if (HasMatching) |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 4881 | Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx); |
Dale Johannesen | 9949933 | 2009-12-23 07:32:51 +0000 | [diff] [blame] | 4882 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 4883 | Ops.push_back(Res); |
| 4884 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4885 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4886 | unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4887 | EVT RegisterVT = RegVTs[Value]; |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4888 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4889 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4890 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 4891 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4895 | /// isAllocatableRegister - If the specified register is safe to allocate, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4896 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4897 | /// register class for the register. Otherwise, return null. |
| 4898 | static const TargetRegisterClass * |
| 4899 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4900 | const TargetLowering &TLI, |
| 4901 | const TargetRegisterInfo *TRI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4902 | EVT FoundVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4903 | const TargetRegisterClass *FoundRC = 0; |
| 4904 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4905 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4906 | EVT ThisVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4907 | |
| 4908 | const TargetRegisterClass *RC = *RCI; |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 4909 | // 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] | 4910 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4911 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4912 | I != E; ++I) { |
| 4913 | if (TLI.isTypeLegal(*I)) { |
| 4914 | // If we have already found this register in a different register class, |
| 4915 | // choose the one with the largest VT specified. For example, on |
| 4916 | // PowerPC, we favor f64 register classes over f32. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4917 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4918 | ThisVT = *I; |
| 4919 | break; |
| 4920 | } |
| 4921 | } |
| 4922 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4923 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4924 | if (ThisVT == MVT::Other) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4925 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4926 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4927 | // frame pointer in functions that need it (due to them not being taken |
| 4928 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4929 | // yet). This is a slight code pessimization, but should still work. |
| 4930 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4931 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4932 | if (*I == Reg) { |
| 4933 | // We found a matching register class. Keep looking at others in case |
| 4934 | // we find one with larger registers that this physreg is also in. |
| 4935 | FoundRC = RC; |
| 4936 | FoundVT = ThisVT; |
| 4937 | break; |
| 4938 | } |
| 4939 | } |
| 4940 | return FoundRC; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4941 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4942 | |
| 4943 | |
| 4944 | namespace llvm { |
| 4945 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4946 | /// lowering. |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4947 | class VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4948 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4949 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4950 | /// CallOperand - If this is the result output operand or a clobber |
| 4951 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4952 | /// This gets modified as the asm is processed. |
| 4953 | SDValue CallOperand; |
| 4954 | |
| 4955 | /// AssignedRegs - If this is a register or register class operand, this |
| 4956 | /// contains the set of register corresponding to the operand. |
| 4957 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4958 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4959 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4960 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4961 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4962 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4963 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4964 | /// busy in OutputRegs/InputRegs. |
| 4965 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4966 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4967 | std::set<unsigned> &InputRegs, |
| 4968 | const TargetRegisterInfo &TRI) const { |
| 4969 | if (isOutReg) { |
| 4970 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4971 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4972 | } |
| 4973 | if (isInReg) { |
| 4974 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4975 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4976 | } |
| 4977 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4978 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4979 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4980 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4981 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4982 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4983 | const TargetLowering &TLI, |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4984 | const TargetData *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4985 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4986 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4987 | if (isa<BasicBlock>(CallOperandVal)) |
| 4988 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4989 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4990 | const llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4991 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4992 | // If this is an indirect operand, the operand is a pointer to the |
| 4993 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 4994 | if (isIndirect) { |
| 4995 | const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
| 4996 | if (!PtrTy) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 4997 | report_fatal_error("Indirect operand for inline asm not a pointer!"); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 4998 | OpTy = PtrTy->getElementType(); |
| 4999 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5000 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5001 | // If OpTy is not a single value, it may be a struct/union that we |
| 5002 | // can tile with integers. |
| 5003 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 5004 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 5005 | switch (BitSize) { |
| 5006 | default: break; |
| 5007 | case 1: |
| 5008 | case 8: |
| 5009 | case 16: |
| 5010 | case 32: |
| 5011 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 5012 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5013 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5014 | break; |
| 5015 | } |
| 5016 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5017 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5018 | return TLI.getValueType(OpTy, true); |
| 5019 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5020 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5021 | private: |
| 5022 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 5023 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5024 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5025 | const TargetRegisterInfo &TRI) { |
| 5026 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 5027 | Regs.insert(Reg); |
| 5028 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 5029 | for (; *Aliases; ++Aliases) |
| 5030 | Regs.insert(*Aliases); |
| 5031 | } |
| 5032 | }; |
| 5033 | } // end llvm namespace. |
| 5034 | |
| 5035 | |
| 5036 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 5037 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 5038 | /// register allocator to handle the assignment process. However, if the asm |
| 5039 | /// 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] | 5040 | /// allocation. This produces generally horrible, but correct, code. |
| 5041 | /// |
| 5042 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5043 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 5044 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5045 | void SelectionDAGBuilder:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5046 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5047 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5048 | std::set<unsigned> &InputRegs) { |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 5049 | LLVMContext &Context = FuncInfo.Fn->getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5050 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5051 | // Compute whether this value requires an input register, an output register, |
| 5052 | // or both. |
| 5053 | bool isOutReg = false; |
| 5054 | bool isInReg = false; |
| 5055 | switch (OpInfo.Type) { |
| 5056 | case InlineAsm::isOutput: |
| 5057 | isOutReg = true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5058 | |
| 5059 | // 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] | 5060 | // the input register so no other inputs allocate to it. |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5061 | isInReg = OpInfo.hasMatchingInput(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5062 | break; |
| 5063 | case InlineAsm::isInput: |
| 5064 | isInReg = true; |
| 5065 | isOutReg = false; |
| 5066 | break; |
| 5067 | case InlineAsm::isClobber: |
| 5068 | isOutReg = true; |
| 5069 | isInReg = true; |
| 5070 | break; |
| 5071 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5072 | |
| 5073 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5074 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5075 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5076 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5077 | // If this is a constraint for a single physreg, or a constraint for a |
| 5078 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5079 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5080 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5081 | OpInfo.ConstraintVT); |
| 5082 | |
| 5083 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5084 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5085 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5086 | // cast of the input value. More generally, handle any case where the input |
| 5087 | // value disagrees with the register class we plan to stick this in. |
| 5088 | if (OpInfo.Type == InlineAsm::isInput && |
| 5089 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5090 | // 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] | 5091 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5092 | // vector types). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5093 | EVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5094 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5095 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5096 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5097 | OpInfo.ConstraintVT = RegVT; |
| 5098 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5099 | // If the input is a FP value and we want it in FP registers, do a |
| 5100 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5101 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5102 | // machine. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5103 | RegVT = EVT::getIntegerVT(Context, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5104 | OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5105 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5106 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5107 | OpInfo.ConstraintVT = RegVT; |
| 5108 | } |
| 5109 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5110 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5111 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5112 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5113 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5114 | EVT RegVT; |
| 5115 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5116 | |
| 5117 | // If this is a constraint for a specific physical register, like {r17}, |
| 5118 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5119 | if (unsigned AssignedReg = PhysReg.first) { |
| 5120 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5121 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5122 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5123 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5124 | // Get the actual register value type. This is important, because the user |
| 5125 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5126 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5127 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5128 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5129 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5130 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5131 | |
| 5132 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5133 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5134 | TargetRegisterClass::iterator I = RC->begin(); |
| 5135 | for (; *I != AssignedReg; ++I) |
| 5136 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5137 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5138 | // Already added the first reg. |
| 5139 | --NumRegs; ++I; |
| 5140 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5141 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5142 | Regs.push_back(*I); |
| 5143 | } |
| 5144 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5145 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5146 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5147 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5148 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5149 | return; |
| 5150 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5151 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5152 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5153 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5154 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5155 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5156 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5157 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5158 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5159 | // Create the appropriate number of virtual registers. |
| 5160 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5161 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5162 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5163 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5164 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 5165 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5166 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5167 | |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5168 | // This is a reference to a register class that doesn't directly correspond |
| 5169 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5170 | // registers from the class. |
| 5171 | std::vector<unsigned> RegClassRegs |
| 5172 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5173 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5174 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5175 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5176 | unsigned NumAllocated = 0; |
| 5177 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5178 | unsigned Reg = RegClassRegs[i]; |
| 5179 | // See if this register is available. |
| 5180 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5181 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5182 | // Make sure we find consecutive registers. |
| 5183 | NumAllocated = 0; |
| 5184 | continue; |
| 5185 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5186 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5187 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5188 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5189 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5190 | if (!RC) { // Couldn't allocate this register. |
| 5191 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5192 | NumAllocated = 0; |
| 5193 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5194 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5195 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5196 | // Okay, this register is good, we can use it. |
| 5197 | ++NumAllocated; |
| 5198 | |
| 5199 | // If we allocated enough consecutive registers, succeed. |
| 5200 | if (NumAllocated == NumRegs) { |
| 5201 | unsigned RegStart = (i-NumAllocated)+1; |
| 5202 | unsigned RegEnd = i+1; |
| 5203 | // Mark all of the allocated registers used. |
| 5204 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5205 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5206 | |
| 5207 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5208 | OpInfo.ConstraintVT); |
| 5209 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5210 | return; |
| 5211 | } |
| 5212 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5213 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5214 | // Otherwise, we couldn't allocate enough registers for this. |
| 5215 | } |
| 5216 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5217 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 5218 | /// processed uses a memory 'm' constraint. |
| 5219 | static bool |
| 5220 | hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 5221 | const TargetLowering &TLI) { |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5222 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 5223 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 5224 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 5225 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 5226 | if (CType == TargetLowering::C_Memory) |
| 5227 | return true; |
| 5228 | } |
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 | // Indirect operand accesses access memory. |
| 5231 | if (CI.isIndirect) |
| 5232 | return true; |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | return false; |
| 5236 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5237 | |
| 5238 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5239 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5240 | void SelectionDAGBuilder::visitInlineAsm(CallSite CS) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5241 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 5242 | |
| 5243 | /// ConstraintOperands - Information about all of the constraints. |
| 5244 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5245 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5246 | std::set<unsigned> OutputRegs, InputRegs; |
| 5247 | |
| 5248 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5249 | // ConstraintOperands list. |
| 5250 | std::vector<InlineAsm::ConstraintInfo> |
| 5251 | ConstraintInfos = IA->ParseConstraints(); |
| 5252 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5253 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5254 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5255 | SDValue Chain, Flag; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5256 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5257 | // We won't need to flush pending loads if this asm doesn't touch |
| 5258 | // memory and is nonvolatile. |
| 5259 | if (hasMemory || IA->hasSideEffects()) |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5260 | Chain = getRoot(); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5261 | else |
| 5262 | Chain = DAG.getRoot(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5263 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5264 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5265 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5266 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5267 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5268 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5269 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5270 | EVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5271 | |
| 5272 | // Compute the value type for each operand. |
| 5273 | switch (OpInfo.Type) { |
| 5274 | case InlineAsm::isOutput: |
| 5275 | // Indirect outputs just consume an argument. |
| 5276 | if (OpInfo.isIndirect) { |
| 5277 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5278 | break; |
| 5279 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5280 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5281 | // The return value of the call is this value. As such, there is no |
| 5282 | // corresponding argument. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5283 | assert(!CS.getType()->isVoidTy() && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5284 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5285 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5286 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5287 | } else { |
| 5288 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5289 | OpVT = TLI.getValueType(CS.getType()); |
| 5290 | } |
| 5291 | ++ResNo; |
| 5292 | break; |
| 5293 | case InlineAsm::isInput: |
| 5294 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 5295 | break; |
| 5296 | case InlineAsm::isClobber: |
| 5297 | // Nothing to do. |
| 5298 | break; |
| 5299 | } |
| 5300 | |
| 5301 | // If this is an input or an indirect output, process the call argument. |
| 5302 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5303 | if (OpInfo.CallOperandVal) { |
Dale Johannesen | 5339c55 | 2009-07-20 23:27:39 +0000 | [diff] [blame] | 5304 | // Strip bitcasts, if any. This mostly comes up for functions. |
Dale Johannesen | 7671124 | 2009-08-06 22:45:51 +0000 | [diff] [blame] | 5305 | OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts(); |
| 5306 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5307 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5308 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5309 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5310 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5311 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5312 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5313 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5314 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5315 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5316 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5317 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5318 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5319 | // Second pass over the constraints: compute which constraint option to use |
| 5320 | // and assign registers to constraints that want a specific physreg. |
| 5321 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5322 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5323 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5324 | // 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] | 5325 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5326 | // other is floating point, or their sizes are different, flag it as an |
| 5327 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5328 | if (OpInfo.hasMatchingInput()) { |
| 5329 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
Chris Lattner | 87d677c | 2010-04-07 23:50:38 +0000 | [diff] [blame] | 5330 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5331 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5332 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5333 | Input.ConstraintVT.isInteger()) || |
| 5334 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5335 | Input.ConstraintVT.getSizeInBits())) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5336 | report_fatal_error("Unsupported asm: input constraint" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5337 | " with a matching output constraint of incompatible" |
| 5338 | " type!"); |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5339 | } |
| 5340 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5341 | } |
| 5342 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5343 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5344 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5345 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5346 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5347 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5348 | // need to to provide an address for the memory input. |
| 5349 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5350 | !OpInfo.isIndirect) { |
| 5351 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5352 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5353 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5354 | // Memory operands really want the address of the value. If we don't have |
| 5355 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5356 | // it to a stack slot. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5357 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5358 | // If the operand is a float, integer, or vector constant, spill to a |
| 5359 | // constant pool entry to get its address. |
| 5360 | Value *OpVal = OpInfo.CallOperandVal; |
| 5361 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5362 | isa<ConstantVector>(OpVal)) { |
| 5363 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5364 | TLI.getPointerTy()); |
| 5365 | } else { |
| 5366 | // Otherwise, create a stack slot and emit a store to it before the |
| 5367 | // asm. |
| 5368 | const Type *Ty = OpVal->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5369 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5370 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5371 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 5372 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5373 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5374 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5375 | OpInfo.CallOperand, StackSlot, NULL, 0, |
| 5376 | false, false, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5377 | OpInfo.CallOperand = StackSlot; |
| 5378 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5379 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5380 | // There is no longer a Value* corresponding to this operand. |
| 5381 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5382 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5383 | // It is now an indirect operand. |
| 5384 | OpInfo.isIndirect = true; |
| 5385 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5386 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5387 | // If this constraint is for a specific register, allocate it before |
| 5388 | // anything else. |
| 5389 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5390 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5391 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5392 | |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5393 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5394 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5395 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5396 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5397 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5398 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5399 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5400 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5401 | // to be. |
| 5402 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5403 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5406 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5407 | std::vector<SDValue> AsmNodeOperands; |
| 5408 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5409 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 5410 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 5411 | TLI.getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5412 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5413 | // If we have a !srcloc metadata node associated with it, we want to attach |
| 5414 | // this to the ultimately generated inline asm machineinstr. To do this, we |
| 5415 | // pass in the third operand as this (potentially null) inline asm MDNode. |
| 5416 | const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc"); |
| 5417 | AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5418 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5419 | // Loop over all of the inputs, copying the operand values into the |
| 5420 | // appropriate registers and processing the output regs. |
| 5421 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5422 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5423 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5424 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5425 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5426 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5427 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5428 | |
| 5429 | switch (OpInfo.Type) { |
| 5430 | case InlineAsm::isOutput: { |
| 5431 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5432 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5433 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5434 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5435 | |
| 5436 | // Add information to the INLINEASM node to know about this output. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5437 | unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
| 5438 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5439 | TLI.getPointerTy())); |
| 5440 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5441 | break; |
| 5442 | } |
| 5443 | |
| 5444 | // Otherwise, this is a register or register class output. |
| 5445 | |
| 5446 | // Copy the output from the appropriate register. Find a register that |
| 5447 | // we can use. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5448 | if (OpInfo.AssignedRegs.Regs.empty()) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5449 | report_fatal_error("Couldn't allocate output reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5450 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5451 | |
| 5452 | // If this is an indirect operand, store through the pointer after the |
| 5453 | // asm. |
| 5454 | if (OpInfo.isIndirect) { |
| 5455 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5456 | OpInfo.CallOperandVal)); |
| 5457 | } else { |
| 5458 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5459 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5460 | // Concatenate this output onto the outputs list. |
| 5461 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5462 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5463 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5464 | // Add information to the INLINEASM node to know that this register is |
| 5465 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5466 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5467 | InlineAsm::Kind_RegDefEarlyClobber : |
| 5468 | InlineAsm::Kind_RegDef, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5469 | false, |
| 5470 | 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5471 | DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5472 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5473 | break; |
| 5474 | } |
| 5475 | case InlineAsm::isInput: { |
| 5476 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5477 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5478 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5479 | // If this is required to match an output register we have already set, |
| 5480 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5481 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5482 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5483 | // Scan until we find the definition we already emitted of this operand. |
| 5484 | // When we find it, create a RegsForValue operand. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5485 | unsigned CurOp = InlineAsm::Op_FirstOperand; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5486 | for (; OperandNo; --OperandNo) { |
| 5487 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5488 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5489 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5490 | assert((InlineAsm::isRegDefKind(OpFlag) || |
| 5491 | InlineAsm::isRegDefEarlyClobberKind(OpFlag) || |
| 5492 | InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5493 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5496 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5497 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5498 | if (InlineAsm::isRegDefKind(OpFlag) || |
| 5499 | InlineAsm::isRegDefEarlyClobberKind(OpFlag)) { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5500 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame^] | 5501 | if (OpInfo.isIndirect) { |
| 5502 | // This happens on gcc/testsuite/gcc.dg/pr8788-1.c |
| 5503 | LLVMContext &Ctx = CurMBB->getParent()->getFunction()->getContext(); |
| 5504 | Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:" |
| 5505 | " don't know how to handle tied " |
| 5506 | "indirect register inputs"); |
| 5507 | } |
| 5508 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5509 | RegsForValue MatchedRegs; |
| 5510 | MatchedRegs.TLI = &TLI; |
| 5511 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5512 | EVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5513 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5514 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5515 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5516 | i != e; ++i) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5517 | MatchedRegs.Regs.push_back |
| 5518 | (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5519 | |
| 5520 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5521 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5522 | Chain, &Flag); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5523 | MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5524 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5525 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5526 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5527 | } |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5528 | |
| 5529 | assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!"); |
| 5530 | assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 && |
| 5531 | "Unexpected number of operands"); |
| 5532 | // Add information to the INLINEASM node to know about this input. |
| 5533 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5534 | OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag, |
| 5535 | OpInfo.getMatchedOperand()); |
| 5536 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
| 5537 | TLI.getPointerTy())); |
| 5538 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5539 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5540 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5541 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5542 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5543 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5544 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5545 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5546 | std::vector<SDValue> Ops; |
| 5547 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5548 | hasMemory, Ops, DAG); |
Chris Lattner | 87d677c | 2010-04-07 23:50:38 +0000 | [diff] [blame] | 5549 | if (Ops.empty()) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5550 | report_fatal_error("Invalid operand for inline asm" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5551 | " constraint '" + OpInfo.ConstraintCode + "'!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5552 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5553 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5554 | unsigned ResOpType = |
| 5555 | InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5556 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5557 | TLI.getPointerTy())); |
| 5558 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5559 | break; |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
| 5562 | if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5563 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5564 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5565 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5566 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5567 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5568 | unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5569 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5570 | TLI.getPointerTy())); |
| 5571 | AsmNodeOperands.push_back(InOperandVal); |
| 5572 | break; |
| 5573 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5574 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5575 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5576 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5577 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5578 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5579 | "Don't know how to handle indirect register inputs yet!"); |
| 5580 | |
| 5581 | // Copy the input into the appropriate registers. |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 5582 | if (OpInfo.AssignedRegs.Regs.empty() || |
Chris Lattner | 87d677c | 2010-04-07 23:50:38 +0000 | [diff] [blame] | 5583 | !OpInfo.AssignedRegs.areValueTypesLegal()) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5584 | report_fatal_error("Couldn't allocate input reg for" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 5585 | " constraint '"+ OpInfo.ConstraintCode +"'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5586 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5587 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5588 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5589 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5590 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5591 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5592 | break; |
| 5593 | } |
| 5594 | case InlineAsm::isClobber: { |
| 5595 | // Add the clobbered value to the operand list, so that the register |
| 5596 | // allocator is aware that the physreg got clobbered. |
| 5597 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5598 | OpInfo.AssignedRegs.AddInlineAsmOperands( |
| 5599 | InlineAsm::Kind_RegDefEarlyClobber, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5600 | false, 0, DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5601 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5602 | break; |
| 5603 | } |
| 5604 | } |
| 5605 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5606 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5607 | // 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] | 5608 | AsmNodeOperands[0] = Chain; |
| 5609 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5610 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5611 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5612 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5613 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5614 | Flag = Chain.getValue(1); |
| 5615 | |
| 5616 | // If this asm returns a register value, copy the result from that register |
| 5617 | // and set it as the value of the call. |
| 5618 | if (!RetValRegs.Regs.empty()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5619 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5620 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5621 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5622 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5623 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5624 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5625 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5626 | // If any of the results of the inline asm is a vector, it may have the |
| 5627 | // wrong width/num elts. This can happen for register classes that can |
| 5628 | // contain multiple different value types. The preg or vreg allocated may |
| 5629 | // not have the same VT as was expected. Convert it to the right type |
| 5630 | // with bit_convert. |
| 5631 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5632 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5633 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5634 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5635 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5636 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5637 | // If a result value was tied to an input value, the computed result may |
| 5638 | // have a wider width than the expected result. Extract the relevant |
| 5639 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5640 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5641 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5642 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5643 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5644 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5645 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5646 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5647 | // Don't need to use this as a chain in this case. |
| 5648 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5649 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5650 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5651 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5652 | std::vector<std::pair<SDValue, Value*> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5653 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5654 | // Process indirect outputs, first output all of the flagged copies out of |
| 5655 | // physregs. |
| 5656 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5657 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
| 5658 | Value *Ptr = IndirectStoresToEmit[i].second; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5659 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5660 | Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5661 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5662 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5663 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5664 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5665 | // Emit the non-flagged stores from the physregs. |
| 5666 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5667 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
| 5668 | SDValue Val = DAG.getStore(Chain, getCurDebugLoc(), |
| 5669 | StoresToEmit[i].first, |
| 5670 | getValue(StoresToEmit[i].second), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5671 | StoresToEmit[i].second, 0, |
| 5672 | false, false, 0); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5673 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5674 | } |
| 5675 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5676 | if (!OutChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5677 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5678 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5679 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5680 | DAG.setRoot(Chain); |
| 5681 | } |
| 5682 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5683 | void SelectionDAGBuilder::visitVAStart(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5684 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
| 5685 | MVT::Other, getRoot(), |
| 5686 | getValue(I.getOperand(1)), |
| 5687 | DAG.getSrcValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5688 | } |
| 5689 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5690 | void SelectionDAGBuilder::visitVAArg(VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5691 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5692 | getRoot(), getValue(I.getOperand(0)), |
| 5693 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5694 | setValue(&I, V); |
| 5695 | DAG.setRoot(V.getValue(1)); |
| 5696 | } |
| 5697 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5698 | void SelectionDAGBuilder::visitVAEnd(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5699 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
| 5700 | MVT::Other, getRoot(), |
| 5701 | getValue(I.getOperand(1)), |
| 5702 | DAG.getSrcValue(I.getOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5705 | void SelectionDAGBuilder::visitVACopy(CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5706 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
| 5707 | MVT::Other, getRoot(), |
| 5708 | getValue(I.getOperand(1)), |
| 5709 | getValue(I.getOperand(2)), |
| 5710 | DAG.getSrcValue(I.getOperand(1)), |
| 5711 | DAG.getSrcValue(I.getOperand(2)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5714 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5715 | /// implementation, which just calls LowerCall. |
| 5716 | /// FIXME: When all targets are |
| 5717 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5718 | std::pair<SDValue, SDValue> |
| 5719 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5720 | bool RetSExt, bool RetZExt, bool isVarArg, |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 5721 | bool isInreg, unsigned NumFixedArgs, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 5722 | CallingConv::ID CallConv, bool isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5723 | bool isReturnValueUsed, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5724 | SDValue Callee, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5725 | ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5726 | // Handle all of the outgoing arguments. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5727 | SmallVector<ISD::OutputArg, 32> Outs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5728 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5729 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5730 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5731 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5732 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5733 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5734 | const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5735 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5736 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5737 | ISD::ArgFlagsTy Flags; |
| 5738 | unsigned OriginalAlignment = |
| 5739 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5740 | |
| 5741 | if (Args[i].isZExt) |
| 5742 | Flags.setZExt(); |
| 5743 | if (Args[i].isSExt) |
| 5744 | Flags.setSExt(); |
| 5745 | if (Args[i].isInReg) |
| 5746 | Flags.setInReg(); |
| 5747 | if (Args[i].isSRet) |
| 5748 | Flags.setSRet(); |
| 5749 | if (Args[i].isByVal) { |
| 5750 | Flags.setByVal(); |
| 5751 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5752 | const Type *ElementTy = Ty->getElementType(); |
| 5753 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5754 | unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5755 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5756 | // info is not there but there are cases it cannot get right. |
| 5757 | if (Args[i].Alignment) |
| 5758 | FrameAlign = Args[i].Alignment; |
| 5759 | Flags.setByValAlign(FrameAlign); |
| 5760 | Flags.setByValSize(FrameSize); |
| 5761 | } |
| 5762 | if (Args[i].isNest) |
| 5763 | Flags.setNest(); |
| 5764 | Flags.setOrigAlign(OriginalAlignment); |
| 5765 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5766 | EVT PartVT = getRegisterType(RetTy->getContext(), VT); |
| 5767 | unsigned NumParts = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5768 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5769 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5770 | |
| 5771 | if (Args[i].isSExt) |
| 5772 | ExtendKind = ISD::SIGN_EXTEND; |
| 5773 | else if (Args[i].isZExt) |
| 5774 | ExtendKind = ISD::ZERO_EXTEND; |
| 5775 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5776 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5777 | PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5778 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5779 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5780 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5781 | ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs); |
| 5782 | if (NumParts > 1 && j == 0) |
| 5783 | MyFlags.Flags.setSplit(); |
| 5784 | else if (j != 0) |
| 5785 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5786 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5787 | Outs.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5788 | } |
| 5789 | } |
| 5790 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5791 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5792 | // Handle the incoming return values from the call. |
| 5793 | SmallVector<ISD::InputArg, 32> Ins; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5794 | SmallVector<EVT, 4> RetTys; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5795 | ComputeValueVTs(*this, RetTy, RetTys); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5796 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5797 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5798 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5799 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5800 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5801 | ISD::InputArg MyFlags; |
| 5802 | MyFlags.VT = RegisterVT; |
| 5803 | MyFlags.Used = isReturnValueUsed; |
| 5804 | if (RetSExt) |
| 5805 | MyFlags.Flags.setSExt(); |
| 5806 | if (RetZExt) |
| 5807 | MyFlags.Flags.setZExt(); |
| 5808 | if (isInreg) |
| 5809 | MyFlags.Flags.setInReg(); |
| 5810 | Ins.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5811 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5814 | SmallVector<SDValue, 4> InVals; |
Evan Cheng | 022d9e1 | 2010-02-02 23:55:14 +0000 | [diff] [blame] | 5815 | Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5816 | Outs, Ins, dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5817 | |
| 5818 | // Verify that the target's LowerCall behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5819 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5820 | "LowerCall didn't return a valid chain!"); |
| 5821 | assert((!isTailCall || InVals.empty()) && |
| 5822 | "LowerCall emitted a return value for a tail call!"); |
| 5823 | assert((isTailCall || InVals.size() == Ins.size()) && |
| 5824 | "LowerCall didn't emit the correct number of values!"); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5825 | |
| 5826 | // For a tail call, the return value is merely live-out and there aren't |
| 5827 | // any nodes in the DAG representing it. Return a special value to |
| 5828 | // indicate that a tail call has been emitted and no more Instructions |
| 5829 | // should be processed in the current block. |
| 5830 | if (isTailCall) { |
| 5831 | DAG.setRoot(Chain); |
| 5832 | return std::make_pair(SDValue(), SDValue()); |
| 5833 | } |
| 5834 | |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 5835 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5836 | assert(InVals[i].getNode() && |
| 5837 | "LowerCall emitted a null value!"); |
| 5838 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5839 | "LowerCall emitted a value with the wrong type!"); |
| 5840 | }); |
| 5841 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5842 | // Collect the legal value parts into potentially illegal values |
| 5843 | // that correspond to the original function's return values. |
| 5844 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5845 | if (RetSExt) |
| 5846 | AssertOp = ISD::AssertSext; |
| 5847 | else if (RetZExt) |
| 5848 | AssertOp = ISD::AssertZext; |
| 5849 | SmallVector<SDValue, 4> ReturnValues; |
| 5850 | unsigned CurReg = 0; |
| 5851 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5852 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5853 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5854 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5855 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5856 | ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg], |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5857 | NumRegs, RegisterVT, VT, |
| 5858 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5859 | CurReg += NumRegs; |
| 5860 | } |
| 5861 | |
| 5862 | // For a function returning void, there is no return value. We can't create |
| 5863 | // such a node, so we just return a null return value in that case. In |
| 5864 | // that case, nothing will actualy look at the value. |
| 5865 | if (ReturnValues.empty()) |
| 5866 | return std::make_pair(SDValue(), Chain); |
| 5867 | |
| 5868 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
| 5869 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5870 | &ReturnValues[0], ReturnValues.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5871 | return std::make_pair(Res, Chain); |
| 5872 | } |
| 5873 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5874 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5875 | SmallVectorImpl<SDValue> &Results, |
| 5876 | SelectionDAG &DAG) { |
| 5877 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5878 | if (Res.getNode()) |
| 5879 | Results.push_back(Res); |
| 5880 | } |
| 5881 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5882 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5883 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5884 | return SDValue(); |
| 5885 | } |
| 5886 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5887 | void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5888 | SDValue Op = getValue(V); |
| 5889 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5890 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5891 | "Copy from a reg to the same reg!"); |
| 5892 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5893 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5894 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5895 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5896 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5897 | PendingExports.push_back(Chain); |
| 5898 | } |
| 5899 | |
| 5900 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5901 | |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 5902 | void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5903 | // If this is the entry block, emit arguments. |
| 5904 | Function &F = *LLVMBB->getParent(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5905 | SelectionDAG &DAG = SDB->DAG; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5906 | SDValue OldRoot = DAG.getRoot(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5907 | DebugLoc dl = SDB->getCurDebugLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5908 | const TargetData *TD = TLI.getTargetData(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5909 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5910 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5911 | // Check whether the function can return without sret-demotion. |
| 5912 | SmallVector<EVT, 4> OutVTs; |
| 5913 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5914 | getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5915 | OutVTs, OutsFlags, TLI); |
| 5916 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 5917 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5918 | FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5919 | OutVTs, OutsFlags, DAG); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5920 | if (!FLI.CanLowerReturn) { |
| 5921 | // Put in an sret pointer parameter before all the other parameters. |
| 5922 | SmallVector<EVT, 1> ValueVTs; |
| 5923 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5924 | |
| 5925 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 5926 | // or one register. |
| 5927 | ISD::ArgFlagsTy Flags; |
| 5928 | Flags.setSRet(); |
| 5929 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]); |
| 5930 | ISD::InputArg RetArg(Flags, RegisterVT, true); |
| 5931 | Ins.push_back(RetArg); |
| 5932 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5933 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5934 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5935 | unsigned Idx = 1; |
| 5936 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 5937 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5938 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5939 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5940 | bool isArgValueUsed = !I->use_empty(); |
| 5941 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5942 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5943 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5944 | const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5945 | ISD::ArgFlagsTy Flags; |
| 5946 | unsigned OriginalAlignment = |
| 5947 | TD->getABITypeAlignment(ArgTy); |
| 5948 | |
| 5949 | if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5950 | Flags.setZExt(); |
| 5951 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5952 | Flags.setSExt(); |
| 5953 | if (F.paramHasAttr(Idx, Attribute::InReg)) |
| 5954 | Flags.setInReg(); |
| 5955 | if (F.paramHasAttr(Idx, Attribute::StructRet)) |
| 5956 | Flags.setSRet(); |
| 5957 | if (F.paramHasAttr(Idx, Attribute::ByVal)) { |
| 5958 | Flags.setByVal(); |
| 5959 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5960 | const Type *ElementTy = Ty->getElementType(); |
| 5961 | unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
| 5962 | unsigned FrameSize = TD->getTypeAllocSize(ElementTy); |
| 5963 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5964 | // this info is not there but there are cases it cannot get right. |
| 5965 | if (F.getParamAlignment(Idx)) |
| 5966 | FrameAlign = F.getParamAlignment(Idx); |
| 5967 | Flags.setByValAlign(FrameAlign); |
| 5968 | Flags.setByValSize(FrameSize); |
| 5969 | } |
| 5970 | if (F.paramHasAttr(Idx, Attribute::Nest)) |
| 5971 | Flags.setNest(); |
| 5972 | Flags.setOrigAlign(OriginalAlignment); |
| 5973 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5974 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5975 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5976 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5977 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed); |
| 5978 | if (NumRegs > 1 && i == 0) |
| 5979 | MyFlags.Flags.setSplit(); |
| 5980 | // if it isn't first piece, alignment must be 1 |
| 5981 | else if (i > 0) |
| 5982 | MyFlags.Flags.setOrigAlign(1); |
| 5983 | Ins.push_back(MyFlags); |
| 5984 | } |
| 5985 | } |
| 5986 | } |
| 5987 | |
| 5988 | // Call the target to set up the argument values. |
| 5989 | SmallVector<SDValue, 8> InVals; |
| 5990 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 5991 | F.isVarArg(), Ins, |
| 5992 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5993 | |
| 5994 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5995 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5996 | "LowerFormalArguments didn't return a valid chain!"); |
| 5997 | assert(InVals.size() == Ins.size() && |
| 5998 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 5999 | DEBUG({ |
| 6000 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 6001 | assert(InVals[i].getNode() && |
| 6002 | "LowerFormalArguments emitted a null value!"); |
| 6003 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 6004 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 6005 | } |
| 6006 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6007 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6008 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6009 | DAG.setRoot(NewRoot); |
| 6010 | |
| 6011 | // Set up the argument values. |
| 6012 | unsigned i = 0; |
| 6013 | Idx = 1; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6014 | if (!FLI.CanLowerReturn) { |
| 6015 | // Create a virtual register for the sret pointer, and put in a copy |
| 6016 | // from the sret argument into it. |
| 6017 | SmallVector<EVT, 1> ValueVTs; |
| 6018 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 6019 | EVT VT = ValueVTs[0]; |
| 6020 | EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 6021 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6022 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6023 | RegVT, VT, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6024 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6025 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6026 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
| 6027 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)); |
| 6028 | FLI.DemoteRegister = SRetReg; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6029 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), |
| 6030 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6031 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6032 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6033 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 6034 | // Idx indexes LLVM arguments. Don't touch it. |
| 6035 | ++i; |
| 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 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 6039 | ++I, ++Idx) { |
| 6040 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6041 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6042 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6043 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6044 | for (unsigned Value = 0; Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6045 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6046 | EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 6047 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6048 | |
| 6049 | if (!I->use_empty()) { |
| 6050 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 6051 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 6052 | AssertOp = ISD::AssertSext; |
| 6053 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 6054 | AssertOp = ISD::AssertZext; |
| 6055 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6056 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6057 | NumParts, PartVT, VT, |
| 6058 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6059 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6060 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6061 | i += NumParts; |
| 6062 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6063 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6064 | if (!I->use_empty()) { |
Evan Cheng | 8e36a5c | 2010-03-29 21:27:30 +0000 | [diff] [blame] | 6065 | SDValue Res; |
| 6066 | if (!ArgValues.empty()) |
| 6067 | Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
| 6068 | SDB->getCurDebugLoc()); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6069 | SDB->setValue(I, Res); |
| 6070 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6071 | // If this argument is live outside of the entry block, insert a copy from |
| 6072 | // 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] | 6073 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6074 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6075 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6076 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6077 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6078 | |
| 6079 | // Finally, if the target has anything special to do, allow it to do so. |
| 6080 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6081 | EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6082 | } |
| 6083 | |
| 6084 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6085 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6086 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6087 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6088 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6089 | /// the end. |
| 6090 | /// |
| 6091 | void |
| 6092 | SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) { |
| 6093 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 6094 | |
| 6095 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6096 | |
| 6097 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6098 | // from this block. |
| 6099 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 6100 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 6101 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 6102 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6103 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6104 | // If this terminator has multiple identical successors (common for |
| 6105 | // switches), only handle each succ once. |
| 6106 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6107 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6108 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6109 | PHINode *PN; |
| 6110 | |
| 6111 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6112 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6113 | // emitted yet. |
| 6114 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6115 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6116 | // Ignore dead phi's. |
| 6117 | if (PN->use_empty()) continue; |
| 6118 | |
| 6119 | unsigned Reg; |
| 6120 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6121 | |
| 6122 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6123 | unsigned &RegOut = SDB->ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6124 | if (RegOut == 0) { |
| 6125 | RegOut = FuncInfo->CreateRegForValue(C); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6126 | SDB->CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6127 | } |
| 6128 | Reg = RegOut; |
| 6129 | } else { |
| 6130 | Reg = FuncInfo->ValueMap[PHIOp]; |
| 6131 | if (Reg == 0) { |
| 6132 | assert(isa<AllocaInst>(PHIOp) && |
| 6133 | FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 6134 | "Didn't codegen value into a register!??"); |
| 6135 | Reg = FuncInfo->CreateRegForValue(PHIOp); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6136 | SDB->CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6137 | } |
| 6138 | } |
| 6139 | |
| 6140 | // Remember that this register needs to added to the machine PHI node as |
| 6141 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6142 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6143 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6144 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6145 | EVT VT = ValueVTs[vti]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6146 | unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6147 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6148 | SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6149 | Reg += NumRegisters; |
| 6150 | } |
| 6151 | } |
| 6152 | } |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6153 | SDB->ConstantsOut.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6156 | /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only |
| 6157 | /// supports legal types, and it emits MachineInstrs directly instead of |
| 6158 | /// creating SelectionDAG nodes. |
| 6159 | /// |
| 6160 | bool |
| 6161 | SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, |
| 6162 | FastISel *F) { |
| 6163 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6164 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6165 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6166 | unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6167 | |
| 6168 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6169 | // from this block. |
| 6170 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 6171 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 6172 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 6173 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6174 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6175 | // If this terminator has multiple identical successors (common for |
| 6176 | // switches), only handle each succ once. |
| 6177 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6178 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6179 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 6180 | PHINode *PN; |
| 6181 | |
| 6182 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6183 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6184 | // emitted yet. |
| 6185 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 6186 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 6187 | // Ignore dead phi's. |
| 6188 | if (PN->use_empty()) continue; |
| 6189 | |
| 6190 | // Only handle legal types. Two interesting things to note here. First, |
| 6191 | // by bailing out early, we may leave behind some dead instructions, |
| 6192 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 6193 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 6194 | // use CreateRegForValue to create registers, so it always creates |
| 6195 | // exactly one register for each non-void instruction. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6196 | EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6197 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
| 6198 | // Promote MVT::i1. |
| 6199 | if (VT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6200 | VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT); |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6201 | else { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6202 | SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 6203 | return false; |
| 6204 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 6208 | |
| 6209 | unsigned Reg = F->getRegForValue(PHIOp); |
| 6210 | if (Reg == 0) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6211 | SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6212 | return false; |
| 6213 | } |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6214 | SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6215 | } |
| 6216 | } |
| 6217 | |
| 6218 | return true; |
| 6219 | } |