Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements routines for translating from LLVM IR into SelectionDAG IR. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 15 | #include "SDNodeDbgValue.h" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 16 | #include "SelectionDAGBuilder.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 17 | #include "FunctionLoweringInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/CallingConv.h" |
| 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/Function.h" |
| 26 | #include "llvm/GlobalVariable.h" |
| 27 | #include "llvm/InlineAsm.h" |
| 28 | #include "llvm/Instructions.h" |
| 29 | #include "llvm/Intrinsics.h" |
| 30 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 31 | #include "llvm/LLVMContext.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 32 | #include "llvm/Module.h" |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/Analysis.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/FastISel.h" |
| 35 | #include "llvm/CodeGen/GCStrategy.h" |
| 36 | #include "llvm/CodeGen/GCMetadata.h" |
| 37 | #include "llvm/CodeGen/MachineFunction.h" |
| 38 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 39 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 40 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 41 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 42 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/SelectionDAG.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetRegisterInfo.h" |
| 47 | #include "llvm/Target/TargetData.h" |
| 48 | #include "llvm/Target/TargetFrameInfo.h" |
| 49 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 50 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 51 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetOptions.h" |
| 53 | #include "llvm/Support/Compiler.h" |
Mikhail Glushenkov | 2388a58 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 54 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 56 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 57 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 58 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 59 | #include <algorithm> |
| 60 | using namespace llvm; |
| 61 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 62 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 63 | /// some float libcalls (6, 8 or 12 bits). |
| 64 | static unsigned LimitFloatPrecision; |
| 65 | |
| 66 | static cl::opt<unsigned, true> |
| 67 | LimitFPPrecision("limit-float-precision", |
| 68 | cl::desc("Generate low-precision inline sequences " |
| 69 | "for some float libcalls"), |
| 70 | cl::location(LimitFloatPrecision), |
| 71 | cl::init(0)); |
| 72 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 73 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 74 | /// combined into the value they represent. If the parts combine to a type |
| 75 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 76 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 77 | /// (ISD::AssertSext). |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 78 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 79 | const SDValue *Parts, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 80 | unsigned NumParts, EVT PartVT, EVT ValueVT, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 81 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 82 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 83 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 84 | SDValue Val = Parts[0]; |
| 85 | |
| 86 | if (NumParts > 1) { |
| 87 | // Assemble the value from multiple parts. |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 88 | if (!ValueVT.isVector() && ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 89 | unsigned PartBits = PartVT.getSizeInBits(); |
| 90 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 91 | |
| 92 | // Assemble the power of 2 part. |
| 93 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 94 | 1 << Log2_32(NumParts) : NumParts; |
| 95 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 96 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 97 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 98 | SDValue Lo, Hi; |
| 99 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 100 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 101 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 102 | if (RoundParts > 2) { |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 103 | Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 104 | PartVT, HalfVT); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 105 | Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 106 | RoundParts / 2, PartVT, HalfVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 107 | } else { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 108 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]); |
| 109 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 110 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 111 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 112 | if (TLI.isBigEndian()) |
| 113 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 114 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 115 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 116 | |
| 117 | if (RoundParts < NumParts) { |
| 118 | // Assemble the trailing non-power-of-2 part. |
| 119 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 120 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 121 | Hi = getCopyFromParts(DAG, dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 122 | Parts + RoundParts, OddParts, PartVT, OddVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 123 | |
| 124 | // Combine the round and odd parts. |
| 125 | Lo = Val; |
| 126 | if (TLI.isBigEndian()) |
| 127 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 128 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 129 | Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi); |
| 130 | Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 131 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 132 | TLI.getPointerTy())); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 133 | Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo); |
| 134 | Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 135 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 136 | } else if (ValueVT.isVector()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 137 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 138 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 139 | unsigned NumIntermediates; |
| 140 | unsigned NumRegs = |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 141 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 142 | NumIntermediates, RegisterVT); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 143 | assert(NumRegs == NumParts |
| 144 | && "Part count doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 145 | NumParts = NumRegs; // Silence a compiler warning. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 146 | assert(RegisterVT == PartVT |
| 147 | && "Part type doesn't match vector breakdown!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 148 | assert(RegisterVT == Parts[0].getValueType() && |
| 149 | "Part type doesn't match part!"); |
| 150 | |
| 151 | // Assemble the parts into intermediate operands. |
| 152 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 153 | if (NumIntermediates == NumParts) { |
| 154 | // If the register was not expanded, truncate or copy the value, |
| 155 | // as appropriate. |
| 156 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 157 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 158 | PartVT, IntermediateVT); |
| 159 | } else if (NumParts > 0) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 160 | // If the intermediate type was expanded, build the intermediate |
| 161 | // operands from the parts. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 162 | assert(NumParts % NumIntermediates == 0 && |
| 163 | "Must expand into a divisible number of parts!"); |
| 164 | unsigned Factor = NumParts / NumIntermediates; |
| 165 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 166 | Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 167 | PartVT, IntermediateVT); |
| 168 | } |
| 169 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 170 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 171 | // intermediate operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 172 | Val = DAG.getNode(IntermediateVT.isVector() ? |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 173 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 174 | ValueVT, &Ops[0], NumIntermediates); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 175 | } else if (PartVT.isFloatingPoint()) { |
| 176 | // FP split into multiple FP parts (for ppcf128) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 177 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 178 | "Unexpected split"); |
| 179 | SDValue Lo, Hi; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 180 | Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]); |
| 181 | Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 182 | if (TLI.isBigEndian()) |
| 183 | std::swap(Lo, Hi); |
| 184 | Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi); |
| 185 | } else { |
| 186 | // FP split into integer parts (soft fp) |
| 187 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 188 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 189 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 190 | Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 195 | PartVT = Val.getValueType(); |
| 196 | |
| 197 | if (PartVT == ValueVT) |
| 198 | return Val; |
| 199 | |
| 200 | if (PartVT.isVector()) { |
| 201 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 202 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if (ValueVT.isVector()) { |
| 206 | assert(ValueVT.getVectorElementType() == PartVT && |
| 207 | ValueVT.getVectorNumElements() == 1 && |
| 208 | "Only trivial scalar-to-vector conversions should get here!"); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 209 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (PartVT.isInteger() && |
| 213 | ValueVT.isInteger()) { |
| 214 | if (ValueVT.bitsLT(PartVT)) { |
| 215 | // For a truncate, see if we have any information to |
| 216 | // indicate whether the truncated bits will always be |
| 217 | // zero or sign-extension. |
| 218 | if (AssertOp != ISD::DELETED_NODE) |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 219 | Val = DAG.getNode(AssertOp, dl, PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 220 | DAG.getValueType(ValueVT)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 221 | return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 222 | } else { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 223 | return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 228 | if (ValueVT.bitsLT(Val.getValueType())) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 229 | // FP_ROUND's are always exact here. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 230 | return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val, |
| 231 | DAG.getIntPtrConstant(1)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 234 | return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 237 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
| 238 | return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 239 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 240 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 241 | return SDValue(); |
| 242 | } |
| 243 | |
| 244 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 245 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 246 | /// 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] | 247 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 248 | SDValue Val, SDValue *Parts, unsigned NumParts, |
| 249 | EVT PartVT, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 250 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 251 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 252 | EVT PtrVT = TLI.getPointerTy(); |
| 253 | EVT ValueVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 254 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 255 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 256 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 257 | |
| 258 | if (!NumParts) |
| 259 | return; |
| 260 | |
| 261 | if (!ValueVT.isVector()) { |
| 262 | if (PartVT == ValueVT) { |
| 263 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 264 | Parts[0] = Val; |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 269 | // If the parts cover more bits than the value has, promote the value. |
| 270 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 271 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 272 | Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 273 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 274 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 275 | Val = DAG.getNode(ExtendKind, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 276 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 277 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 278 | } |
| 279 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 280 | // Different types of the same size. |
| 281 | assert(NumParts == 1 && PartVT != ValueVT); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 282 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 283 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 284 | // If the parts cover less bits than value has, truncate the value. |
| 285 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 286 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 287 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 288 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 289 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | // The value may have changed - recompute ValueVT. |
| 294 | ValueVT = Val.getValueType(); |
| 295 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 296 | "Failed to tile the value with PartVT!"); |
| 297 | |
| 298 | if (NumParts == 1) { |
| 299 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 300 | Parts[0] = Val; |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | // Expand the value into multiple parts. |
| 305 | if (NumParts & (NumParts - 1)) { |
| 306 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 307 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 308 | "Do not know what to expand to!"); |
| 309 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 310 | unsigned RoundBits = RoundParts * PartBits; |
| 311 | unsigned OddParts = NumParts - RoundParts; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 312 | SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 313 | DAG.getConstant(RoundBits, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 314 | TLI.getPointerTy())); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 315 | getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 316 | OddParts, PartVT); |
| 317 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 318 | if (TLI.isBigEndian()) |
| 319 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 320 | std::reverse(Parts + RoundParts, Parts + NumParts); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 321 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 322 | NumParts = RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 323 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 324 | Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 328 | // EXTRACT_ELEMENT. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 329 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 330 | EVT::getIntegerVT(*DAG.getContext(), |
| 331 | ValueVT.getSizeInBits()), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 332 | Val); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 333 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 334 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 335 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 336 | unsigned ThisBits = StepSize * PartBits / 2; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 337 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 338 | SDValue &Part0 = Parts[i]; |
| 339 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 340 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 341 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 342 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 343 | DAG.getConstant(1, PtrVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 344 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 345 | ThisVT, Part0, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 346 | DAG.getConstant(0, PtrVT)); |
| 347 | |
| 348 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 349 | Part0 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 350 | PartVT, Part0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 351 | Part1 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 352 | PartVT, Part1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (TLI.isBigEndian()) |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 358 | std::reverse(Parts, Parts + OrigNumParts); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 359 | |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | // Vector ValueVT. |
| 364 | if (NumParts == 1) { |
| 365 | if (PartVT != ValueVT) { |
Bob Wilson | 5afffae | 2009-12-18 01:03:29 +0000 | [diff] [blame] | 366 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 367 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 368 | } else { |
| 369 | assert(ValueVT.getVectorElementType() == PartVT && |
| 370 | ValueVT.getVectorNumElements() == 1 && |
| 371 | "Only trivial vector-to-scalar conversions should get here!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 372 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 373 | PartVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 374 | DAG.getConstant(0, PtrVT)); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | Parts[0] = Val; |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | // Handle a multi-element vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 383 | EVT IntermediateVT, RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 384 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 385 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
| 386 | IntermediateVT, NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 387 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 388 | |
| 389 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 390 | NumParts = NumRegs; // Silence a compiler warning. |
| 391 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 392 | |
| 393 | // Split the vector into intermediate operands. |
| 394 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 395 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 396 | if (IntermediateVT.isVector()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 397 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 398 | IntermediateVT, Val, |
| 399 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 400 | PtrVT)); |
| 401 | else |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 402 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 403 | IntermediateVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 404 | DAG.getConstant(i, PtrVT)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 407 | // Split the intermediate operands into legal parts. |
| 408 | if (NumParts == NumIntermediates) { |
| 409 | // If the register was not expanded, promote or copy the value, |
| 410 | // as appropriate. |
| 411 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 412 | getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 413 | } else if (NumParts > 0) { |
| 414 | // If the intermediate type was expanded, split each the value into |
| 415 | // legal parts. |
| 416 | assert(NumParts % NumIntermediates == 0 && |
| 417 | "Must expand into a divisible number of parts!"); |
| 418 | unsigned Factor = NumParts / NumIntermediates; |
| 419 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 420 | getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 424 | namespace { |
| 425 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 426 | /// that a particular set of values is assigned, and the type information |
| 427 | /// about the value. The most common situation is to represent one value at a |
| 428 | /// time, but struct or array values are handled element-wise as multiple |
| 429 | /// values. The splitting of aggregates is performed recursively, so that we |
| 430 | /// never have aggregate-typed registers. The values at this point do not |
| 431 | /// necessarily have legal types, so each value may require one or more |
| 432 | /// registers of some legal type. |
| 433 | /// |
| 434 | struct RegsForValue { |
| 435 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 436 | /// may need be promoted or synthesized from one or more registers. |
| 437 | /// |
| 438 | SmallVector<EVT, 4> ValueVTs; |
| 439 | |
| 440 | /// RegVTs - The value types of the registers. This is the same size as |
| 441 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 442 | /// register or registers are. (Individual values are never synthesized |
| 443 | /// from more than one type of register.) |
| 444 | /// |
| 445 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 446 | /// getRegisterType member function, however when with physical registers |
| 447 | /// it is necessary to have a separate record of the types. |
| 448 | /// |
| 449 | SmallVector<EVT, 4> RegVTs; |
| 450 | |
| 451 | /// Regs - This list holds the registers assigned to the values. |
| 452 | /// Each legal or promoted value requires one register, and each |
| 453 | /// expanded value requires multiple registers. |
| 454 | /// |
| 455 | SmallVector<unsigned, 4> Regs; |
| 456 | |
| 457 | RegsForValue() {} |
| 458 | |
| 459 | RegsForValue(const SmallVector<unsigned, 4> ®s, |
| 460 | EVT regvt, EVT valuevt) |
| 461 | : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 462 | |
| 463 | RegsForValue(const SmallVector<unsigned, 4> ®s, |
| 464 | const SmallVector<EVT, 4> ®vts, |
| 465 | const SmallVector<EVT, 4> &valuevts) |
| 466 | : ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
| 467 | |
| 468 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
| 469 | unsigned Reg, const Type *Ty) { |
| 470 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 471 | |
| 472 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 473 | EVT ValueVT = ValueVTs[Value]; |
| 474 | unsigned NumRegs = tli.getNumRegisters(Context, ValueVT); |
| 475 | EVT RegisterVT = tli.getRegisterType(Context, ValueVT); |
| 476 | for (unsigned i = 0; i != NumRegs; ++i) |
| 477 | Regs.push_back(Reg + i); |
| 478 | RegVTs.push_back(RegisterVT); |
| 479 | Reg += NumRegs; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 484 | bool areValueTypesLegal(const TargetLowering &TLI) { |
| 485 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 486 | EVT RegisterVT = RegVTs[Value]; |
| 487 | if (!TLI.isTypeLegal(RegisterVT)) |
| 488 | return false; |
| 489 | } |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | /// append - Add the specified values to this one. |
| 494 | void append(const RegsForValue &RHS) { |
| 495 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 496 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 497 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 498 | } |
| 499 | |
| 500 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 501 | /// this value and returns the result as a ValueVTs value. This uses |
| 502 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 503 | /// If the Flag pointer is NULL, no flag is used. |
| 504 | SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, |
| 505 | DebugLoc dl, |
| 506 | SDValue &Chain, SDValue *Flag) const; |
| 507 | |
| 508 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 509 | /// specified value into the registers specified by this object. This uses |
| 510 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 511 | /// If the Flag pointer is NULL, no flag is used. |
| 512 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
| 513 | SDValue &Chain, SDValue *Flag) const; |
| 514 | |
| 515 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 516 | /// operand list. This adds the code marker, matching input operand index |
| 517 | /// (if applicable), and includes the number of values added into it. |
| 518 | void AddInlineAsmOperands(unsigned Kind, |
| 519 | bool HasMatching, unsigned MatchingIdx, |
| 520 | SelectionDAG &DAG, |
| 521 | std::vector<SDValue> &Ops) const; |
| 522 | }; |
| 523 | } |
| 524 | |
| 525 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 526 | /// this value and returns the result as a ValueVT value. This uses |
| 527 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 528 | /// If the Flag pointer is NULL, no flag is used. |
| 529 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
| 530 | FunctionLoweringInfo &FuncInfo, |
| 531 | DebugLoc dl, |
| 532 | SDValue &Chain, SDValue *Flag) const { |
| 533 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 534 | |
| 535 | // Assemble the legal parts into the final values. |
| 536 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 537 | SmallVector<SDValue, 8> Parts; |
| 538 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 539 | // Copy the legal parts from the registers. |
| 540 | EVT ValueVT = ValueVTs[Value]; |
| 541 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
| 542 | EVT RegisterVT = RegVTs[Value]; |
| 543 | |
| 544 | Parts.resize(NumRegs); |
| 545 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 546 | SDValue P; |
| 547 | if (Flag == 0) { |
| 548 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
| 549 | } else { |
| 550 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
| 551 | *Flag = P.getValue(2); |
| 552 | } |
| 553 | |
| 554 | Chain = P.getValue(1); |
| 555 | |
| 556 | // If the source register was virtual and if we know something about it, |
| 557 | // add an assert node. |
| 558 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 559 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 560 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 561 | if (FuncInfo.LiveOutRegInfo.size() > SlotNo) { |
| 562 | const FunctionLoweringInfo::LiveOutInfo &LOI = |
| 563 | FuncInfo.LiveOutRegInfo[SlotNo]; |
| 564 | |
| 565 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 566 | unsigned NumSignBits = LOI.NumSignBits; |
| 567 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
| 568 | |
| 569 | // FIXME: We capture more information than the dag can represent. For |
| 570 | // now, just use the tightest assertzext/assertsext possible. |
| 571 | bool isSExt = true; |
| 572 | EVT FromVT(MVT::Other); |
| 573 | if (NumSignBits == RegSize) |
| 574 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 575 | else if (NumZeroBits >= RegSize-1) |
| 576 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 577 | else if (NumSignBits > RegSize-8) |
| 578 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
| 579 | else if (NumZeroBits >= RegSize-8) |
| 580 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 581 | else if (NumSignBits > RegSize-16) |
| 582 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
| 583 | else if (NumZeroBits >= RegSize-16) |
| 584 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
| 585 | else if (NumSignBits > RegSize-32) |
| 586 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
| 587 | else if (NumZeroBits >= RegSize-32) |
| 588 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
| 589 | |
| 590 | if (FromVT != MVT::Other) |
| 591 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
| 592 | RegisterVT, P, DAG.getValueType(FromVT)); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | Parts[i] = P; |
| 597 | } |
| 598 | |
| 599 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
| 600 | NumRegs, RegisterVT, ValueVT); |
| 601 | Part += NumRegs; |
| 602 | Parts.clear(); |
| 603 | } |
| 604 | |
| 605 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 606 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 607 | &Values[0], ValueVTs.size()); |
| 608 | } |
| 609 | |
| 610 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 611 | /// specified value into the registers specified by this object. This uses |
| 612 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 613 | /// If the Flag pointer is NULL, no flag is used. |
| 614 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
| 615 | SDValue &Chain, SDValue *Flag) const { |
| 616 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 617 | |
| 618 | // Get the list of the values's legal parts. |
| 619 | unsigned NumRegs = Regs.size(); |
| 620 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 621 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 622 | EVT ValueVT = ValueVTs[Value]; |
| 623 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
| 624 | EVT RegisterVT = RegVTs[Value]; |
| 625 | |
| 626 | getCopyToParts(DAG, dl, |
| 627 | Val.getValue(Val.getResNo() + Value), |
| 628 | &Parts[Part], NumParts, RegisterVT); |
| 629 | Part += NumParts; |
| 630 | } |
| 631 | |
| 632 | // Copy the parts into the registers. |
| 633 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 634 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 635 | SDValue Part; |
| 636 | if (Flag == 0) { |
| 637 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
| 638 | } else { |
| 639 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
| 640 | *Flag = Part.getValue(1); |
| 641 | } |
| 642 | |
| 643 | Chains[i] = Part.getValue(0); |
| 644 | } |
| 645 | |
| 646 | if (NumRegs == 1 || Flag) |
| 647 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
| 648 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 649 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 650 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 651 | // user as well as a successor (the TF operands are flagged to the user). |
| 652 | // c1, f1 = CopyToReg |
| 653 | // c2, f2 = CopyToReg |
| 654 | // c3 = TokenFactor c1, c2 |
| 655 | // ... |
| 656 | // = op c3, ..., f2 |
| 657 | Chain = Chains[NumRegs-1]; |
| 658 | else |
| 659 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
| 660 | } |
| 661 | |
| 662 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 663 | /// operand list. This adds the code marker and includes the number of |
| 664 | /// values added into it. |
| 665 | void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching, |
| 666 | unsigned MatchingIdx, |
| 667 | SelectionDAG &DAG, |
| 668 | std::vector<SDValue> &Ops) const { |
| 669 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 670 | |
| 671 | unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size()); |
| 672 | if (HasMatching) |
| 673 | Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx); |
| 674 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
| 675 | Ops.push_back(Res); |
| 676 | |
| 677 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 678 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
| 679 | EVT RegisterVT = RegVTs[Value]; |
| 680 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 681 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
| 682 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
| 683 | } |
| 684 | } |
| 685 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 686 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 687 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 688 | AA = &aa; |
| 689 | GFI = gfi; |
| 690 | TD = DAG.getTarget().getTargetData(); |
| 691 | } |
| 692 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 693 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 694 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 695 | /// for a new block. This doesn't clear out information about |
| 696 | /// additional blocks that are needed to complete switch lowering |
| 697 | /// or PHI node updating; that information is cleared out as it is |
| 698 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 699 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 700 | NodeMap.clear(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 701 | UnusedArgNodeMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 702 | PendingLoads.clear(); |
| 703 | PendingExports.clear(); |
Chris Lattner | a4f2bb0 | 2010-04-02 20:17:23 +0000 | [diff] [blame] | 704 | CurDebugLoc = DebugLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 705 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 709 | /// flushing any PendingLoad items. This must be done before emitting |
| 710 | /// a store or any other node that may need to be ordered after any |
| 711 | /// prior load instructions. |
| 712 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 713 | SDValue SelectionDAGBuilder::getRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 714 | if (PendingLoads.empty()) |
| 715 | return DAG.getRoot(); |
| 716 | |
| 717 | if (PendingLoads.size() == 1) { |
| 718 | SDValue Root = PendingLoads[0]; |
| 719 | DAG.setRoot(Root); |
| 720 | PendingLoads.clear(); |
| 721 | return Root; |
| 722 | } |
| 723 | |
| 724 | // Otherwise, we have to make a token factor node. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 725 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 726 | &PendingLoads[0], PendingLoads.size()); |
| 727 | PendingLoads.clear(); |
| 728 | DAG.setRoot(Root); |
| 729 | return Root; |
| 730 | } |
| 731 | |
| 732 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 733 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 734 | /// to do this before emitting a terminator instruction. |
| 735 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 736 | SDValue SelectionDAGBuilder::getControlRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 737 | SDValue Root = DAG.getRoot(); |
| 738 | |
| 739 | if (PendingExports.empty()) |
| 740 | return Root; |
| 741 | |
| 742 | // Turn all of the CopyToReg chains into one factored node. |
| 743 | if (Root.getOpcode() != ISD::EntryToken) { |
| 744 | unsigned i = 0, e = PendingExports.size(); |
| 745 | for (; i != e; ++i) { |
| 746 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 747 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 748 | break; // Don't add the root if we already indirectly depend on it. |
| 749 | } |
| 750 | |
| 751 | if (i == e) |
| 752 | PendingExports.push_back(Root); |
| 753 | } |
| 754 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 755 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 756 | &PendingExports[0], |
| 757 | PendingExports.size()); |
| 758 | PendingExports.clear(); |
| 759 | DAG.setRoot(Root); |
| 760 | return Root; |
| 761 | } |
| 762 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 763 | void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) { |
| 764 | if (DAG.GetOrdering(Node) != 0) return; // Already has ordering. |
| 765 | DAG.AssignOrdering(Node, SDNodeOrder); |
| 766 | |
| 767 | for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) |
| 768 | AssignOrderingToNode(Node->getOperand(I).getNode()); |
| 769 | } |
| 770 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 771 | void SelectionDAGBuilder::visit(const Instruction &I) { |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 772 | // Set up outgoing PHI node register values before emitting the terminator. |
| 773 | if (isa<TerminatorInst>(&I)) |
| 774 | HandlePHINodesInSuccessorBlocks(I.getParent()); |
| 775 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 776 | CurDebugLoc = I.getDebugLoc(); |
| 777 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 778 | visit(I.getOpcode(), I); |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 779 | |
Dan Gohman | 92884f7 | 2010-04-20 15:03:56 +0000 | [diff] [blame] | 780 | if (!isa<TerminatorInst>(&I) && !HasTailCall) |
| 781 | CopyToExportRegsIfNeeded(&I); |
| 782 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 783 | CurDebugLoc = DebugLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 786 | void SelectionDAGBuilder::visitPHI(const PHINode &) { |
| 787 | llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!"); |
| 788 | } |
| 789 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 790 | void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 791 | // Note: this doesn't use InstVisitor, because it has to work with |
| 792 | // ConstantExpr's in addition to instructions. |
| 793 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 794 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 795 | // Build the switch statement using the Instruction.def file. |
| 796 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 797 | case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 798 | #include "llvm/Instruction.def" |
| 799 | } |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 800 | |
| 801 | // Assign the ordering to the freshly created DAG nodes. |
| 802 | if (NodeMap.count(&I)) { |
| 803 | ++SDNodeOrder; |
| 804 | AssignOrderingToNode(getValue(&I).getNode()); |
| 805 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 806 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 807 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 808 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 809 | SDValue &N = NodeMap[V]; |
| 810 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 811 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 812 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 813 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 814 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 815 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | c7bd7b7 | 2010-06-21 16:02:28 +0000 | [diff] [blame] | 816 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 817 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 818 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Dan Gohman | c7bd7b7 | 2010-06-21 16:02:28 +0000 | [diff] [blame] | 819 | return N = DAG.getGlobalAddress(GV, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 820 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 821 | if (isa<ConstantPointerNull>(C)) |
Dan Gohman | c7bd7b7 | 2010-06-21 16:02:28 +0000 | [diff] [blame] | 822 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 823 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 824 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | c7bd7b7 | 2010-06-21 16:02:28 +0000 | [diff] [blame] | 825 | return N = DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 826 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 827 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dan Gohman | c7bd7b7 | 2010-06-21 16:02:28 +0000 | [diff] [blame] | 828 | return N = DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 829 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 830 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 831 | visit(CE->getOpcode(), *CE); |
| 832 | SDValue N1 = NodeMap[V]; |
Dan Gohman | ac7d05c | 2010-04-16 16:55:18 +0000 | [diff] [blame] | 833 | assert(N1.getNode() && "visit didn't populate the NodeMap!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 834 | return N1; |
| 835 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 836 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 837 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 838 | SmallVector<SDValue, 4> Constants; |
| 839 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 840 | OI != OE; ++OI) { |
| 841 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 842 | // If the operand is an empty aggregate, there are no values. |
| 843 | if (!Val) continue; |
| 844 | // Add each leaf value from the operand to the Constants list |
| 845 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 846 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 847 | Constants.push_back(SDValue(Val, i)); |
| 848 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 849 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 850 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 851 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 854 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 855 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 856 | "Unknown struct or array constant!"); |
| 857 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 858 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 859 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 860 | unsigned NumElts = ValueVTs.size(); |
| 861 | if (NumElts == 0) |
| 862 | return SDValue(); // empty struct |
| 863 | SmallVector<SDValue, 4> Constants(NumElts); |
| 864 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 865 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 866 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 867 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 868 | else if (EltVT.isFloatingPoint()) |
| 869 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 870 | else |
| 871 | Constants[i] = DAG.getConstant(0, EltVT); |
| 872 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 873 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 874 | return DAG.getMergeValues(&Constants[0], NumElts, |
| 875 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 878 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 879 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 880 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 881 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 882 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 883 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 884 | // Now that we know the number and type of the elements, get that number of |
| 885 | // elements into the Ops array based on what kind of constant it is. |
| 886 | SmallVector<SDValue, 16> Ops; |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 887 | if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 888 | for (unsigned i = 0; i != NumElements; ++i) |
| 889 | Ops.push_back(getValue(CP->getOperand(i))); |
| 890 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 891 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 892 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 893 | |
| 894 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 895 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 896 | Op = DAG.getConstantFP(0, EltVT); |
| 897 | else |
| 898 | Op = DAG.getConstant(0, EltVT); |
| 899 | Ops.assign(NumElements, Op); |
| 900 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 901 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 902 | // Create a BUILD_VECTOR node. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 903 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 904 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 905 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 906 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 907 | // If this is a static alloca, generate it as the frameindex instead of |
| 908 | // computation. |
| 909 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 910 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 911 | FuncInfo.StaticAllocaMap.find(AI); |
| 912 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 913 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 914 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 915 | |
Dan Gohman | c7bd7b7 | 2010-06-21 16:02:28 +0000 | [diff] [blame] | 916 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 917 | assert(InReg && "Value not in map!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 918 | |
Dan Gohman | c7bd7b7 | 2010-06-21 16:02:28 +0000 | [diff] [blame] | 919 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
| 920 | SDValue Chain = DAG.getEntryNode(); |
| 921 | return RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 924 | /// Get the EVTs and ArgFlags collections that represent the legalized return |
| 925 | /// type of the given function. This does not require a DAG or a return value, |
| 926 | /// 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] | 927 | static void getReturnInfo(const Type* ReturnType, |
| 928 | Attributes attr, SmallVectorImpl<EVT> &OutVTs, |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 929 | SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 930 | const TargetLowering &TLI, |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 931 | SmallVectorImpl<uint64_t> *Offsets = 0) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 932 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 933 | ComputeValueVTs(TLI, ReturnType, ValueVTs); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 934 | unsigned NumValues = ValueVTs.size(); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 935 | if (NumValues == 0) return; |
| 936 | unsigned Offset = 0; |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 937 | |
| 938 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 939 | EVT VT = ValueVTs[j]; |
| 940 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 941 | |
| 942 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 943 | ExtendKind = ISD::SIGN_EXTEND; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 944 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 945 | ExtendKind = ISD::ZERO_EXTEND; |
| 946 | |
| 947 | // FIXME: C calling convention requires the return type to be promoted to |
| 948 | // at least 32-bit. But this is not necessary for non-C calling |
| 949 | // conventions. The frontend should mark functions whose return values |
| 950 | // require promoting with signext or zeroext attributes. |
| 951 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 952 | EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32); |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 953 | if (VT.bitsLT(MinVT)) |
| 954 | VT = MinVT; |
| 955 | } |
| 956 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 957 | unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT); |
| 958 | EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 959 | unsigned PartSize = TLI.getTargetData()->getTypeAllocSize( |
| 960 | PartVT.getTypeForEVT(ReturnType->getContext())); |
| 961 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 962 | // 'inreg' on function refers to return value |
| 963 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 964 | if (attr & Attribute::InReg) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 965 | Flags.setInReg(); |
| 966 | |
| 967 | // Propagate extension type if any |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 968 | if (attr & Attribute::SExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 969 | Flags.setSExt(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 970 | else if (attr & Attribute::ZExt) |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 971 | Flags.setZExt(); |
| 972 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 973 | for (unsigned i = 0; i < NumParts; ++i) { |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 974 | OutVTs.push_back(PartVT); |
| 975 | OutFlags.push_back(Flags); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 976 | if (Offsets) |
| 977 | { |
| 978 | Offsets->push_back(Offset); |
| 979 | Offset += PartSize; |
| 980 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 984 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 985 | void SelectionDAGBuilder::visitRet(const ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 986 | SDValue Chain = getControlRoot(); |
| 987 | SmallVector<ISD::OutputArg, 8> Outs; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 988 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 989 | if (!FuncInfo.CanLowerReturn) { |
| 990 | unsigned DemoteReg = FuncInfo.DemoteRegister; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 991 | const Function *F = I.getParent()->getParent(); |
| 992 | |
| 993 | // Emit a store of the return value through the virtual register. |
| 994 | // Leave Outs empty so that LowerReturn won't try to load return |
| 995 | // registers the usual way. |
| 996 | SmallVector<EVT, 1> PtrValueVTs; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 997 | ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 998 | PtrValueVTs); |
| 999 | |
| 1000 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 1001 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1002 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1003 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1004 | SmallVector<uint64_t, 4> Offsets; |
| 1005 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1006 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1007 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1008 | SmallVector<SDValue, 4> Chains(NumValues); |
| 1009 | EVT PtrVT = PtrValueVTs[0]; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1010 | for (unsigned i = 0; i != NumValues; ++i) { |
| 1011 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr, |
| 1012 | DAG.getConstant(Offsets[i], PtrVT)); |
| 1013 | Chains[i] = |
| 1014 | DAG.getStore(Chain, getCurDebugLoc(), |
| 1015 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1016 | Add, NULL, Offsets[i], false, false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1019 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 1020 | MVT::Other, &Chains[0], NumValues); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 1021 | } else if (I.getNumOperands() != 0) { |
| 1022 | SmallVector<EVT, 4> ValueVTs; |
| 1023 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs); |
| 1024 | unsigned NumValues = ValueVTs.size(); |
| 1025 | if (NumValues) { |
| 1026 | SDValue RetOp = getValue(I.getOperand(0)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1027 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 1028 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1029 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1030 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1031 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1032 | const Function *F = I.getParent()->getParent(); |
| 1033 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 1034 | ExtendKind = ISD::SIGN_EXTEND; |
| 1035 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 1036 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1037 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1038 | // FIXME: C calling convention requires the return type to be promoted |
| 1039 | // 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] | 1040 | // conventions. The frontend should mark functions whose return values |
| 1041 | // require promoting with signext or zeroext attributes. |
| 1042 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) { |
| 1043 | EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32); |
| 1044 | if (VT.bitsLT(MinVT)) |
| 1045 | VT = MinVT; |
| 1046 | } |
| 1047 | |
| 1048 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
| 1049 | EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
| 1050 | SmallVector<SDValue, 4> Parts(NumParts); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 1051 | getCopyToParts(DAG, getCurDebugLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1052 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
| 1053 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 1054 | |
| 1055 | // 'inreg' on function refers to return value |
| 1056 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
| 1057 | if (F->paramHasAttr(0, Attribute::InReg)) |
| 1058 | Flags.setInReg(); |
| 1059 | |
| 1060 | // Propagate extension type if any |
| 1061 | if (F->paramHasAttr(0, Attribute::SExt)) |
| 1062 | Flags.setSExt(); |
| 1063 | else if (F->paramHasAttr(0, Attribute::ZExt)) |
| 1064 | Flags.setZExt(); |
| 1065 | |
| 1066 | for (unsigned i = 0; i < NumParts; ++i) |
| 1067 | Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true)); |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1068 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1071 | |
| 1072 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1073 | CallingConv::ID CallConv = |
| 1074 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1075 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
| 1076 | Outs, getCurDebugLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1079 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1080 | "LowerReturn didn't return a valid chain!"); |
| 1081 | |
| 1082 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1083 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1086 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1087 | /// created for it, emit nodes to copy the value into the virtual |
| 1088 | /// registers. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1089 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) { |
Dan Gohman | 33b7a29 | 2010-04-16 17:15:02 +0000 | [diff] [blame] | 1090 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1091 | if (VMI != FuncInfo.ValueMap.end()) { |
| 1092 | assert(!V->use_empty() && "Unused value assigned virtual registers!"); |
| 1093 | CopyValueToVirtualRegister(V, VMI->second); |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1097 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1098 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1099 | /// CopyTo/FromReg. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1100 | void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1101 | // No need to export constants. |
| 1102 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1103 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1104 | // Already exported? |
| 1105 | if (FuncInfo.isExportedInst(V)) return; |
| 1106 | |
| 1107 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1108 | CopyValueToVirtualRegister(V, Reg); |
| 1109 | } |
| 1110 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1111 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1112 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1113 | // The operands of the setcc have to be in this block. We don't know |
| 1114 | // how to export them from some other block. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1115 | if (const Instruction *VI = dyn_cast<Instruction>(V)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1116 | // Can export from current BB. |
| 1117 | if (VI->getParent() == FromBB) |
| 1118 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1119 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1120 | // Is already exported, noop. |
| 1121 | return FuncInfo.isExportedInst(V); |
| 1122 | } |
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 | // If this is an argument, we can export it if the BB is the entry block or |
| 1125 | // if it is already exported. |
| 1126 | if (isa<Argument>(V)) { |
| 1127 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1128 | return true; |
| 1129 | |
| 1130 | // Otherwise, can only export this if it is already exported. |
| 1131 | return FuncInfo.isExportedInst(V); |
| 1132 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1133 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1134 | // Otherwise, constants can always be exported. |
| 1135 | return true; |
| 1136 | } |
| 1137 | |
| 1138 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1139 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1140 | return I->getParent() == BB; |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1144 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1145 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1146 | /// AND operator tree. |
| 1147 | /// |
| 1148 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1149 | SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1150 | MachineBasicBlock *TBB, |
| 1151 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1152 | MachineBasicBlock *CurBB, |
| 1153 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1154 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1155 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1156 | // If the leaf of the tree is a comparison, merge the condition into |
| 1157 | // the caseblock. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1158 | if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1159 | // The operands of the cmp have to be in this block. We don't know |
| 1160 | // how to export them from some other block. If this is the first block |
| 1161 | // of the sequence, no exporting is needed. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1162 | if (CurBB == SwitchBB || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1163 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1164 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1165 | ISD::CondCode Condition; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1166 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1167 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1168 | } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1169 | Condition = getFCmpCondCode(FC->getPredicate()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1170 | } else { |
| 1171 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1172 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1173 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1174 | |
| 1175 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1176 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1177 | SwitchCases.push_back(CB); |
| 1178 | return; |
| 1179 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1183 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1184 | NULL, TBB, FBB, CurBB); |
| 1185 | SwitchCases.push_back(CB); |
| 1186 | } |
| 1187 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1188 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1189 | void SelectionDAGBuilder::FindMergedConditions(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1190 | MachineBasicBlock *TBB, |
| 1191 | MachineBasicBlock *FBB, |
| 1192 | MachineBasicBlock *CurBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1193 | MachineBasicBlock *SwitchBB, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1194 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1195 | // If this node is not part of the or/and tree, emit it as a branch. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1196 | const Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1197 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1198 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1199 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1200 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1201 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1202 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1203 | return; |
| 1204 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1205 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1206 | // Create TmpBB after CurBB. |
| 1207 | MachineFunction::iterator BBI = CurBB; |
| 1208 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1209 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1210 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1211 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1212 | if (Opc == Instruction::Or) { |
| 1213 | // Codegen X | Y as: |
| 1214 | // jmp_if_X TBB |
| 1215 | // jmp TmpBB |
| 1216 | // TmpBB: |
| 1217 | // jmp_if_Y TBB |
| 1218 | // jmp FBB |
| 1219 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1220 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1221 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1222 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1223 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1224 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1225 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1226 | } else { |
| 1227 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1228 | // Codegen X & Y as: |
| 1229 | // jmp_if_X TmpBB |
| 1230 | // jmp FBB |
| 1231 | // TmpBB: |
| 1232 | // jmp_if_Y TBB |
| 1233 | // jmp FBB |
| 1234 | // |
| 1235 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1236 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1237 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1238 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1239 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1240 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1241 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1246 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1247 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1248 | bool |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1249 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1250 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1251 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1252 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1253 | // will get folded into a single comparison, so don't emit two blocks. |
| 1254 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1255 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1256 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1257 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1258 | return false; |
| 1259 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1260 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1261 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1262 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1263 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1264 | Cases[0].CC == Cases[1].CC && |
| 1265 | isa<Constant>(Cases[0].CmpRHS) && |
| 1266 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1267 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1268 | return false; |
| 1269 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1270 | return false; |
| 1271 | } |
| 1272 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1273 | return true; |
| 1274 | } |
| 1275 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1276 | void SelectionDAGBuilder::visitBr(const BranchInst &I) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1277 | MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()]; |
| 1278 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1279 | // Update machine-CFG edges. |
| 1280 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1281 | |
| 1282 | // Figure out which block is immediately after the current one. |
| 1283 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1284 | MachineFunction::iterator BBI = BrMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1285 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1286 | NextBlock = BBI; |
| 1287 | |
| 1288 | if (I.isUnconditional()) { |
| 1289 | // Update machine-CFG edges. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1290 | BrMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1291 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1292 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1293 | if (Succ0MBB != NextBlock) |
| 1294 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1295 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1296 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1297 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | // If this condition is one of the special cases we handle, do special stuff |
| 1302 | // now. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1303 | const Value *CondVal = I.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1304 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1305 | |
| 1306 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1307 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1308 | // For example, instead of something like: |
| 1309 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1310 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1311 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1312 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1313 | // or C, F |
| 1314 | // jnz foo |
| 1315 | // Emit: |
| 1316 | // cmp A, B |
| 1317 | // je foo |
| 1318 | // cmp D, E |
| 1319 | // jle foo |
| 1320 | // |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1321 | if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1322 | if (BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1323 | (BOp->getOpcode() == Instruction::And || |
| 1324 | BOp->getOpcode() == Instruction::Or)) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1325 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, |
| 1326 | BOp->getOpcode()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1327 | // If the compares in later blocks need to use values not currently |
| 1328 | // exported from this block, export them now. This block should always |
| 1329 | // be the first entry. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1330 | assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1331 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1332 | // Allow some cases to be rejected. |
| 1333 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1334 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1335 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1336 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1337 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1338 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1339 | // Emit the branch for this block. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1340 | visitSwitchCase(SwitchCases[0], BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1341 | SwitchCases.erase(SwitchCases.begin()); |
| 1342 | return; |
| 1343 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1344 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1345 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1346 | // SwitchCases. |
| 1347 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1348 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1349 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1350 | SwitchCases.clear(); |
| 1351 | } |
| 1352 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1353 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1354 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1355 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1356 | NULL, Succ0MBB, Succ1MBB, BrMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1357 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1358 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1359 | // cond branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1360 | visitSwitchCase(CB, BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1364 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1365 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB, |
| 1366 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1367 | SDValue Cond; |
| 1368 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1369 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1370 | |
| 1371 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1372 | if (CB.CmpMHS == NULL) { |
| 1373 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1374 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1375 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1376 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1377 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1378 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1379 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1380 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1381 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1382 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1383 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1384 | } else { |
| 1385 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1386 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1387 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1388 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1389 | |
| 1390 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1391 | EVT VT = CmpOp.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1392 | |
| 1393 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1394 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1395 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1396 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1397 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1398 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1399 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1400 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1401 | } |
| 1402 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1403 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1404 | // Update successor info |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1405 | SwitchBB->addSuccessor(CB.TrueBB); |
| 1406 | SwitchBB->addSuccessor(CB.FalseBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1407 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1408 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1409 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1410 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1411 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1412 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1413 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1414 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1415 | // If the lhs block is the next block, invert the condition so that we can |
| 1416 | // fall through to the lhs instead of the rhs block. |
| 1417 | if (CB.TrueBB == NextBlock) { |
| 1418 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1419 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1420 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1421 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1422 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1423 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1424 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1425 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1426 | |
Dan Gohman | deca052 | 2010-06-24 17:08:31 +0000 | [diff] [blame] | 1427 | // Insert the false branch. |
| 1428 | if (CB.FalseBB != NextBlock) |
| 1429 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1430 | DAG.getBasicBlock(CB.FalseBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1431 | |
| 1432 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1436 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1437 | // Emit the code for the jump table |
| 1438 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1439 | EVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1440 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1441 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1442 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1443 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
| 1444 | MVT::Other, Index.getValue(1), |
| 1445 | Table, Index); |
| 1446 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1450 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1451 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1452 | JumpTableHeader &JTH, |
| 1453 | MachineBasicBlock *SwitchBB) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1454 | // Subtract the lowest switch case value from the value being switched on and |
| 1455 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1456 | // difference between smallest and largest cases. |
| 1457 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1458 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1459 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1460 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1461 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1462 | // 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] | 1463 | // 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] | 1464 | // can be used as an index into the jump table in a subsequent basic block. |
| 1465 | // This value may be smaller or larger than the target's pointer type, and |
| 1466 | // therefore require extension or truncating. |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1467 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1468 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1469 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1470 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1471 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1472 | JT.Reg = JumpTableReg; |
| 1473 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1474 | // Emit the range check for the jump table, and branch to the default block |
| 1475 | // for the switch statement if the value being switched on exceeds the largest |
| 1476 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1477 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1478 | TLI.getSetCCResultType(Sub.getValueType()), Sub, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1479 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1480 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1481 | |
| 1482 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1483 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1484 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1485 | MachineFunction::iterator BBI = SwitchBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1486 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1487 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1488 | NextBlock = BBI; |
| 1489 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1490 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1491 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1492 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1493 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1494 | if (JT.MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1495 | BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
| 1496 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1497 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1498 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1502 | /// suitable for "bit tests" |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1503 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B, |
| 1504 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1505 | // Subtract the minimum value |
| 1506 | SDValue SwitchOp = getValue(B.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1507 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1508 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1509 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1510 | |
| 1511 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1512 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1513 | TLI.getSetCCResultType(Sub.getValueType()), |
| 1514 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1515 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1516 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1517 | SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), |
| 1518 | TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1519 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1520 | B.Reg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1521 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1522 | B.Reg, ShiftOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1523 | |
| 1524 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1525 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1526 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1527 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1528 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1529 | NextBlock = BBI; |
| 1530 | |
| 1531 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1532 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1533 | SwitchBB->addSuccessor(B.Default); |
| 1534 | SwitchBB->addSuccessor(MBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1535 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1536 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1537 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1538 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1539 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1540 | if (MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1541 | BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
| 1542 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1543 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1544 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /// visitBitTestCase - this function produces one "bit test" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1548 | void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1549 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1550 | BitTestCase &B, |
| 1551 | MachineBasicBlock *SwitchBB) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1552 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1553 | TLI.getPointerTy()); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1554 | SDValue Cmp; |
| 1555 | if (CountPopulation_64(B.Mask) == 1) { |
| 1556 | // Testing for a single bit; just compare the shift count with what it |
| 1557 | // would need to be to shift a 1 bit in that position. |
| 1558 | Cmp = DAG.getSetCC(getCurDebugLoc(), |
| 1559 | TLI.getSetCCResultType(ShiftOp.getValueType()), |
| 1560 | ShiftOp, |
| 1561 | DAG.getConstant(CountTrailingZeros_64(B.Mask), |
| 1562 | TLI.getPointerTy()), |
| 1563 | ISD::SETEQ); |
| 1564 | } else { |
| 1565 | // Make desired shift |
| 1566 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
| 1567 | TLI.getPointerTy(), |
| 1568 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1569 | ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1570 | |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1571 | // Emit bit tests and jumps |
| 1572 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
| 1573 | TLI.getPointerTy(), SwitchVal, |
| 1574 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
| 1575 | Cmp = DAG.getSetCC(getCurDebugLoc(), |
| 1576 | TLI.getSetCCResultType(AndOp.getValueType()), |
| 1577 | AndOp, DAG.getConstant(0, TLI.getPointerTy()), |
| 1578 | ISD::SETNE); |
| 1579 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1580 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1581 | SwitchBB->addSuccessor(B.TargetBB); |
| 1582 | SwitchBB->addSuccessor(NextMBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1583 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1584 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1585 | MVT::Other, getControlRoot(), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1586 | Cmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1587 | |
| 1588 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1589 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1590 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1591 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1592 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1593 | NextBlock = BBI; |
| 1594 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1595 | if (NextMBB != NextBlock) |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1596 | BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
| 1597 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1598 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1599 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1602 | void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1603 | MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()]; |
| 1604 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1605 | // Retrieve successors. |
| 1606 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1607 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1608 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1609 | const Value *Callee(I.getCalledValue()); |
| 1610 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1611 | visitInlineAsm(&I); |
| 1612 | else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1613 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1614 | |
| 1615 | // If the value of the invoke is used outside of its defining block, make it |
| 1616 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1617 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1618 | |
| 1619 | // Update successor info |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1620 | InvokeMBB->addSuccessor(Return); |
| 1621 | InvokeMBB->addSuccessor(LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1622 | |
| 1623 | // Drop into normal successor. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1624 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1625 | MVT::Other, getControlRoot(), |
| 1626 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1629 | void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1633 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1634 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1635 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1636 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1637 | MachineBasicBlock *Default, |
| 1638 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1639 | Case& BackCase = *(CR.Range.second-1); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1640 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1641 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1642 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1643 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1644 | return false; |
| 1645 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1646 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1647 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1648 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1649 | |
| 1650 | // Figure out which block is immediately after the current one. |
| 1651 | MachineBasicBlock *NextBlock = 0; |
| 1652 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1653 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1654 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1655 | NextBlock = BBI; |
| 1656 | |
| 1657 | // TODO: If any two of the cases has the same destination, and if one value |
| 1658 | // is the same as the other, but has one bit unset that the other has set, |
| 1659 | // use bit manipulation to do two compares at once. For example: |
| 1660 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1661 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1662 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1663 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1664 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1665 | // branches in this order. See if rearranging a case value would help. |
| 1666 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1667 | if (I->BB == NextBlock) { |
| 1668 | std::swap(*I, BackCase); |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
| 1672 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1673 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1674 | // Create a CaseBlock record representing a conditional branch to |
| 1675 | // the Case's target mbb if the value being switched on SV is equal |
| 1676 | // to C. |
| 1677 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1678 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1679 | MachineBasicBlock *FallThrough; |
| 1680 | if (I != E-1) { |
| 1681 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1682 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1683 | |
| 1684 | // Put SV in a virtual register to make it available from the new blocks. |
| 1685 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1686 | } else { |
| 1687 | // If the last case doesn't match, go to the default block. |
| 1688 | FallThrough = Default; |
| 1689 | } |
| 1690 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1691 | const Value *RHS, *LHS, *MHS; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1692 | ISD::CondCode CC; |
| 1693 | if (I->High == I->Low) { |
| 1694 | // This is just small small case range :) containing exactly 1 case |
| 1695 | CC = ISD::SETEQ; |
| 1696 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1697 | } else { |
| 1698 | CC = ISD::SETLE; |
| 1699 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1700 | } |
| 1701 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1702 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1703 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1704 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1705 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1706 | // before the next MBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1707 | if (CurBlock == SwitchBB) |
| 1708 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1709 | else |
| 1710 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1711 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1712 | CurBlock = FallThrough; |
| 1713 | } |
| 1714 | |
| 1715 | return true; |
| 1716 | } |
| 1717 | |
| 1718 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1719 | return !DisableJumpTables && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1720 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1721 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1722 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1723 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1724 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
| 1725 | APInt LastExt(Last), FirstExt(First); |
| 1726 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
| 1727 | LastExt.sext(BitWidth); FirstExt.sext(BitWidth); |
| 1728 | return (LastExt - FirstExt + 1ULL); |
| 1729 | } |
| 1730 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1731 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1732 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR, |
| 1733 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1734 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1735 | MachineBasicBlock* Default, |
| 1736 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1737 | Case& FrontCase = *CR.Range.first; |
| 1738 | Case& BackCase = *(CR.Range.second-1); |
| 1739 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1740 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1741 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1742 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1743 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1744 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1745 | I!=E; ++I) |
| 1746 | TSize += I->size(); |
| 1747 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 1748 | if (!areJTsAllowed(TLI) || TSize.ult(4)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1749 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1750 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1751 | APInt Range = ComputeRange(First, Last); |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1752 | double Density = TSize.roundToDouble() / Range.roundToDouble(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1753 | if (Density < 0.4) |
| 1754 | return false; |
| 1755 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1756 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1757 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
| 1758 | << "Range: " << Range |
| 1759 | << "Size: " << TSize << ". Density: " << Density << "\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1760 | |
| 1761 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1762 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1763 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1764 | |
| 1765 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1766 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1767 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1768 | |
| 1769 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1770 | |
| 1771 | // Create a new basic block to hold the code for loading the address |
| 1772 | // of the jump table, and jumping to it. Update successor information; |
| 1773 | // we will either branch to the default case for the switch, or the jump |
| 1774 | // table. |
| 1775 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1776 | CurMF->insert(BBI, JumpTableBB); |
| 1777 | CR.CaseBB->addSuccessor(Default); |
| 1778 | CR.CaseBB->addSuccessor(JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1779 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1780 | // Build a vector of destination BBs, corresponding to each target |
| 1781 | // of the jump table. If the value of the jump table slot corresponds to |
| 1782 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1783 | // the default BB. |
| 1784 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1785 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1786 | 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] | 1787 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 1788 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1789 | |
| 1790 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1791 | DestBBs.push_back(I->BB); |
| 1792 | if (TEI==High) |
| 1793 | ++I; |
| 1794 | } else { |
| 1795 | DestBBs.push_back(Default); |
| 1796 | } |
| 1797 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1798 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1799 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1800 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1801 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1802 | E = DestBBs.end(); I != E; ++I) { |
| 1803 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1804 | SuccsHandled[(*I)->getNumber()] = true; |
| 1805 | JumpTableBB->addSuccessor(*I); |
| 1806 | } |
| 1807 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1808 | |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 1809 | // Create a jump table index for this jump table. |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1810 | unsigned JTEncoding = TLI.getJumpTableEncoding(); |
| 1811 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 1812 | ->createJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1813 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1814 | // Set the jump table information so that we can codegen it as a second |
| 1815 | // MachineBasicBlock |
| 1816 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1817 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB)); |
| 1818 | if (CR.CaseBB == SwitchBB) |
| 1819 | visitJumpTableHeader(JT, JTH, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1820 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1821 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1822 | |
| 1823 | return true; |
| 1824 | } |
| 1825 | |
| 1826 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1827 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1828 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 1829 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1830 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1831 | MachineBasicBlock *Default, |
| 1832 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1833 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1834 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1835 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1836 | |
| 1837 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1838 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 1839 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1840 | |
| 1841 | Case& FrontCase = *CR.Range.first; |
| 1842 | Case& BackCase = *(CR.Range.second-1); |
| 1843 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1844 | |
| 1845 | // Size is the number of Cases represented by this range. |
| 1846 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1847 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1848 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1849 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1850 | double FMetric = 0; |
| 1851 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1852 | |
| 1853 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1854 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1855 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1856 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1857 | I!=E; ++I) |
| 1858 | TSize += I->size(); |
| 1859 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1860 | APInt LSize = FrontCase.size(); |
| 1861 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1862 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1863 | << "First: " << First << ", Last: " << Last <<'\n' |
| 1864 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1865 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1866 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1867 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 1868 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1869 | APInt Range = ComputeRange(LEnd, RBegin); |
| 1870 | assert((Range - 2ULL).isNonNegative() && |
| 1871 | "Invalid case distance"); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1872 | double LDensity = (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 1873 | (LEnd - First + 1ULL).roundToDouble(); |
| 1874 | double RDensity = (double)RSize.roundToDouble() / |
| 1875 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1876 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1877 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1878 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 1879 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 1880 | << "LDensity: " << LDensity |
| 1881 | << ", RDensity: " << RDensity << '\n' |
| 1882 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1883 | if (FMetric < Metric) { |
| 1884 | Pivot = J; |
| 1885 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1886 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | LSize += J->size(); |
| 1890 | RSize -= J->size(); |
| 1891 | } |
| 1892 | if (areJTsAllowed(TLI)) { |
| 1893 | // If our case is dense we *really* should handle it earlier! |
| 1894 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1895 | } else { |
| 1896 | Pivot = CR.Range.first + Size/2; |
| 1897 | } |
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 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1900 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1901 | Constant *C = Pivot->Low; |
| 1902 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1903 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1904 | // 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] | 1905 | // 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] | 1906 | // 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] | 1907 | // 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] | 1908 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1909 | // rather than creating a leaf node for it. |
| 1910 | if ((LHSR.second - LHSR.first) == 1 && |
| 1911 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1912 | cast<ConstantInt>(C)->getValue() == |
| 1913 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1914 | TrueBB = LHSR.first->BB; |
| 1915 | } else { |
| 1916 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1917 | CurMF->insert(BBI, TrueBB); |
| 1918 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1919 | |
| 1920 | // Put SV in a virtual register to make it available from the new blocks. |
| 1921 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1922 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1923 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1924 | // Similar to the optimization above, if the Value being switched on is |
| 1925 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1926 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1927 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1928 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1929 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 1930 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1931 | FalseBB = RHSR.first->BB; |
| 1932 | } else { |
| 1933 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1934 | CurMF->insert(BBI, FalseBB); |
| 1935 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 1936 | |
| 1937 | // Put SV in a virtual register to make it available from the new blocks. |
| 1938 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1942 | // 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] | 1943 | // Otherwise, branch to LHS. |
| 1944 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1945 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1946 | if (CR.CaseBB == SwitchBB) |
| 1947 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1948 | else |
| 1949 | SwitchCases.push_back(CB); |
| 1950 | |
| 1951 | return true; |
| 1952 | } |
| 1953 | |
| 1954 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1955 | /// range span less, than machine word bitwidth, encode case range into series |
| 1956 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1957 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 1958 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1959 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1960 | MachineBasicBlock* Default, |
| 1961 | MachineBasicBlock *SwitchBB){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1962 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 1963 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1964 | |
| 1965 | Case& FrontCase = *CR.Range.first; |
| 1966 | Case& BackCase = *(CR.Range.second-1); |
| 1967 | |
| 1968 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1969 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1970 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1971 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 1972 | // If target does not have legal shift left, do not emit bit tests at all. |
| 1973 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 1974 | return false; |
| 1975 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1976 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1977 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1978 | I!=E; ++I) { |
| 1979 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1980 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1981 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1982 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1983 | // Count unique destinations |
| 1984 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1985 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1986 | Dests.insert(I->BB); |
| 1987 | if (Dests.size() > 3) |
| 1988 | // Don't bother the code below, if there are too much unique destinations |
| 1989 | return false; |
| 1990 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 1991 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1992 | << Dests.size() << '\n' |
| 1993 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1994 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1995 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1996 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 1997 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 1998 | APInt cmpRange = maxValue - minValue; |
| 1999 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2000 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2001 | << "Low bound: " << minValue << '\n' |
| 2002 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2003 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 2004 | if (cmpRange.uge(IntPtrBits) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2005 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 2006 | !(Dests.size() == 2 && numCmps >= 5) && |
| 2007 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 2008 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2009 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2010 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2011 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 2012 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2013 | // Optimize the case where all the case values fit in a |
| 2014 | // word without having to subtract minValue. In this case, |
| 2015 | // we can optimize away the subtraction. |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 2016 | if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2017 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2018 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2019 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2020 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2021 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2022 | CaseBitsVector CasesBits; |
| 2023 | unsigned i, count = 0; |
| 2024 | |
| 2025 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2026 | MachineBasicBlock* Dest = I->BB; |
| 2027 | for (i = 0; i < count; ++i) |
| 2028 | if (Dest == CasesBits[i].BB) |
| 2029 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2030 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2031 | if (i == count) { |
| 2032 | assert((count < 3) && "Too much destinations to test!"); |
| 2033 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 2034 | count++; |
| 2035 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2036 | |
| 2037 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 2038 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 2039 | |
| 2040 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 2041 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
| 2042 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2043 | for (uint64_t j = lo; j <= hi; j++) { |
| 2044 | CasesBits[i].Mask |= 1ULL << j; |
| 2045 | CasesBits[i].Bits++; |
| 2046 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2047 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2048 | } |
| 2049 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2050 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2051 | BitTestInfo BTC; |
| 2052 | |
| 2053 | // Figure out which block is immediately after the current one. |
| 2054 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2055 | ++BBI; |
| 2056 | |
| 2057 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2058 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2059 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2060 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2061 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2062 | << ", Bits: " << CasesBits[i].Bits |
| 2063 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2064 | |
| 2065 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2066 | CurMF->insert(BBI, CaseBB); |
| 2067 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2068 | CaseBB, |
| 2069 | CasesBits[i].BB)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2070 | |
| 2071 | // Put SV in a virtual register to make it available from the new blocks. |
| 2072 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2073 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2074 | |
| 2075 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2076 | -1U, (CR.CaseBB == SwitchBB), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2077 | CR.CaseBB, Default, BTC); |
| 2078 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2079 | if (CR.CaseBB == SwitchBB) |
| 2080 | visitBitTestHeader(BTB, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2081 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2082 | BitTestCases.push_back(BTB); |
| 2083 | |
| 2084 | return true; |
| 2085 | } |
| 2086 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2087 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2088 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 2089 | const SwitchInst& SI) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2090 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2091 | |
| 2092 | // Start with "simple" cases |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2093 | for (size_t i = 1; i < SI.getNumSuccessors(); ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2094 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 2095 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 2096 | SI.getSuccessorValue(i), |
| 2097 | SMBB)); |
| 2098 | } |
| 2099 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 2100 | |
| 2101 | // Merge case into clusters |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2102 | if (Cases.size() >= 2) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2103 | // Must recompute end() each iteration because it may be |
| 2104 | // invalidated by erase if we hold on to it |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2105 | for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) { |
| 2106 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 2107 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2108 | MachineBasicBlock* nextBB = J->BB; |
| 2109 | MachineBasicBlock* currentBB = I->BB; |
| 2110 | |
| 2111 | // If the two neighboring cases go to the same destination, merge them |
| 2112 | // into a single case. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2113 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2114 | I->High = J->High; |
| 2115 | J = Cases.erase(J); |
| 2116 | } else { |
| 2117 | I = J++; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 2122 | if (I->Low != I->High) |
| 2123 | // A range counts double, since it requires two compares. |
| 2124 | ++numCmps; |
| 2125 | } |
| 2126 | |
| 2127 | return numCmps; |
| 2128 | } |
| 2129 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2130 | void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2131 | MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()]; |
| 2132 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2133 | // Figure out which block is immediately after the current one. |
| 2134 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2135 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2136 | |
| 2137 | // If there is only the default destination, branch to it if it is not the |
| 2138 | // next basic block. Otherwise, just fall through. |
| 2139 | if (SI.getNumOperands() == 2) { |
| 2140 | // Update machine-CFG edges. |
| 2141 | |
| 2142 | // If this is not a fall-through branch, emit the branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2143 | SwitchMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2144 | if (Default != NextBlock) |
| 2145 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 2146 | MVT::Other, getControlRoot(), |
| 2147 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2148 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2149 | return; |
| 2150 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2151 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2152 | // If there are any non-default case statements, create a vector of Cases |
| 2153 | // representing each one, and sort the vector so that we can efficiently |
| 2154 | // create a binary search tree from them. |
| 2155 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2156 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2157 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2158 | << ". Total compares: " << numCmps << '\n'); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 2159 | numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2160 | |
| 2161 | // Get the Value to be switched on and default basic blocks, which will be |
| 2162 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2163 | // search tree. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2164 | const Value *SV = SI.getOperand(0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2165 | |
| 2166 | // Push the initial CaseRec onto the worklist |
| 2167 | CaseRecVector WorkList; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2168 | WorkList.push_back(CaseRec(SwitchMBB,0,0, |
| 2169 | CaseRange(Cases.begin(),Cases.end()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2170 | |
| 2171 | while (!WorkList.empty()) { |
| 2172 | // Grab a record representing a case range to process off the worklist |
| 2173 | CaseRec CR = WorkList.back(); |
| 2174 | WorkList.pop_back(); |
| 2175 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2176 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2177 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2178 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2179 | // If the range has few cases (two or less) emit a series of specific |
| 2180 | // tests. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2181 | if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2182 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2183 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2184 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 2185 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2186 | // lowering the switch to a binary tree of conditional branches. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2187 | if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2188 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2189 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2190 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2191 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2192 | handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2193 | } |
| 2194 | } |
| 2195 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2196 | void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2197 | MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()]; |
| 2198 | |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2199 | // Update machine-CFG edges with unique successors. |
Jakob Stoklund Olesen | b5b90ed | 2010-02-11 18:06:56 +0000 | [diff] [blame] | 2200 | SmallVector<BasicBlock*, 32> succs; |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2201 | succs.reserve(I.getNumSuccessors()); |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2202 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2203 | succs.push_back(I.getSuccessor(i)); |
Jakob Stoklund Olesen | b5b90ed | 2010-02-11 18:06:56 +0000 | [diff] [blame] | 2204 | array_pod_sort(succs.begin(), succs.end()); |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2205 | succs.erase(std::unique(succs.begin(), succs.end()), succs.end()); |
| 2206 | for (unsigned i = 0, e = succs.size(); i != e; ++i) |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2207 | IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]); |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2208 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2209 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2210 | MVT::Other, getControlRoot(), |
| 2211 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2212 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2213 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2214 | void SelectionDAGBuilder::visitFSub(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2215 | // -0.0 - X --> fneg |
| 2216 | const Type *Ty = I.getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2217 | if (Ty->isVectorTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2218 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2219 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2220 | const Type *ElTy = DestTy->getElementType(); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2221 | unsigned VL = DestTy->getNumElements(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2222 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2223 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2224 | if (CV == CNZ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2225 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2226 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2227 | Op2.getValueType(), Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2228 | return; |
| 2229 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2230 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2231 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2232 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2233 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2234 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2235 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2236 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2237 | Op2.getValueType(), Op2)); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2238 | return; |
| 2239 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2240 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2241 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2244 | void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2245 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2246 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2247 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
| 2248 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2251 | void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2252 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2253 | SDValue Op2 = getValue(I.getOperand(1)); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2254 | if (!I.getType()->isVectorTy() && |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2255 | Op2.getValueType() != TLI.getShiftAmountTy()) { |
| 2256 | // If the operand is smaller than the shift count type, promote it. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2257 | EVT PTy = TLI.getPointerTy(); |
| 2258 | EVT STy = TLI.getShiftAmountTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2259 | if (STy.bitsGT(Op2.getValueType())) |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2260 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
| 2261 | TLI.getShiftAmountTy(), Op2); |
| 2262 | // If the operand is larger than the shift count type but the shift |
| 2263 | // count type has enough bits to represent any shift value, truncate |
| 2264 | // it now. This is a common case and it exposes the truncate to |
| 2265 | // optimization early. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2266 | else if (STy.getSizeInBits() >= |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2267 | Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2268 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2269 | TLI.getShiftAmountTy(), Op2); |
| 2270 | // Otherwise we'll need to temporarily settle for some other |
| 2271 | // convenient type; type legalization will make adjustments as |
| 2272 | // needed. |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2273 | else if (PTy.bitsLT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2274 | Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2275 | TLI.getPointerTy(), Op2); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2276 | else if (PTy.bitsGT(Op2.getValueType())) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2277 | Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2278 | TLI.getPointerTy(), Op2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2279 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2280 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2281 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
| 2282 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2285 | void SelectionDAGBuilder::visitICmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2286 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2287 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2288 | predicate = IC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2289 | else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2290 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2291 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2292 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2293 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2294 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2295 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2296 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2299 | void SelectionDAGBuilder::visitFCmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2300 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2301 | if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2302 | predicate = FC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2303 | else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2304 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2305 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2306 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2307 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2308 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2309 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2312 | void SelectionDAGBuilder::visitSelect(const User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2313 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2314 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2315 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2316 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2317 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2318 | SmallVector<SDValue, 4> Values(NumValues); |
| 2319 | SDValue Cond = getValue(I.getOperand(0)); |
| 2320 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2321 | SDValue FalseVal = getValue(I.getOperand(2)); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2322 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2323 | for (unsigned i = 0; i != NumValues; ++i) |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2324 | Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(), |
Chris Lattner | b3e87b2 | 2010-03-12 07:15:36 +0000 | [diff] [blame] | 2325 | TrueVal.getNode()->getValueType(TrueVal.getResNo()+i), |
| 2326 | Cond, |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2327 | SDValue(TrueVal.getNode(), |
| 2328 | TrueVal.getResNo() + i), |
| 2329 | SDValue(FalseVal.getNode(), |
| 2330 | FalseVal.getResNo() + i)); |
| 2331 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2332 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2333 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2334 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2335 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2336 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2337 | void SelectionDAGBuilder::visitTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2338 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2339 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2340 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2341 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2344 | void SelectionDAGBuilder::visitZExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2345 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2346 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2347 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2348 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2349 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2352 | void SelectionDAGBuilder::visitSExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2353 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2354 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2355 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2356 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2357 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2360 | void SelectionDAGBuilder::visitFPTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2361 | // FPTrunc is never a no-op cast, no need to check |
| 2362 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2363 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2364 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
| 2365 | DestVT, N, DAG.getIntPtrConstant(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2368 | void SelectionDAGBuilder::visitFPExt(const User &I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2369 | // FPTrunc is never a no-op cast, no need to check |
| 2370 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2371 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2372 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2375 | void SelectionDAGBuilder::visitFPToUI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2376 | // FPToUI is never a no-op cast, no need to check |
| 2377 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2378 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2379 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2382 | void SelectionDAGBuilder::visitFPToSI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2383 | // FPToSI is never a no-op cast, no need to check |
| 2384 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2385 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2386 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2389 | void SelectionDAGBuilder::visitUIToFP(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2390 | // UIToFP is never a no-op cast, no need to check |
| 2391 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2392 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2393 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2396 | void SelectionDAGBuilder::visitSIToFP(const User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2397 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2398 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2399 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2400 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2403 | void SelectionDAGBuilder::visitPtrToInt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2404 | // What to do depends on the size of the integer and the size of the pointer. |
| 2405 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2406 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2407 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2408 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2411 | void SelectionDAGBuilder::visitIntToPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2412 | // What to do depends on the size of the integer and the size of the pointer. |
| 2413 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2414 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2415 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2416 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2419 | void SelectionDAGBuilder::visitBitCast(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2420 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2421 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2422 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2423 | // BitCast assures us that source and destination are the same size so this is |
| 2424 | // either a BIT_CONVERT or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2425 | if (DestVT != N.getValueType()) |
| 2426 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
| 2427 | DestVT, N)); // convert types. |
| 2428 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2429 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2432 | void SelectionDAGBuilder::visitInsertElement(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2433 | SDValue InVec = getValue(I.getOperand(0)); |
| 2434 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2435 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2436 | TLI.getPointerTy(), |
| 2437 | getValue(I.getOperand(2))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2438 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
| 2439 | TLI.getValueType(I.getType()), |
| 2440 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2443 | void SelectionDAGBuilder::visitExtractElement(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2444 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2445 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2446 | TLI.getPointerTy(), |
| 2447 | getValue(I.getOperand(1))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2448 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2449 | TLI.getValueType(I.getType()), InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2452 | // Utility for visitShuffleVector - Returns true if the mask is mask starting |
| 2453 | // from SIndx and increasing to the element length (undefs are allowed). |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2454 | static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) { |
| 2455 | unsigned MaskNumElts = Mask.size(); |
| 2456 | for (unsigned i = 0; i != MaskNumElts; ++i) |
| 2457 | if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2458 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2459 | return true; |
| 2460 | } |
| 2461 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2462 | void SelectionDAGBuilder::visitShuffleVector(const User &I) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2463 | SmallVector<int, 8> Mask; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2464 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2465 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2466 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2467 | // Convert the ConstantVector mask operand into an array of ints, with -1 |
| 2468 | // representing undef values. |
| 2469 | SmallVector<Constant*, 8> MaskElts; |
Chris Lattner | b29d596 | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 2470 | cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2471 | unsigned MaskNumElts = MaskElts.size(); |
| 2472 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2473 | if (isa<UndefValue>(MaskElts[i])) |
| 2474 | Mask.push_back(-1); |
| 2475 | else |
| 2476 | Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue()); |
| 2477 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2478 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2479 | EVT VT = TLI.getValueType(I.getType()); |
| 2480 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2481 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2482 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2483 | if (SrcNumElts == MaskNumElts) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2484 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2485 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2486 | return; |
| 2487 | } |
| 2488 | |
| 2489 | // 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] | 2490 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2491 | // Mask is longer than the source vectors and is a multiple of the source |
| 2492 | // 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] | 2493 | // lengths match. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2494 | if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) { |
| 2495 | // The shuffle is concatenating two vectors together. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2496 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
| 2497 | VT, Src1, Src2)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2498 | return; |
| 2499 | } |
| 2500 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2501 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2502 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2503 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2504 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2505 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2506 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2507 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2508 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2509 | MOps1[0] = Src1; |
| 2510 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2511 | |
| 2512 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2513 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2514 | &MOps1[0], NumConcat); |
| 2515 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2516 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2517 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2518 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2519 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2520 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2521 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2522 | int Idx = Mask[i]; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2523 | if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2524 | MappedOps.push_back(Idx); |
| 2525 | else |
| 2526 | MappedOps.push_back(Idx + MaskNumElts - SrcNumElts); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2527 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2528 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2529 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2530 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2531 | return; |
| 2532 | } |
| 2533 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2534 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2535 | // Analyze the access pattern of the vector to see if we can extract |
| 2536 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2537 | // the range of elements the mask access on both vectors. |
| 2538 | int MinRange[2] = { SrcNumElts+1, SrcNumElts+1}; |
| 2539 | int MaxRange[2] = {-1, -1}; |
| 2540 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2541 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2542 | int Idx = Mask[i]; |
| 2543 | int Input = 0; |
| 2544 | if (Idx < 0) |
| 2545 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2546 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2547 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2548 | Input = 1; |
| 2549 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2550 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2551 | if (Idx > MaxRange[Input]) |
| 2552 | MaxRange[Input] = Idx; |
| 2553 | if (Idx < MinRange[Input]) |
| 2554 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2555 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2556 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2557 | // Check if the access is smaller than the vector size and can we find |
| 2558 | // a reasonable extract index. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2559 | int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not |
| 2560 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2561 | int StartIdx[2]; // StartIdx to extract from |
| 2562 | for (int Input=0; Input < 2; ++Input) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2563 | if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2564 | RangeUse[Input] = 0; // Unused |
| 2565 | StartIdx[Input] = 0; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2566 | } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2567 | // 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] | 2568 | // start index that is a multiple of the mask length. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2569 | if (MaxRange[Input] < (int)MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2570 | RangeUse[Input] = 1; // Extract from beginning of the vector |
| 2571 | StartIdx[Input] = 0; |
| 2572 | } else { |
| 2573 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2574 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2575 | StartIdx[Input] + MaskNumElts < SrcNumElts) |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2576 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2577 | } |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2578 | } |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 2581 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2582 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2583 | return; |
| 2584 | } |
| 2585 | else if (RangeUse[0] < 2 && RangeUse[1] < 2) { |
| 2586 | // Extract appropriate subvector and generate a vector shuffle |
| 2587 | for (int Input=0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2588 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2589 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2590 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2591 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2592 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2593 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2594 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2595 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2596 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2597 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2598 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2599 | int Idx = Mask[i]; |
| 2600 | if (Idx < 0) |
| 2601 | MappedOps.push_back(Idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2602 | else if (Idx < (int)SrcNumElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2603 | MappedOps.push_back(Idx - StartIdx[0]); |
| 2604 | else |
| 2605 | MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2606 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2607 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2608 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2609 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2610 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2611 | } |
| 2612 | } |
| 2613 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2614 | // We can't use either concat vectors or extract subvectors so fall back to |
| 2615 | // replacing the shuffle with extract and build vector. |
| 2616 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2617 | EVT EltVT = VT.getVectorElementType(); |
| 2618 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2619 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2620 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2621 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2622 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2623 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2624 | int Idx = Mask[i]; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2625 | SDValue Res; |
| 2626 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2627 | if (Idx < (int)SrcNumElts) |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2628 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2629 | EltVT, Src1, DAG.getConstant(Idx, PtrVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2630 | else |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2631 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2632 | EltVT, Src2, |
| 2633 | DAG.getConstant(Idx - SrcNumElts, PtrVT)); |
| 2634 | |
| 2635 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2636 | } |
| 2637 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2638 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2639 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 2640 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2643 | void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2644 | const Value *Op0 = I.getOperand(0); |
| 2645 | const Value *Op1 = I.getOperand(1); |
| 2646 | const Type *AggTy = I.getType(); |
| 2647 | const Type *ValTy = Op1->getType(); |
| 2648 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2649 | bool FromUndef = isa<UndefValue>(Op1); |
| 2650 | |
| 2651 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2652 | I.idx_begin(), I.idx_end()); |
| 2653 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2654 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2655 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2656 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2657 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2658 | |
| 2659 | unsigned NumAggValues = AggValueVTs.size(); |
| 2660 | unsigned NumValValues = ValValueVTs.size(); |
| 2661 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2662 | |
| 2663 | SDValue Agg = getValue(Op0); |
| 2664 | SDValue Val = getValue(Op1); |
| 2665 | unsigned i = 0; |
| 2666 | // Copy the beginning value(s) from the original aggregate. |
| 2667 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2668 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2669 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2670 | // Copy values from the inserted value(s). |
| 2671 | for (; i != LinearIndex + NumValValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2672 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2673 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2674 | // Copy remaining value(s) from the original aggregate. |
| 2675 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2676 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2677 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2678 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2679 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2680 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2681 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2684 | void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2685 | const Value *Op0 = I.getOperand(0); |
| 2686 | const Type *AggTy = Op0->getType(); |
| 2687 | const Type *ValTy = I.getType(); |
| 2688 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2689 | |
| 2690 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2691 | I.idx_begin(), I.idx_end()); |
| 2692 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2693 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2694 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2695 | |
| 2696 | unsigned NumValValues = ValValueVTs.size(); |
| 2697 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2698 | |
| 2699 | SDValue Agg = getValue(Op0); |
| 2700 | // Copy out the selected value(s). |
| 2701 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2702 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2703 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2704 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 2705 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2706 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2707 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2708 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2709 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2712 | void SelectionDAGBuilder::visitGetElementPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2713 | SDValue N = getValue(I.getOperand(0)); |
| 2714 | const Type *Ty = I.getOperand(0)->getType(); |
| 2715 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2716 | for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2717 | OI != E; ++OI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2718 | const Value *Idx = *OI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2719 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2720 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2721 | if (Field) { |
| 2722 | // N = N + Offset |
| 2723 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2724 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2725 | DAG.getIntPtrConstant(Offset)); |
| 2726 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2727 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2728 | Ty = StTy->getElementType(Field); |
Chris Lattner | 93b122d | 2010-03-16 21:25:55 +0000 | [diff] [blame] | 2729 | } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) { |
| 2730 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2731 | |
| 2732 | // Offset canonically 0 for unions, but type changes |
| 2733 | Ty = UnTy->getElementType(Field); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2734 | } else { |
| 2735 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2736 | |
| 2737 | // If this is a constant subscript, handle it quickly. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2738 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 2739 | if (CI->isZero()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2740 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2741 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2742 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2743 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2744 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2745 | if (PtrBits < 64) |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 2746 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 2747 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2748 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2749 | else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2750 | OffsVal = DAG.getIntPtrConstant(Offs); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2751 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2752 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 2753 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2754 | continue; |
| 2755 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2756 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2757 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2758 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 2759 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2760 | SDValue IdxN = getValue(Idx); |
| 2761 | |
| 2762 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2763 | // it. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 2764 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2765 | |
| 2766 | // If this is a multiply by a power of two, turn it into a shl |
| 2767 | // immediately. This is a very common case. |
| 2768 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2769 | if (ElementSize.isPowerOf2()) { |
| 2770 | unsigned Amt = ElementSize.logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2771 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2772 | N.getValueType(), IdxN, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2773 | DAG.getConstant(Amt, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2774 | } else { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 2775 | SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2776 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2777 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2781 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2782 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2783 | } |
| 2784 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 2785 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2786 | setValue(&I, N); |
| 2787 | } |
| 2788 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2789 | void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2790 | // If this is a fixed sized alloca in the entry block of the function, |
| 2791 | // allocate it statically on the stack. |
| 2792 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2793 | return; // getValue will auto-populate this. |
| 2794 | |
| 2795 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2796 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2797 | unsigned Align = |
| 2798 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2799 | I.getAlignment()); |
| 2800 | |
| 2801 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2802 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2803 | EVT IntPtr = TLI.getPointerTy(); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 2804 | if (AllocSize.getValueType() != IntPtr) |
| 2805 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
| 2806 | |
| 2807 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr, |
| 2808 | AllocSize, |
| 2809 | DAG.getConstant(TySize, IntPtr)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2810 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2811 | // Handle alignment. If the requested alignment is less than or equal to |
| 2812 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2813 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 2814 | unsigned StackAlign = TM.getFrameInfo()->getStackAlignment(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2815 | if (Align <= StackAlign) |
| 2816 | Align = 0; |
| 2817 | |
| 2818 | // Round the size of the allocation up to the stack alignment size |
| 2819 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2820 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2821 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2822 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2823 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2824 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2825 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2826 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2827 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2828 | |
| 2829 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2830 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2831 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2832 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2833 | setValue(&I, DSA); |
| 2834 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2835 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2836 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2837 | // object. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2838 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2841 | void SelectionDAGBuilder::visitLoad(const LoadInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2842 | const Value *SV = I.getOperand(0); |
| 2843 | SDValue Ptr = getValue(SV); |
| 2844 | |
| 2845 | const Type *Ty = I.getType(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2846 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2847 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2848 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2849 | unsigned Alignment = I.getAlignment(); |
| 2850 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2851 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2852 | SmallVector<uint64_t, 4> Offsets; |
| 2853 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2854 | unsigned NumValues = ValueVTs.size(); |
| 2855 | if (NumValues == 0) |
| 2856 | return; |
| 2857 | |
| 2858 | SDValue Root; |
| 2859 | bool ConstantMemory = false; |
| 2860 | if (I.isVolatile()) |
| 2861 | // Serialize volatile loads with other side effects. |
| 2862 | Root = getRoot(); |
| 2863 | else if (AA->pointsToConstantMemory(SV)) { |
| 2864 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2865 | Root = DAG.getEntryNode(); |
| 2866 | ConstantMemory = true; |
| 2867 | } else { |
| 2868 | // Do not serialize non-volatile loads against each other. |
| 2869 | Root = DAG.getRoot(); |
| 2870 | } |
| 2871 | |
| 2872 | SmallVector<SDValue, 4> Values(NumValues); |
| 2873 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2874 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2875 | for (unsigned i = 0; i != NumValues; ++i) { |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2876 | SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 2877 | PtrVT, Ptr, |
| 2878 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2879 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2880 | A, SV, Offsets[i], isVolatile, |
| 2881 | isNonTemporal, Alignment); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2882 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2883 | Values[i] = L; |
| 2884 | Chains[i] = L.getValue(1); |
| 2885 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2886 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2887 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2888 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2889 | MVT::Other, &Chains[0], NumValues); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2890 | if (isVolatile) |
| 2891 | DAG.setRoot(Chain); |
| 2892 | else |
| 2893 | PendingLoads.push_back(Chain); |
| 2894 | } |
| 2895 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2896 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2897 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2898 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2899 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2900 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2901 | void SelectionDAGBuilder::visitStore(const StoreInst &I) { |
| 2902 | const Value *SrcV = I.getOperand(0); |
| 2903 | const Value *PtrV = I.getOperand(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2904 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2905 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2906 | SmallVector<uint64_t, 4> Offsets; |
| 2907 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2908 | unsigned NumValues = ValueVTs.size(); |
| 2909 | if (NumValues == 0) |
| 2910 | return; |
| 2911 | |
| 2912 | // Get the lowered operands. Note that we do this after |
| 2913 | // checking if NumResults is zero, because with zero results |
| 2914 | // the operands won't have values in the map. |
| 2915 | SDValue Src = getValue(SrcV); |
| 2916 | SDValue Ptr = getValue(PtrV); |
| 2917 | |
| 2918 | SDValue Root = getRoot(); |
| 2919 | SmallVector<SDValue, 4> Chains(NumValues); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2920 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2921 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2922 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2923 | unsigned Alignment = I.getAlignment(); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2924 | |
| 2925 | for (unsigned i = 0; i != NumValues; ++i) { |
| 2926 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr, |
| 2927 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2928 | Chains[i] = DAG.getStore(Root, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 2929 | SDValue(Src.getNode(), Src.getResNo() + i), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2930 | Add, PtrV, Offsets[i], isVolatile, |
| 2931 | isNonTemporal, Alignment); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2934 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 2935 | MVT::Other, &Chains[0], NumValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2939 | /// node. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2940 | void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2941 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2942 | bool HasChain = !I.doesNotAccessMemory(); |
| 2943 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2944 | |
| 2945 | // Build the operand list. |
| 2946 | SmallVector<SDValue, 8> Ops; |
| 2947 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2948 | if (OnlyLoad) { |
| 2949 | // We don't need to serialize loads against other loads. |
| 2950 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2951 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2952 | Ops.push_back(getRoot()); |
| 2953 | } |
| 2954 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2955 | |
| 2956 | // Info is set by getTgtMemInstrinsic |
| 2957 | TargetLowering::IntrinsicInfo Info; |
| 2958 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 2959 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2960 | // 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] | 2961 | if (!IsTgtIntrinsic) |
| 2962 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2963 | |
| 2964 | // Add all operands of the call to the operand list. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 2965 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 2966 | SDValue Op = getValue(I.getArgOperand(i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2967 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2968 | "Intrinsic uses a non-legal type?"); |
| 2969 | Ops.push_back(Op); |
| 2970 | } |
| 2971 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2972 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2973 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2974 | #ifndef NDEBUG |
| 2975 | for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) { |
| 2976 | assert(TLI.isTypeLegal(ValueVTs[Val]) && |
| 2977 | "Intrinsic uses a non-legal type?"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2978 | } |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2979 | #endif // NDEBUG |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2980 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2981 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2982 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2983 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 2984 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2985 | |
| 2986 | // Create the node. |
| 2987 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2988 | if (IsTgtIntrinsic) { |
| 2989 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 2990 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2991 | VTs, &Ops[0], Ops.size(), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 2992 | Info.memVT, Info.ptrVal, Info.offset, |
| 2993 | Info.align, Info.vol, |
| 2994 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 2995 | } else if (!HasChain) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2996 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 2997 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 2998 | } else if (!I.getType()->isVoidTy()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2999 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3000 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3001 | } else { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3002 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3003 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3006 | if (HasChain) { |
| 3007 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 3008 | if (OnlyLoad) |
| 3009 | PendingLoads.push_back(Chain); |
| 3010 | else |
| 3011 | DAG.setRoot(Chain); |
| 3012 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3013 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3014 | if (!I.getType()->isVoidTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3015 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3016 | EVT VT = TLI.getValueType(PTy); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3017 | Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3018 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3019 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3020 | setValue(&I, Result); |
| 3021 | } |
| 3022 | } |
| 3023 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3024 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3025 | /// number with exponent of 1: |
| 3026 | /// |
| 3027 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3028 | /// |
| 3029 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3030 | static SDValue |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3031 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3032 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3033 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 3034 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 3035 | DAG.getConstant(0x3f800000, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3036 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3039 | /// GetExponent - Get the exponent: |
| 3040 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3041 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3042 | /// |
| 3043 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3044 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3045 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3046 | DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3047 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3048 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 3049 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3050 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3051 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 3052 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3053 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3056 | /// getF32Constant - Get 32-bit floating point constant. |
| 3057 | static SDValue |
| 3058 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3059 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3062 | /// Inlined utility function to implement binary input atomic intrinsics for |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3063 | /// visitIntrinsicCall: I is a call instruction |
| 3064 | /// Op is the associated NodeType for I |
| 3065 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3066 | SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I, |
| 3067 | ISD::NodeType Op) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3068 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3069 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3070 | DAG.getAtomic(Op, getCurDebugLoc(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3071 | getValue(I.getArgOperand(1)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 3072 | Root, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3073 | getValue(I.getArgOperand(0)), |
| 3074 | getValue(I.getArgOperand(1)), |
| 3075 | I.getArgOperand(0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3076 | setValue(&I, L); |
| 3077 | DAG.setRoot(L.getValue(1)); |
| 3078 | return 0; |
| 3079 | } |
| 3080 | |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3081 | // implVisitAluOverflow - Lower arithmetic overflow instrinsics. |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3082 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3083 | SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3084 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 3085 | SDValue Op2 = getValue(I.getArgOperand(1)); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3086 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3087 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3088 | setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2)); |
Bill Wendling | 2ce4e5c | 2008-12-10 00:28:22 +0000 | [diff] [blame] | 3089 | return 0; |
| 3090 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 3091 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3092 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 3093 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3094 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3095 | SelectionDAGBuilder::visitExp(const CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3096 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3097 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3098 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3099 | if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3100 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3101 | SDValue Op = getValue(I.getArgOperand(0)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3102 | |
| 3103 | // Put the exponent in the right bit position for later addition to the |
| 3104 | // final result: |
| 3105 | // |
| 3106 | // #define LOG2OFe 1.4426950f |
| 3107 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3108 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3109 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3110 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3111 | |
| 3112 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3113 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3114 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3115 | |
| 3116 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3117 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3118 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3119 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3120 | if (LimitFloatPrecision <= 6) { |
| 3121 | // For floating-point precision of 6: |
| 3122 | // |
| 3123 | // TwoToFractionalPartOfX = |
| 3124 | // 0.997535578f + |
| 3125 | // (0.735607626f + 0.252464424f * x) * x; |
| 3126 | // |
| 3127 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3128 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3129 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3130 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3131 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3132 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3133 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3134 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3135 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3136 | |
| 3137 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3138 | SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3139 | TwoToFracPartOfX, IntegerPartOfX); |
| 3140 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3141 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3142 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3143 | // For floating-point precision of 12: |
| 3144 | // |
| 3145 | // TwoToFractionalPartOfX = |
| 3146 | // 0.999892986f + |
| 3147 | // (0.696457318f + |
| 3148 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3149 | // |
| 3150 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3151 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3152 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3153 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3154 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3155 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3156 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3157 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3158 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3159 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3160 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3161 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3164 | SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3165 | TwoToFracPartOfX, IntegerPartOfX); |
| 3166 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3167 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3168 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3169 | // For floating-point precision of 18: |
| 3170 | // |
| 3171 | // TwoToFractionalPartOfX = |
| 3172 | // 0.999999982f + |
| 3173 | // (0.693148872f + |
| 3174 | // (0.240227044f + |
| 3175 | // (0.554906021e-1f + |
| 3176 | // (0.961591928e-2f + |
| 3177 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3178 | // |
| 3179 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3180 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3181 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3182 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3183 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3184 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3185 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3186 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3187 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3188 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3189 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3190 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3191 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3192 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3193 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3194 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3195 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3196 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3197 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3198 | getF32Constant(DAG, 0x3f800000)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3199 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3200 | MVT::i32, t13); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3201 | |
| 3202 | // Add the exponent into the result in integer domain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3203 | SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3204 | TwoToFracPartOfX, IntegerPartOfX); |
| 3205 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3206 | result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3207 | } |
| 3208 | } else { |
| 3209 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3210 | result = DAG.getNode(ISD::FEXP, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3211 | getValue(I.getArgOperand(0)).getValueType(), |
| 3212 | getValue(I.getArgOperand(0))); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3215 | setValue(&I, result); |
| 3216 | } |
| 3217 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3218 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 3219 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3220 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3221 | SelectionDAGBuilder::visitLog(const CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3222 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3223 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3224 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3225 | if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3226 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3227 | SDValue Op = getValue(I.getArgOperand(0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3228 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3229 | |
| 3230 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3231 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3232 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3233 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3234 | |
| 3235 | // Get the significand and build it into a floating-point number with |
| 3236 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3237 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3238 | |
| 3239 | if (LimitFloatPrecision <= 6) { |
| 3240 | // For floating-point precision of 6: |
| 3241 | // |
| 3242 | // LogofMantissa = |
| 3243 | // -1.1609546f + |
| 3244 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3245 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3246 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3247 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3248 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3249 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3250 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3251 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3252 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3253 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3254 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3255 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3256 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3257 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3258 | // For floating-point precision of 12: |
| 3259 | // |
| 3260 | // LogOfMantissa = |
| 3261 | // -1.7417939f + |
| 3262 | // (2.8212026f + |
| 3263 | // (-1.4699568f + |
| 3264 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3265 | // |
| 3266 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3267 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3268 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3269 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3270 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3271 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3272 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3273 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3274 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3275 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3276 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3277 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3278 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3279 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3280 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3281 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3282 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3283 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3284 | // For floating-point precision of 18: |
| 3285 | // |
| 3286 | // LogOfMantissa = |
| 3287 | // -2.1072184f + |
| 3288 | // (4.2372794f + |
| 3289 | // (-3.7029485f + |
| 3290 | // (2.2781945f + |
| 3291 | // (-0.87823314f + |
| 3292 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3293 | // |
| 3294 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3295 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3296 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3297 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3298 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3299 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3300 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3301 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3302 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3303 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3304 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3305 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3306 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3307 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3308 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3309 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3310 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3311 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3312 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3313 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3314 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3315 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3316 | MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3317 | } |
| 3318 | } else { |
| 3319 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3320 | result = DAG.getNode(ISD::FLOG, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3321 | getValue(I.getArgOperand(0)).getValueType(), |
| 3322 | getValue(I.getArgOperand(0))); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3325 | setValue(&I, result); |
| 3326 | } |
| 3327 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3328 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3329 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3330 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3331 | SelectionDAGBuilder::visitLog2(const CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3332 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3333 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3334 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3335 | if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3336 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3337 | SDValue Op = getValue(I.getArgOperand(0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3338 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3339 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3340 | // Get the exponent. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3341 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3342 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3343 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3344 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3345 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3346 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3347 | // Different possible minimax approximations of significand in |
| 3348 | // floating-point for various degrees of accuracy over [1,2]. |
| 3349 | if (LimitFloatPrecision <= 6) { |
| 3350 | // For floating-point precision of 6: |
| 3351 | // |
| 3352 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3353 | // |
| 3354 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3355 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3356 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3357 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3358 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3359 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3360 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3361 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3362 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3363 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3364 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3365 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3366 | // For floating-point precision of 12: |
| 3367 | // |
| 3368 | // Log2ofMantissa = |
| 3369 | // -2.51285454f + |
| 3370 | // (4.07009056f + |
| 3371 | // (-2.12067489f + |
| 3372 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3373 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3374 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3375 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3376 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3377 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3378 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3379 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3380 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3381 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3382 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3383 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3384 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3385 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3386 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3387 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3388 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3389 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3390 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3391 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3392 | // For floating-point precision of 18: |
| 3393 | // |
| 3394 | // Log2ofMantissa = |
| 3395 | // -3.0400495f + |
| 3396 | // (6.1129976f + |
| 3397 | // (-5.3420409f + |
| 3398 | // (3.2865683f + |
| 3399 | // (-1.2669343f + |
| 3400 | // (0.27515199f - |
| 3401 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3402 | // |
| 3403 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3404 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3405 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3406 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3407 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3408 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3409 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3410 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3411 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3412 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3413 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3414 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3415 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3416 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3417 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3418 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3419 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3420 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3421 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3422 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3423 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3424 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3425 | MVT::f32, LogOfExponent, Log2ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3426 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3427 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3428 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3429 | result = DAG.getNode(ISD::FLOG2, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3430 | getValue(I.getArgOperand(0)).getValueType(), |
| 3431 | getValue(I.getArgOperand(0))); |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3432 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3433 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3434 | setValue(&I, result); |
| 3435 | } |
| 3436 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3437 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3438 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3439 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3440 | SelectionDAGBuilder::visitLog10(const CallInst &I) { |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3441 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3442 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 3443 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3444 | if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3445 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3446 | SDValue Op = getValue(I.getArgOperand(0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3447 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3448 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3449 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3450 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3451 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3452 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3453 | |
| 3454 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3455 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3456 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3457 | |
| 3458 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3459 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3460 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3461 | // Log10ofMantissa = |
| 3462 | // -0.50419619f + |
| 3463 | // (0.60948995f - 0.10380950f * x) * x; |
| 3464 | // |
| 3465 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3466 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3467 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3468 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3469 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3470 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3471 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3472 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3473 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3474 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3475 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3476 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3477 | // For floating-point precision of 12: |
| 3478 | // |
| 3479 | // Log10ofMantissa = |
| 3480 | // -0.64831180f + |
| 3481 | // (0.91751397f + |
| 3482 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3483 | // |
| 3484 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3485 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3486 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3487 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3488 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3489 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3490 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3491 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3492 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3493 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3494 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3495 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3496 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3497 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3498 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3499 | // For floating-point precision of 18: |
| 3500 | // |
| 3501 | // Log10ofMantissa = |
| 3502 | // -0.84299375f + |
| 3503 | // (1.5327582f + |
| 3504 | // (-1.0688956f + |
| 3505 | // (0.49102474f + |
| 3506 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3507 | // |
| 3508 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3509 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3510 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3511 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3512 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3513 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3514 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3515 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3516 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3517 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3518 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3519 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3520 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3521 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3522 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3523 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3524 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3525 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3526 | result = DAG.getNode(ISD::FADD, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3527 | MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3528 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3529 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3530 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3531 | result = DAG.getNode(ISD::FLOG10, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3532 | getValue(I.getArgOperand(0)).getValueType(), |
| 3533 | getValue(I.getArgOperand(0))); |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3534 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3535 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3536 | setValue(&I, result); |
| 3537 | } |
| 3538 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3539 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3540 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3541 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3542 | SelectionDAGBuilder::visitExp2(const CallInst &I) { |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3543 | SDValue result; |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3544 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3545 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3546 | if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3547 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3548 | SDValue Op = getValue(I.getArgOperand(0)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3549 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3550 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3551 | |
| 3552 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3553 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3554 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3555 | |
| 3556 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3557 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3558 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3559 | |
| 3560 | if (LimitFloatPrecision <= 6) { |
| 3561 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3562 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3563 | // TwoToFractionalPartOfX = |
| 3564 | // 0.997535578f + |
| 3565 | // (0.735607626f + 0.252464424f * x) * x; |
| 3566 | // |
| 3567 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3568 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3569 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3570 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3571 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3572 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3573 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3574 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3575 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3576 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3577 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3578 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3579 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3580 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3581 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3582 | // For floating-point precision of 12: |
| 3583 | // |
| 3584 | // TwoToFractionalPartOfX = |
| 3585 | // 0.999892986f + |
| 3586 | // (0.696457318f + |
| 3587 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3588 | // |
| 3589 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3590 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3591 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3592 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3593 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3594 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3595 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3596 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3597 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3598 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3599 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3600 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3601 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3602 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3603 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3604 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3605 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3606 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3607 | // For floating-point precision of 18: |
| 3608 | // |
| 3609 | // TwoToFractionalPartOfX = |
| 3610 | // 0.999999982f + |
| 3611 | // (0.693148872f + |
| 3612 | // (0.240227044f + |
| 3613 | // (0.554906021e-1f + |
| 3614 | // (0.961591928e-2f + |
| 3615 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3616 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3617 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3618 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3619 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3620 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3621 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3622 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3623 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3624 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3625 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3626 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3627 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3628 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3629 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3630 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3631 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3632 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3633 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3634 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3635 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3636 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3637 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3638 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3639 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3640 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3641 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3642 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3643 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3644 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3645 | result = DAG.getNode(ISD::FEXP2, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3646 | getValue(I.getArgOperand(0)).getValueType(), |
| 3647 | getValue(I.getArgOperand(0))); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3648 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3649 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3650 | setValue(&I, result); |
| 3651 | } |
| 3652 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3653 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3654 | /// limited-precision mode with x == 10.0f. |
| 3655 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3656 | SelectionDAGBuilder::visitPow(const CallInst &I) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3657 | SDValue result; |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3658 | const Value *Val = I.getArgOperand(0); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3659 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3660 | bool IsExp10 = false; |
| 3661 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3662 | if (getValue(Val).getValueType() == MVT::f32 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3663 | getValue(I.getArgOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3664 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3665 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3666 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3667 | APFloat Ten(10.0f); |
| 3668 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3669 | } |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3674 | SDValue Op = getValue(I.getArgOperand(1)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3675 | |
| 3676 | // Put the exponent in the right bit position for later addition to the |
| 3677 | // final result: |
| 3678 | // |
| 3679 | // #define LOG2OF10 3.3219281f |
| 3680 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3681 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3682 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3683 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3684 | |
| 3685 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3686 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3687 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3688 | |
| 3689 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3690 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3691 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3692 | |
| 3693 | if (LimitFloatPrecision <= 6) { |
| 3694 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3695 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3696 | // twoToFractionalPartOfX = |
| 3697 | // 0.997535578f + |
| 3698 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3699 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3700 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3701 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3702 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3703 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3704 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3705 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3706 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3707 | getF32Constant(DAG, 0x3f7f5e7e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3708 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3709 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3710 | DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3711 | |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3712 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3713 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3714 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3715 | // For floating-point precision of 12: |
| 3716 | // |
| 3717 | // TwoToFractionalPartOfX = |
| 3718 | // 0.999892986f + |
| 3719 | // (0.696457318f + |
| 3720 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3721 | // |
| 3722 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3723 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3724 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3725 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3726 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3727 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3728 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3729 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3730 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3731 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3732 | getF32Constant(DAG, 0x3f7ff8fd)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3733 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3734 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3735 | DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3736 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3737 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3738 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3739 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3740 | // For floating-point precision of 18: |
| 3741 | // |
| 3742 | // TwoToFractionalPartOfX = |
| 3743 | // 0.999999982f + |
| 3744 | // (0.693148872f + |
| 3745 | // (0.240227044f + |
| 3746 | // (0.554906021e-1f + |
| 3747 | // (0.961591928e-2f + |
| 3748 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3749 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3750 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3751 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3752 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3753 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3754 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3755 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3756 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3757 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3758 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3759 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3760 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3761 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3762 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3763 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3764 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3765 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3766 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
| 3767 | SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3768 | getF32Constant(DAG, 0x3f800000)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3769 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3770 | SDValue TwoToFractionalPartOfX = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3771 | DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3772 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3773 | result = DAG.getNode(ISD::BIT_CONVERT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3774 | MVT::f32, TwoToFractionalPartOfX); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3775 | } |
| 3776 | } else { |
| 3777 | // No special expansion. |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3778 | result = DAG.getNode(ISD::FPOW, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3779 | getValue(I.getArgOperand(0)).getValueType(), |
| 3780 | getValue(I.getArgOperand(0)), |
| 3781 | getValue(I.getArgOperand(1))); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | setValue(&I, result); |
| 3785 | } |
| 3786 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3787 | |
| 3788 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
| 3789 | static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS, |
| 3790 | SelectionDAG &DAG) { |
| 3791 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 3792 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 3793 | // optimizing for size, we only want to do this if the expansion would produce |
| 3794 | // a small number of multiplies, otherwise we do the full expansion. |
| 3795 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 3796 | // Get the exponent as a positive value. |
| 3797 | unsigned Val = RHSC->getSExtValue(); |
| 3798 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3799 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3800 | // powi(x, 0) -> 1.0 |
| 3801 | if (Val == 0) |
| 3802 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 3803 | |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 3804 | const Function *F = DAG.getMachineFunction().getFunction(); |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3805 | if (!F->hasFnAttr(Attribute::OptimizeForSize) || |
| 3806 | // If optimizing for size, don't insert too many multiplies. This |
| 3807 | // inserts up to 5 multiplies. |
| 3808 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 3809 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3810 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3811 | // powi(x,15) generates one more multiply than it should), but this has |
| 3812 | // the benefit of being both really simple and much better than a libcall. |
| 3813 | SDValue Res; // Logically starts equal to 1.0 |
| 3814 | SDValue CurSquare = LHS; |
| 3815 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3816 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3817 | if (Res.getNode()) |
| 3818 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 3819 | else |
| 3820 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 3821 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3822 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3823 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 3824 | CurSquare, CurSquare); |
| 3825 | Val >>= 1; |
| 3826 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3827 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3828 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 3829 | if (RHSC->getSExtValue() < 0) |
| 3830 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 3831 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 3832 | return Res; |
| 3833 | } |
| 3834 | } |
| 3835 | |
| 3836 | // Otherwise, expand to a libcall. |
| 3837 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 3838 | } |
| 3839 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3840 | /// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function |
| 3841 | /// argument, create the corresponding DBG_VALUE machine instruction for it now. |
| 3842 | /// At the end of instruction selection, they will be inserted to the entry BB. |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3843 | bool |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3844 | SelectionDAGBuilder::EmitFuncArgumentDbgValue(const DbgValueInst &DI, |
| 3845 | const Value *V, MDNode *Variable, |
Dan Gohman | 5d11ea3 | 2010-05-01 00:33:16 +0000 | [diff] [blame] | 3846 | uint64_t Offset, |
| 3847 | const SDValue &N) { |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3848 | if (!isa<Argument>(V)) |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3849 | return false; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3850 | |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 3851 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 3852 | // Ignore inlined function arguments here. |
| 3853 | DIVariable DV(Variable); |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 3854 | if (DV.isInlinedFnArgument(MF.getFunction())) |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 3855 | return false; |
| 3856 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3857 | MachineBasicBlock *MBB = FuncInfo.MBBMap[DI.getParent()]; |
| 3858 | if (MBB != &MF.front()) |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3859 | return false; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3860 | |
| 3861 | unsigned Reg = 0; |
| 3862 | if (N.getOpcode() == ISD::CopyFromReg) { |
| 3863 | Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
Evan Cheng | 1deef27 | 2010-04-29 00:59:34 +0000 | [diff] [blame] | 3864 | if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3865 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 3866 | unsigned PR = RegInfo.getLiveInPhysReg(Reg); |
| 3867 | if (PR) |
| 3868 | Reg = PR; |
| 3869 | } |
| 3870 | } |
| 3871 | |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 3872 | if (!Reg) { |
| 3873 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 3874 | if (VMI == FuncInfo.ValueMap.end()) |
| 3875 | return false; |
| 3876 | Reg = VMI->second; |
| 3877 | } |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3878 | |
| 3879 | const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo(); |
| 3880 | MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(), |
| 3881 | TII->get(TargetOpcode::DBG_VALUE)) |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 3882 | .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3883 | FuncInfo.ArgDbgValues.push_back(&*MIB); |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 3884 | return true; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 3885 | } |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 3887 | // VisualStudio defines setjmp as _setjmp |
| 3888 | #if defined(_MSC_VER) && defined(setjmp) |
| 3889 | #define setjmp_undefined_for_visual_studio |
| 3890 | #undef setjmp |
| 3891 | #endif |
| 3892 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3893 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3894 | /// we want to emit this as a call to a named external function, return the name |
| 3895 | /// otherwise lower it and return null. |
| 3896 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3897 | SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3898 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 3899 | SDValue Res; |
| 3900 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3901 | switch (Intrinsic) { |
| 3902 | default: |
| 3903 | // By default, turn this into a target intrinsic node. |
| 3904 | visitTargetIntrinsic(I, Intrinsic); |
| 3905 | return 0; |
| 3906 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3907 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3908 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3909 | case Intrinsic::returnaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3910 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3911 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3912 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 3913 | case Intrinsic::frameaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3914 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3915 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3916 | return 0; |
| 3917 | case Intrinsic::setjmp: |
| 3918 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3919 | case Intrinsic::longjmp: |
| 3920 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3921 | case Intrinsic::memcpy: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3922 | // Assert for address < 256 since we support only user defined address |
| 3923 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3924 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3925 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3926 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3927 | < 256 && |
| 3928 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3929 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 3930 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 3931 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 3932 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
| 3933 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3934 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3935 | I.getArgOperand(0), 0, I.getArgOperand(1), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3936 | return 0; |
| 3937 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3938 | case Intrinsic::memset: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3939 | // Assert for address < 256 since we support only user defined address |
| 3940 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3941 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3942 | < 256 && |
| 3943 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3944 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 3945 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 3946 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 3947 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
| 3948 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3949 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3950 | I.getArgOperand(0), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3951 | return 0; |
| 3952 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 3953 | case Intrinsic::memmove: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3954 | // Assert for address < 256 since we support only user defined address |
| 3955 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3956 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3957 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3958 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3959 | < 256 && |
| 3960 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3961 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 3962 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 3963 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 3964 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
| 3965 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3966 | |
| 3967 | // If the source and destination are known to not be aliases, we can |
| 3968 | // lower memmove as memcpy. |
| 3969 | uint64_t Size = -1ULL; |
| 3970 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3971 | Size = C->getZExtValue(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3972 | if (AA->alias(I.getArgOperand(0), Size, I.getArgOperand(1), Size) == |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3973 | AliasAnalysis::NoAlias) { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3974 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3975 | false, I.getArgOperand(0), 0, I.getArgOperand(1), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3976 | return 0; |
| 3977 | } |
| 3978 | |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3979 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3980 | I.getArgOperand(0), 0, I.getArgOperand(1), 0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3981 | return 0; |
| 3982 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 3983 | case Intrinsic::dbg_declare: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3984 | const DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Devang Patel | 02f0dbd | 2010-05-07 22:04:20 +0000 | [diff] [blame] | 3985 | if (!DIVariable(DI.getVariable()).Verify()) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 3986 | return 0; |
| 3987 | |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 3988 | MDNode *Variable = DI.getVariable(); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3989 | // Parameters are handled specially. |
Devang Patel | f38c6c8 | 2010-04-28 23:24:13 +0000 | [diff] [blame] | 3990 | bool isParameter = |
| 3991 | DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3992 | const Value *Address = DI.getAddress(); |
Dale Johannesen | 8ac38f2 | 2010-02-08 21:53:27 +0000 | [diff] [blame] | 3993 | if (!Address) |
| 3994 | return 0; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3995 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 3996 | Address = BCI->getOperand(0); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3997 | const AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 3998 | if (AI) { |
| 3999 | // Don't handle byval arguments or VLAs, for example. |
| 4000 | // Non-byval arguments are handled here (they refer to the stack temporary |
| 4001 | // alloca at this point). |
| 4002 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4003 | FuncInfo.StaticAllocaMap.find(AI); |
| 4004 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 4005 | return 0; // VLAs. |
| 4006 | int FI = SI->second; |
Devang Patel | 70d75ca | 2009-11-12 19:02:56 +0000 | [diff] [blame] | 4007 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4008 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 4009 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 4010 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
| 4011 | } |
| 4012 | |
| 4013 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 4014 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 4015 | // absolute, but not relative, values are different depending on whether |
| 4016 | // debug info exists. |
| 4017 | ++SDNodeOrder; |
| 4018 | SDValue &N = NodeMap[Address]; |
| 4019 | SDDbgValue *SDV; |
| 4020 | if (N.getNode()) { |
| 4021 | if (isParameter && !AI) { |
| 4022 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode()); |
| 4023 | if (FINode) |
| 4024 | // Byval parameter. We have a frame index at this point. |
| 4025 | SDV = DAG.getDbgValue(Variable, FINode->getIndex(), |
| 4026 | 0, dl, SDNodeOrder); |
| 4027 | else |
| 4028 | // Can't do anything with other non-AI cases yet. This might be a |
| 4029 | // parameter of a callee function that got inlined, for example. |
| 4030 | return 0; |
| 4031 | } else if (AI) |
| 4032 | SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(), |
| 4033 | 0, dl, SDNodeOrder); |
| 4034 | else |
| 4035 | // Can't do anything with other non-AI cases yet. |
| 4036 | return 0; |
| 4037 | DAG.AddDbgValue(SDV, N.getNode(), isParameter); |
| 4038 | } else { |
| 4039 | // This isn't useful, but it shows what we're missing. |
| 4040 | SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()), |
| 4041 | 0, dl, SDNodeOrder); |
| 4042 | DAG.AddDbgValue(SDV, 0, isParameter); |
| 4043 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4044 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4045 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4046 | case Intrinsic::dbg_value: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4047 | const DbgValueInst &DI = cast<DbgValueInst>(I); |
Devang Patel | 02f0dbd | 2010-05-07 22:04:20 +0000 | [diff] [blame] | 4048 | if (!DIVariable(DI.getVariable()).Verify()) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4049 | return 0; |
| 4050 | |
| 4051 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4052 | uint64_t Offset = DI.getOffset(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4053 | const Value *V = DI.getValue(); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4054 | if (!V) |
| 4055 | return 0; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4056 | |
| 4057 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 4058 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 4059 | // absolute, but not relative, values are different depending on whether |
| 4060 | // debug info exists. |
| 4061 | ++SDNodeOrder; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4062 | SDDbgValue *SDV; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4063 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4064 | SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder); |
| 4065 | DAG.AddDbgValue(SDV, 0, false); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4066 | } else { |
Devang Patel | d47f3c8 | 2010-05-05 22:29:00 +0000 | [diff] [blame] | 4067 | bool createUndef = false; |
| 4068 | // FIXME : Why not use getValue() directly ? |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 4069 | SDValue N = NodeMap[V]; |
| 4070 | if (!N.getNode() && isa<Argument>(V)) |
| 4071 | // Check unused arguments map. |
| 4072 | N = UnusedArgNodeMap[V]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4073 | if (N.getNode()) { |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4074 | if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) { |
| 4075 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 4076 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 4077 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 4078 | } |
Devang Patel | d47f3c8 | 2010-05-05 22:29:00 +0000 | [diff] [blame] | 4079 | } else if (isa<PHINode>(V) && !V->use_empty()) { |
| 4080 | SDValue N = getValue(V); |
| 4081 | if (N.getNode()) { |
| 4082 | if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) { |
| 4083 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 4084 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 4085 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 4086 | } |
| 4087 | } else |
| 4088 | createUndef = true; |
| 4089 | } else |
| 4090 | createUndef = true; |
| 4091 | if (createUndef) { |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4092 | // We may expand this to cover more cases. One case where we have no |
| 4093 | // data available is an unreferenced parameter; we need this fallback. |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4094 | SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()), |
| 4095 | Offset, dl, SDNodeOrder); |
| 4096 | DAG.AddDbgValue(SDV, 0, false); |
| 4097 | } |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | // Build a debug info table entry. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4101 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4102 | V = BCI->getOperand(0); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4103 | const AllocaInst *AI = dyn_cast<AllocaInst>(V); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4104 | // Don't handle byval struct arguments or VLAs, for example. |
| 4105 | if (!AI) |
| 4106 | return 0; |
| 4107 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4108 | FuncInfo.StaticAllocaMap.find(AI); |
| 4109 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 4110 | return 0; // VLAs. |
| 4111 | int FI = SI->second; |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 4112 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4113 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 4114 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 4115 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4116 | return 0; |
| 4117 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4118 | case Intrinsic::eh_exception: { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4119 | // Insert the EXCEPTIONADDR instruction. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 4120 | assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() && |
| 4121 | "Call to eh.exception not in landing pad!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4122 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4123 | SDValue Ops[1]; |
| 4124 | Ops[0] = DAG.getRoot(); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4125 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4126 | setValue(&I, Op); |
| 4127 | DAG.setRoot(Op.getValue(1)); |
| 4128 | return 0; |
| 4129 | } |
| 4130 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4131 | case Intrinsic::eh_selector: { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 4132 | MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()]; |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4133 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 4134 | if (CallMBB->isLandingPad()) |
| 4135 | AddCatchInfo(I, &MMI, CallMBB); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4136 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4137 | #ifndef NDEBUG |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4138 | FuncInfo.CatchInfoLost.insert(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4139 | #endif |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4140 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 4141 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 4142 | if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4143 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4144 | |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4145 | // Insert the EHSELECTION instruction. |
| 4146 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 4147 | SDValue Ops[2]; |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4148 | Ops[0] = getValue(I.getArgOperand(0)); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4149 | Ops[1] = getRoot(); |
| 4150 | SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2); |
Chris Lattner | 3a5815f | 2009-09-17 23:54:54 +0000 | [diff] [blame] | 4151 | DAG.setRoot(Op.getValue(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4152 | setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4153 | return 0; |
| 4154 | } |
| 4155 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4156 | case Intrinsic::eh_typeid_for: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4157 | // Find the type id for the given typeinfo. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4158 | GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0)); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4159 | unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV); |
| 4160 | Res = DAG.getConstant(TypeID, MVT::i32); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4161 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4162 | return 0; |
| 4163 | } |
| 4164 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4165 | case Intrinsic::eh_return_i32: |
| 4166 | case Intrinsic::eh_return_i64: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4167 | DAG.getMachineFunction().getMMI().setCallsEHReturn(true); |
| 4168 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
| 4169 | MVT::Other, |
| 4170 | getControlRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4171 | getValue(I.getArgOperand(0)), |
| 4172 | getValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4173 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4174 | case Intrinsic::eh_unwind_init: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4175 | DAG.getMachineFunction().getMMI().setCallsUnwindInit(true); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4176 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4177 | case Intrinsic::eh_dwarf_cfa: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4178 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), dl, |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 4179 | TLI.getPointerTy()); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4180 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4181 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4182 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4183 | TLI.getPointerTy()), |
| 4184 | CfaArg); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4185 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4186 | TLI.getPointerTy(), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4187 | DAG.getConstant(0, TLI.getPointerTy())); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4188 | setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), |
| 4189 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4190 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4191 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4192 | case Intrinsic::eh_sjlj_callsite: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4193 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4194 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0)); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4195 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4196 | assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4197 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4198 | MMI.setCurrentCallSite(CI->getZExtValue()); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4199 | return 0; |
| 4200 | } |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4201 | case Intrinsic::eh_sjlj_setjmp: { |
| 4202 | setValue(&I, DAG.getNode(ISD::EH_SJLJ_SETJMP, dl, MVT::i32, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4203 | getValue(I.getArgOperand(0)))); |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4204 | return 0; |
| 4205 | } |
Jim Grosbach | 5eb1951 | 2010-05-22 01:06:18 +0000 | [diff] [blame] | 4206 | case Intrinsic::eh_sjlj_longjmp: { |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4207 | DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, dl, MVT::Other, |
| 4208 | getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4209 | getValue(I.getArgOperand(0)))); |
Jim Grosbach | 5eb1951 | 2010-05-22 01:06:18 +0000 | [diff] [blame] | 4210 | return 0; |
| 4211 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4212 | |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4213 | case Intrinsic::convertff: |
| 4214 | case Intrinsic::convertfsi: |
| 4215 | case Intrinsic::convertfui: |
| 4216 | case Intrinsic::convertsif: |
| 4217 | case Intrinsic::convertuif: |
| 4218 | case Intrinsic::convertss: |
| 4219 | case Intrinsic::convertsu: |
| 4220 | case Intrinsic::convertus: |
| 4221 | case Intrinsic::convertuu: { |
| 4222 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4223 | switch (Intrinsic) { |
| 4224 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4225 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4226 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4227 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4228 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4229 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4230 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4231 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4232 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4233 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4234 | EVT DestVT = TLI.getValueType(I.getType()); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4235 | const Value *Op1 = I.getArgOperand(0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4236 | Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1), |
| 4237 | DAG.getValueType(DestVT), |
| 4238 | DAG.getValueType(getValue(Op1).getValueType()), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4239 | getValue(I.getArgOperand(1)), |
| 4240 | getValue(I.getArgOperand(2)), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4241 | Code); |
| 4242 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4243 | return 0; |
| 4244 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4245 | case Intrinsic::sqrt: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4246 | setValue(&I, DAG.getNode(ISD::FSQRT, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4247 | getValue(I.getArgOperand(0)).getValueType(), |
| 4248 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4249 | return 0; |
| 4250 | case Intrinsic::powi: |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4251 | setValue(&I, ExpandPowI(dl, getValue(I.getArgOperand(0)), |
| 4252 | getValue(I.getArgOperand(1)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4253 | return 0; |
| 4254 | case Intrinsic::sin: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4255 | setValue(&I, DAG.getNode(ISD::FSIN, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4256 | getValue(I.getArgOperand(0)).getValueType(), |
| 4257 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4258 | return 0; |
| 4259 | case Intrinsic::cos: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4260 | setValue(&I, DAG.getNode(ISD::FCOS, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4261 | getValue(I.getArgOperand(0)).getValueType(), |
| 4262 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4263 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4264 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4265 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4266 | return 0; |
| 4267 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4268 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4269 | return 0; |
| 4270 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4271 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4272 | return 0; |
| 4273 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4274 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4275 | return 0; |
| 4276 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4277 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4278 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4279 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4280 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4281 | return 0; |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4282 | case Intrinsic::convert_to_fp16: |
| 4283 | setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4284 | MVT::i16, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4285 | return 0; |
| 4286 | case Intrinsic::convert_from_fp16: |
| 4287 | setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4288 | MVT::f32, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4289 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4290 | case Intrinsic::pcmarker: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4291 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4292 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4293 | return 0; |
| 4294 | } |
| 4295 | case Intrinsic::readcyclecounter: { |
| 4296 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4297 | Res = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
| 4298 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4299 | &Op, 1); |
| 4300 | setValue(&I, Res); |
| 4301 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4302 | return 0; |
| 4303 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4304 | case Intrinsic::bswap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4305 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4306 | getValue(I.getArgOperand(0)).getValueType(), |
| 4307 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4308 | return 0; |
| 4309 | case Intrinsic::cttz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4310 | SDValue Arg = getValue(I.getArgOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4311 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4312 | setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4313 | return 0; |
| 4314 | } |
| 4315 | case Intrinsic::ctlz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4316 | SDValue Arg = getValue(I.getArgOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4317 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4318 | setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4319 | return 0; |
| 4320 | } |
| 4321 | case Intrinsic::ctpop: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4322 | SDValue Arg = getValue(I.getArgOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4323 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4324 | setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4325 | return 0; |
| 4326 | } |
| 4327 | case Intrinsic::stacksave: { |
| 4328 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4329 | Res = DAG.getNode(ISD::STACKSAVE, dl, |
| 4330 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
| 4331 | setValue(&I, Res); |
| 4332 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4333 | return 0; |
| 4334 | } |
| 4335 | case Intrinsic::stackrestore: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4336 | Res = getValue(I.getArgOperand(0)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4337 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4338 | return 0; |
| 4339 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4340 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4341 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4342 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4343 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4344 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4345 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4346 | SDValue Src = getValue(I.getArgOperand(0)); // The guard's value. |
| 4347 | AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4348 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4349 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4350 | MFI->setStackProtectorIndex(FI); |
| 4351 | |
| 4352 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4353 | |
| 4354 | // Store the stack protector onto the stack. |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4355 | Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN, |
| 4356 | PseudoSourceValue::getFixedStack(FI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4357 | 0, true, false, 0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4358 | setValue(&I, Res); |
| 4359 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4360 | return 0; |
| 4361 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4362 | case Intrinsic::objectsize: { |
| 4363 | // If we don't know by now, we're never going to know. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4364 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4365 | |
| 4366 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4367 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4368 | SDValue Arg = getValue(I.getCalledValue()); |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 4369 | EVT Ty = Arg.getValueType(); |
| 4370 | |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 4371 | if (CI->isZero()) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4372 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4373 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4374 | Res = DAG.getConstant(0, Ty); |
| 4375 | |
| 4376 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4377 | return 0; |
| 4378 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4379 | case Intrinsic::var_annotation: |
| 4380 | // Discard annotate attributes |
| 4381 | return 0; |
| 4382 | |
| 4383 | case Intrinsic::init_trampoline: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4384 | const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4385 | |
| 4386 | SDValue Ops[6]; |
| 4387 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4388 | Ops[1] = getValue(I.getArgOperand(0)); |
| 4389 | Ops[2] = getValue(I.getArgOperand(1)); |
| 4390 | Ops[3] = getValue(I.getArgOperand(2)); |
| 4391 | Ops[4] = DAG.getSrcValue(I.getArgOperand(0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4392 | Ops[5] = DAG.getSrcValue(F); |
| 4393 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4394 | Res = DAG.getNode(ISD::TRAMPOLINE, dl, |
| 4395 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), |
| 4396 | Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4397 | |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4398 | setValue(&I, Res); |
| 4399 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4400 | return 0; |
| 4401 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4402 | case Intrinsic::gcroot: |
| 4403 | if (GFI) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4404 | const Value *Alloca = I.getArgOperand(0); |
| 4405 | const Constant *TypeMap = cast<Constant>(I.getArgOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4406 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4407 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 4408 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 4409 | } |
| 4410 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4411 | case Intrinsic::gcread: |
| 4412 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4413 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4414 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4415 | case Intrinsic::flt_rounds: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4416 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4417 | return 0; |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4418 | case Intrinsic::trap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4419 | DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4420 | return 0; |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 4421 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 4422 | return implVisitAluOverflow(I, ISD::UADDO); |
| 4423 | case Intrinsic::sadd_with_overflow: |
| 4424 | return implVisitAluOverflow(I, ISD::SADDO); |
| 4425 | case Intrinsic::usub_with_overflow: |
| 4426 | return implVisitAluOverflow(I, ISD::USUBO); |
| 4427 | case Intrinsic::ssub_with_overflow: |
| 4428 | return implVisitAluOverflow(I, ISD::SSUBO); |
| 4429 | case Intrinsic::umul_with_overflow: |
| 4430 | return implVisitAluOverflow(I, ISD::UMULO); |
| 4431 | case Intrinsic::smul_with_overflow: |
| 4432 | return implVisitAluOverflow(I, ISD::SMULO); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 4433 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4434 | case Intrinsic::prefetch: { |
| 4435 | SDValue Ops[4]; |
| 4436 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4437 | Ops[1] = getValue(I.getArgOperand(0)); |
| 4438 | Ops[2] = getValue(I.getArgOperand(1)); |
| 4439 | Ops[3] = getValue(I.getArgOperand(2)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4440 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4441 | return 0; |
| 4442 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4443 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4444 | case Intrinsic::memory_barrier: { |
| 4445 | SDValue Ops[6]; |
| 4446 | Ops[0] = getRoot(); |
| 4447 | for (int x = 1; x < 6; ++x) |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4448 | Ops[x] = getValue(I.getArgOperand(x - 1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4449 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4450 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4451 | return 0; |
| 4452 | } |
| 4453 | case Intrinsic::atomic_cmp_swap: { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4454 | SDValue Root = getRoot(); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4455 | SDValue L = |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4456 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4457 | getValue(I.getArgOperand(1)).getValueType().getSimpleVT(), |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4458 | Root, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4459 | getValue(I.getArgOperand(0)), |
| 4460 | getValue(I.getArgOperand(1)), |
| 4461 | getValue(I.getArgOperand(2)), |
| 4462 | I.getArgOperand(0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4463 | setValue(&I, L); |
| 4464 | DAG.setRoot(L.getValue(1)); |
| 4465 | return 0; |
| 4466 | } |
| 4467 | case Intrinsic::atomic_load_add: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4468 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4469 | case Intrinsic::atomic_load_sub: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4470 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4471 | case Intrinsic::atomic_load_or: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4472 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4473 | case Intrinsic::atomic_load_xor: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4474 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4475 | case Intrinsic::atomic_load_and: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4476 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4477 | case Intrinsic::atomic_load_nand: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4478 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4479 | case Intrinsic::atomic_load_max: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4480 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4481 | case Intrinsic::atomic_load_min: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4482 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4483 | case Intrinsic::atomic_load_umin: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4484 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4485 | case Intrinsic::atomic_load_umax: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4486 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4487 | case Intrinsic::atomic_swap: |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 4488 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4489 | |
| 4490 | case Intrinsic::invariant_start: |
| 4491 | case Intrinsic::lifetime_start: |
| 4492 | // Discard region information. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4493 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 4494 | return 0; |
| 4495 | case Intrinsic::invariant_end: |
| 4496 | case Intrinsic::lifetime_end: |
| 4497 | // Discard region information. |
| 4498 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4499 | } |
| 4500 | } |
| 4501 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4502 | void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 4503 | bool isTailCall, |
| 4504 | MachineBasicBlock *LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4505 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4506 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4507 | const Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4508 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 4509 | MCSymbol *BeginLabel = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4510 | |
| 4511 | TargetLowering::ArgListTy Args; |
| 4512 | TargetLowering::ArgListEntry Entry; |
| 4513 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4514 | |
| 4515 | // Check whether the function can return without sret-demotion. |
| 4516 | SmallVector<EVT, 4> OutVTs; |
| 4517 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
| 4518 | SmallVector<uint64_t, 4> Offsets; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4519 | getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(), |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4520 | OutVTs, OutsFlags, TLI, &Offsets); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4521 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4522 | bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4523 | FTy->isVarArg(), OutVTs, OutsFlags, DAG); |
| 4524 | |
| 4525 | SDValue DemoteStackSlot; |
| 4526 | |
| 4527 | if (!CanLowerReturn) { |
| 4528 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize( |
| 4529 | FTy->getReturnType()); |
| 4530 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment( |
| 4531 | FTy->getReturnType()); |
| 4532 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 4533 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4534 | const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
| 4535 | |
| 4536 | DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4537 | Entry.Node = DemoteStackSlot; |
| 4538 | Entry.Ty = StackSlotPtrType; |
| 4539 | Entry.isSExt = false; |
| 4540 | Entry.isZExt = false; |
| 4541 | Entry.isInReg = false; |
| 4542 | Entry.isSRet = true; |
| 4543 | Entry.isNest = false; |
| 4544 | Entry.isByVal = false; |
| 4545 | Entry.Alignment = Align; |
| 4546 | Args.push_back(Entry); |
| 4547 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 4548 | } |
| 4549 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4550 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4551 | i != e; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4552 | SDValue ArgNode = getValue(*i); |
| 4553 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4554 | |
| 4555 | unsigned attrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4556 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 4557 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 4558 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 4559 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 4560 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 4561 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4562 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4563 | Args.push_back(Entry); |
| 4564 | } |
| 4565 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4566 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4567 | // Insert a label before the invoke call to mark the try range. This can be |
| 4568 | // used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4569 | BeginLabel = MMI.getContext().CreateTempSymbol(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 4570 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4571 | // For SjLj, keep track of which landing pads go with which invokes |
| 4572 | // so as to maintain the ordering of pads in the LSDA. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4573 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4574 | if (CallSiteIndex) { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4575 | MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4576 | // Now that the call site is handled, stop tracking it. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4577 | MMI.setCurrentCallSite(0); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4580 | // Both PendingLoads and PendingExports must be flushed here; |
| 4581 | // this call might not return. |
| 4582 | (void)getRoot(); |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 4583 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4586 | // Check if target-independent constraints permit a tail call here. |
| 4587 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
| 4588 | if (isTailCall && |
Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 4589 | !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4590 | isTailCall = false; |
| 4591 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4592 | std::pair<SDValue,SDValue> Result = |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4593 | TLI.LowerCallTo(getRoot(), RetTy, |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4594 | CS.paramHasAttr(0, Attribute::SExt), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4595 | CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(), |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 4596 | CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(), |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 4597 | CS.getCallingConv(), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4598 | isTailCall, |
| 4599 | !CS.getInstruction()->use_empty(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4600 | Callee, Args, DAG, getCurDebugLoc()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4601 | assert((isTailCall || Result.second.getNode()) && |
| 4602 | "Non-null chain expected with non-tail call!"); |
| 4603 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 4604 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4605 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4606 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4607 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4608 | // The instruction result is the result of loading from the |
| 4609 | // hidden sret parameter. |
| 4610 | SmallVector<EVT, 1> PVTs; |
| 4611 | const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
| 4612 | |
| 4613 | ComputeValueVTs(TLI, PtrRetTy, PVTs); |
| 4614 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 4615 | EVT PtrVT = PVTs[0]; |
| 4616 | unsigned NumValues = OutVTs.size(); |
| 4617 | SmallVector<SDValue, 4> Values(NumValues); |
| 4618 | SmallVector<SDValue, 4> Chains(NumValues); |
| 4619 | |
| 4620 | for (unsigned i = 0; i < NumValues; ++i) { |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4621 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, |
| 4622 | DemoteStackSlot, |
| 4623 | DAG.getConstant(Offsets[i], PtrVT)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4624 | SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4625 | Add, NULL, Offsets[i], false, false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4626 | Values[i] = L; |
| 4627 | Chains[i] = L.getValue(1); |
| 4628 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4629 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4630 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 4631 | MVT::Other, &Chains[0], NumValues); |
| 4632 | PendingLoads.push_back(Chain); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4633 | |
| 4634 | // Collect the legal value parts into potentially illegal values |
| 4635 | // that correspond to the original function's return values. |
| 4636 | SmallVector<EVT, 4> RetTys; |
| 4637 | RetTy = FTy->getReturnType(); |
| 4638 | ComputeValueVTs(TLI, RetTy, RetTys); |
| 4639 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 4640 | SmallVector<SDValue, 4> ReturnValues; |
| 4641 | unsigned CurReg = 0; |
| 4642 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 4643 | EVT VT = RetTys[I]; |
| 4644 | EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT); |
| 4645 | unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT); |
| 4646 | |
| 4647 | SDValue ReturnValue = |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4648 | getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs, |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4649 | RegisterVT, VT, AssertOp); |
| 4650 | ReturnValues.push_back(ReturnValue); |
Kenneth Uildriks | 93ae407 | 2010-01-16 23:37:33 +0000 | [diff] [blame] | 4651 | CurReg += NumRegs; |
| 4652 | } |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4653 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4654 | setValue(CS.getInstruction(), |
| 4655 | DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 4656 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 4657 | &ReturnValues[0], ReturnValues.size())); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4658 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 4659 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 4660 | |
| 4661 | // As a special case, a null chain means that a tail call has been emitted and |
| 4662 | // the DAG root is already updated. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4663 | if (Result.second.getNode()) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4664 | DAG.setRoot(Result.second); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4665 | else |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4666 | HasTailCall = true; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4667 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4668 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4669 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4670 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4671 | MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol(); |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 4672 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4673 | |
| 4674 | // Inform MachineModuleInfo of range. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4675 | MMI.addInvoke(LandingPad, BeginLabel, EndLabel); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4676 | } |
| 4677 | } |
| 4678 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4679 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 4680 | /// value is equal or not-equal to zero. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4681 | static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) { |
| 4682 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4683 | UI != E; ++UI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4684 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4685 | if (IC->isEquality()) |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4686 | if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4687 | if (C->isNullValue()) |
| 4688 | continue; |
| 4689 | // Unknown instruction. |
| 4690 | return false; |
| 4691 | } |
| 4692 | return true; |
| 4693 | } |
| 4694 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4695 | static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, |
| 4696 | const Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4697 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4698 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4699 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 4700 | // input is from a string literal. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4701 | if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4702 | // Cast pointer to the type we really want to load. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4703 | LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput), |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4704 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4705 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4706 | if (const Constant *LoadCst = |
| 4707 | ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput), |
| 4708 | Builder.TD)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4709 | return Builder.getValue(LoadCst); |
| 4710 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4711 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4712 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 4713 | // still constant memory, the input chain can be the entry node. |
| 4714 | SDValue Root; |
| 4715 | bool ConstantMemory = false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4716 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4717 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 4718 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 4719 | Root = Builder.DAG.getEntryNode(); |
| 4720 | ConstantMemory = true; |
| 4721 | } else { |
| 4722 | // Do not serialize non-volatile loads against each other. |
| 4723 | Root = Builder.DAG.getRoot(); |
| 4724 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4725 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4726 | SDValue Ptr = Builder.getValue(PtrVal); |
| 4727 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root, |
| 4728 | Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4729 | false /*volatile*/, |
| 4730 | false /*nontemporal*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4731 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4732 | if (!ConstantMemory) |
| 4733 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 4734 | return LoadVal; |
| 4735 | } |
| 4736 | |
| 4737 | |
| 4738 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 4739 | /// If so, return true and lower it, otherwise return false and it will be |
| 4740 | /// lowered like a normal call. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4741 | bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4742 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 4743 | if (I.getNumArgOperands() != 3) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4744 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4745 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4746 | const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4747 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4748 | !I.getArgOperand(2)->getType()->isIntegerTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4749 | !I.getType()->isIntegerTy()) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4750 | return false; |
| 4751 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4752 | const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4753 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4754 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 4755 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4756 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 4757 | bool ActuallyDoIt = true; |
| 4758 | MVT LoadVT; |
| 4759 | const Type *LoadTy; |
| 4760 | switch (Size->getZExtValue()) { |
| 4761 | default: |
| 4762 | LoadVT = MVT::Other; |
| 4763 | LoadTy = 0; |
| 4764 | ActuallyDoIt = false; |
| 4765 | break; |
| 4766 | case 2: |
| 4767 | LoadVT = MVT::i16; |
| 4768 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 4769 | break; |
| 4770 | case 4: |
| 4771 | LoadVT = MVT::i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4772 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4773 | break; |
| 4774 | case 8: |
| 4775 | LoadVT = MVT::i64; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4776 | LoadTy = Type::getInt64Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4777 | break; |
| 4778 | /* |
| 4779 | case 16: |
| 4780 | LoadVT = MVT::v4i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4781 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4782 | LoadTy = VectorType::get(LoadTy, 4); |
| 4783 | break; |
| 4784 | */ |
| 4785 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4786 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4787 | // This turns into unaligned loads. We only do this if the target natively |
| 4788 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 4789 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4790 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4791 | // Require that we can find a legal MVT, and only do this if the target |
| 4792 | // supports unaligned loads of that type. Expanding into byte loads would |
| 4793 | // bloat the code. |
| 4794 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 4795 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 4796 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
| 4797 | if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT)) |
| 4798 | ActuallyDoIt = false; |
| 4799 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4800 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4801 | if (ActuallyDoIt) { |
| 4802 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 4803 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4804 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 4805 | SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal, |
| 4806 | ISD::SETNE); |
| 4807 | EVT CallVT = TLI.getValueType(I.getType(), true); |
| 4808 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT)); |
| 4809 | return true; |
| 4810 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4811 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4812 | |
| 4813 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4814 | return false; |
| 4815 | } |
| 4816 | |
| 4817 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4818 | void SelectionDAGBuilder::visitCall(const CallInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4819 | const char *RenameFn = 0; |
| 4820 | if (Function *F = I.getCalledFunction()) { |
| 4821 | if (F->isDeclaration()) { |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 4822 | const TargetIntrinsicInfo *II = TM.getIntrinsicInfo(); |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 4823 | if (II) { |
| 4824 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 4825 | RenameFn = visitIntrinsicCall(I, IID); |
| 4826 | if (!RenameFn) |
| 4827 | return; |
| 4828 | } |
| 4829 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4830 | if (unsigned IID = F->getIntrinsicID()) { |
| 4831 | RenameFn = visitIntrinsicCall(I, IID); |
| 4832 | if (!RenameFn) |
| 4833 | return; |
| 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4838 | // can't be a library call. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4839 | if (!F->hasLocalLinkage() && F->hasName()) { |
| 4840 | StringRef Name = F->getName(); |
Duncan Sands | d2c817e | 2010-03-14 21:08:40 +0000 | [diff] [blame] | 4841 | if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") { |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 4842 | if (I.getNumArgOperands() == 2 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4843 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 4844 | I.getType() == I.getArgOperand(0)->getType() && |
| 4845 | I.getType() == I.getArgOperand(1)->getType()) { |
| 4846 | SDValue LHS = getValue(I.getArgOperand(0)); |
| 4847 | SDValue RHS = getValue(I.getArgOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4848 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
| 4849 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4850 | return; |
| 4851 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4852 | } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") { |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 4853 | if (I.getNumArgOperands() == 1 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4854 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 4855 | I.getType() == I.getArgOperand(0)->getType()) { |
| 4856 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4857 | setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(), |
| 4858 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4859 | return; |
| 4860 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4861 | } else if (Name == "sin" || Name == "sinf" || Name == "sinl") { |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 4862 | if (I.getNumArgOperands() == 1 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4863 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 4864 | I.getType() == I.getArgOperand(0)->getType() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4865 | I.onlyReadsMemory()) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4866 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4867 | setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(), |
| 4868 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4869 | return; |
| 4870 | } |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 4871 | } else if (Name == "cos" || Name == "cosf" || Name == "cosl") { |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 4872 | if (I.getNumArgOperands() == 1 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4873 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 4874 | I.getType() == I.getArgOperand(0)->getType() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4875 | I.onlyReadsMemory()) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4876 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4877 | setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(), |
| 4878 | Tmp.getValueType(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4879 | return; |
| 4880 | } |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4881 | } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") { |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 4882 | if (I.getNumArgOperands() == 1 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4883 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 4884 | I.getType() == I.getArgOperand(0)->getType() && |
Dale Johannesen | a45bfd3 | 2009-09-25 18:00:35 +0000 | [diff] [blame] | 4885 | I.onlyReadsMemory()) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4886 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4887 | setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(), |
| 4888 | Tmp.getValueType(), Tmp)); |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 4889 | return; |
| 4890 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 4891 | } else if (Name == "memcmp") { |
| 4892 | if (visitMemCmpCall(I)) |
| 4893 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4894 | } |
| 4895 | } |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4896 | } else if (isa<InlineAsm>(I.getCalledValue())) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4897 | visitInlineAsm(&I); |
| 4898 | return; |
| 4899 | } |
| 4900 | |
| 4901 | SDValue Callee; |
| 4902 | if (!RenameFn) |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4903 | Callee = getValue(I.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4904 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4905 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4906 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 4907 | // Check if we can potentially perform a tail call. More detailed checking is |
| 4908 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 4909 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4912 | namespace llvm { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 4913 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4914 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4915 | /// lowering. |
Duncan Sands | 16d8f8b | 2010-05-11 20:16:09 +0000 | [diff] [blame] | 4916 | class LLVM_LIBRARY_VISIBILITY SDISelAsmOperandInfo : |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4917 | public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 4918 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4919 | /// CallOperand - If this is the result output operand or a clobber |
| 4920 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4921 | /// This gets modified as the asm is processed. |
| 4922 | SDValue CallOperand; |
| 4923 | |
| 4924 | /// AssignedRegs - If this is a register or register class operand, this |
| 4925 | /// contains the set of register corresponding to the operand. |
| 4926 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4927 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4928 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4929 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4930 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4931 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4932 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4933 | /// busy in OutputRegs/InputRegs. |
| 4934 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4935 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4936 | std::set<unsigned> &InputRegs, |
| 4937 | const TargetRegisterInfo &TRI) const { |
| 4938 | if (isOutReg) { |
| 4939 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4940 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4941 | } |
| 4942 | if (isInReg) { |
| 4943 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4944 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4945 | } |
| 4946 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4947 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4948 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4949 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4950 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4951 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4952 | const TargetLowering &TLI, |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4953 | const TargetData *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4954 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4955 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4956 | if (isa<BasicBlock>(CallOperandVal)) |
| 4957 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4958 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4959 | const llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4960 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4961 | // If this is an indirect operand, the operand is a pointer to the |
| 4962 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 4963 | if (isIndirect) { |
| 4964 | const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
| 4965 | if (!PtrTy) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 4966 | report_fatal_error("Indirect operand for inline asm not a pointer!"); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 4967 | OpTy = PtrTy->getElementType(); |
| 4968 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4969 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4970 | // If OpTy is not a single value, it may be a struct/union that we |
| 4971 | // can tile with integers. |
| 4972 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4973 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4974 | switch (BitSize) { |
| 4975 | default: break; |
| 4976 | case 1: |
| 4977 | case 8: |
| 4978 | case 16: |
| 4979 | case 32: |
| 4980 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 4981 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4982 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 4983 | break; |
| 4984 | } |
| 4985 | } |
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 | return TLI.getValueType(OpTy, true); |
| 4988 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4989 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4990 | private: |
| 4991 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4992 | /// specified set. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4993 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4994 | const TargetRegisterInfo &TRI) { |
| 4995 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4996 | Regs.insert(Reg); |
| 4997 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4998 | for (; *Aliases; ++Aliases) |
| 4999 | Regs.insert(*Aliases); |
| 5000 | } |
| 5001 | }; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5002 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5003 | } // end llvm namespace. |
| 5004 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5005 | /// isAllocatableRegister - If the specified register is safe to allocate, |
| 5006 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 5007 | /// register class for the register. Otherwise, return null. |
| 5008 | static const TargetRegisterClass * |
| 5009 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 5010 | const TargetLowering &TLI, |
| 5011 | const TargetRegisterInfo *TRI) { |
| 5012 | EVT FoundVT = MVT::Other; |
| 5013 | const TargetRegisterClass *FoundRC = 0; |
| 5014 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 5015 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
| 5016 | EVT ThisVT = MVT::Other; |
| 5017 | |
| 5018 | const TargetRegisterClass *RC = *RCI; |
| 5019 | // If none of the value types for this register class are valid, we |
| 5020 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 5021 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 5022 | I != E; ++I) { |
| 5023 | if (TLI.isTypeLegal(*I)) { |
| 5024 | // If we have already found this register in a different register class, |
| 5025 | // choose the one with the largest VT specified. For example, on |
| 5026 | // PowerPC, we favor f64 register classes over f32. |
| 5027 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
| 5028 | ThisVT = *I; |
| 5029 | break; |
| 5030 | } |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | if (ThisVT == MVT::Other) continue; |
| 5035 | |
| 5036 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 5037 | // frame pointer in functions that need it (due to them not being taken |
| 5038 | // out of allocation, because a variable sized allocation hasn't been seen |
| 5039 | // yet). This is a slight code pessimization, but should still work. |
| 5040 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 5041 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 5042 | if (*I == Reg) { |
| 5043 | // We found a matching register class. Keep looking at others in case |
| 5044 | // we find one with larger registers that this physreg is also in. |
| 5045 | FoundRC = RC; |
| 5046 | FoundVT = ThisVT; |
| 5047 | break; |
| 5048 | } |
| 5049 | } |
| 5050 | return FoundRC; |
| 5051 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5052 | |
| 5053 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 5054 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 5055 | /// register allocator to handle the assignment process. However, if the asm |
| 5056 | /// 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] | 5057 | /// allocation. This produces generally horrible, but correct, code. |
| 5058 | /// |
| 5059 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5060 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 5061 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5062 | void SelectionDAGBuilder:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5063 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5064 | std::set<unsigned> &OutputRegs, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5065 | std::set<unsigned> &InputRegs) { |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 5066 | LLVMContext &Context = FuncInfo.Fn->getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5067 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5068 | // Compute whether this value requires an input register, an output register, |
| 5069 | // or both. |
| 5070 | bool isOutReg = false; |
| 5071 | bool isInReg = false; |
| 5072 | switch (OpInfo.Type) { |
| 5073 | case InlineAsm::isOutput: |
| 5074 | isOutReg = true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5075 | |
| 5076 | // 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] | 5077 | // the input register so no other inputs allocate to it. |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5078 | isInReg = OpInfo.hasMatchingInput(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5079 | break; |
| 5080 | case InlineAsm::isInput: |
| 5081 | isInReg = true; |
| 5082 | isOutReg = false; |
| 5083 | break; |
| 5084 | case InlineAsm::isClobber: |
| 5085 | isOutReg = true; |
| 5086 | isInReg = true; |
| 5087 | break; |
| 5088 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5089 | |
| 5090 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5091 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5092 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5093 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5094 | // If this is a constraint for a single physreg, or a constraint for a |
| 5095 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5096 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5097 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5098 | OpInfo.ConstraintVT); |
| 5099 | |
| 5100 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5101 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5102 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5103 | // cast of the input value. More generally, handle any case where the input |
| 5104 | // value disagrees with the register class we plan to stick this in. |
| 5105 | if (OpInfo.Type == InlineAsm::isInput && |
| 5106 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5107 | // 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] | 5108 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5109 | // vector types). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5110 | EVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5111 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5112 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5113 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5114 | OpInfo.ConstraintVT = RegVT; |
| 5115 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5116 | // If the input is a FP value and we want it in FP registers, do a |
| 5117 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5118 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5119 | // machine. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5120 | RegVT = EVT::getIntegerVT(Context, |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5121 | OpInfo.ConstraintVT.getSizeInBits()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5122 | OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5123 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5124 | OpInfo.ConstraintVT = RegVT; |
| 5125 | } |
| 5126 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5127 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5128 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5129 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5130 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5131 | EVT RegVT; |
| 5132 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5133 | |
| 5134 | // If this is a constraint for a specific physical register, like {r17}, |
| 5135 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5136 | if (unsigned AssignedReg = PhysReg.first) { |
| 5137 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5138 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5139 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5140 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5141 | // Get the actual register value type. This is important, because the user |
| 5142 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5143 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5144 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5145 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5146 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5147 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5148 | |
| 5149 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5150 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5151 | TargetRegisterClass::iterator I = RC->begin(); |
| 5152 | for (; *I != AssignedReg; ++I) |
| 5153 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5154 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5155 | // Already added the first reg. |
| 5156 | --NumRegs; ++I; |
| 5157 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5158 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5159 | Regs.push_back(*I); |
| 5160 | } |
| 5161 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5162 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5163 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5164 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5165 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5166 | return; |
| 5167 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5168 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5169 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5170 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5171 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5172 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5173 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5174 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5175 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5176 | // Create the appropriate number of virtual registers. |
| 5177 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5178 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5179 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5180 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5181 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5182 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5183 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5184 | |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5185 | // This is a reference to a register class that doesn't directly correspond |
| 5186 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 5187 | // registers from the class. |
| 5188 | std::vector<unsigned> RegClassRegs |
| 5189 | = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5190 | OpInfo.ConstraintVT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5191 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5192 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 5193 | unsigned NumAllocated = 0; |
| 5194 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 5195 | unsigned Reg = RegClassRegs[i]; |
| 5196 | // See if this register is available. |
| 5197 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 5198 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 5199 | // Make sure we find consecutive registers. |
| 5200 | NumAllocated = 0; |
| 5201 | continue; |
| 5202 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5203 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5204 | // Check to see if this register is allocatable (i.e. don't give out the |
| 5205 | // stack pointer). |
Chris Lattner | fc9d161 | 2009-03-24 15:22:11 +0000 | [diff] [blame] | 5206 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 5207 | if (!RC) { // Couldn't allocate this register. |
| 5208 | // Reset NumAllocated to make sure we return consecutive registers. |
| 5209 | NumAllocated = 0; |
| 5210 | continue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5211 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5212 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5213 | // Okay, this register is good, we can use it. |
| 5214 | ++NumAllocated; |
| 5215 | |
| 5216 | // If we allocated enough consecutive registers, succeed. |
| 5217 | if (NumAllocated == NumRegs) { |
| 5218 | unsigned RegStart = (i-NumAllocated)+1; |
| 5219 | unsigned RegEnd = i+1; |
| 5220 | // Mark all of the allocated registers used. |
| 5221 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 5222 | Regs.push_back(RegClassRegs[i]); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5223 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5224 | OpInfo.AssignedRegs = RegsForValue(Regs, *RC->vt_begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5225 | OpInfo.ConstraintVT); |
| 5226 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 5227 | return; |
| 5228 | } |
| 5229 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5230 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5231 | // Otherwise, we couldn't allocate enough registers for this. |
| 5232 | } |
| 5233 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5234 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5235 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5236 | void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) { |
| 5237 | const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5238 | |
| 5239 | /// ConstraintOperands - Information about all of the constraints. |
| 5240 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5241 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5242 | std::set<unsigned> OutputRegs, InputRegs; |
| 5243 | |
| 5244 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 5245 | // ConstraintOperands list. |
| 5246 | std::vector<InlineAsm::ConstraintInfo> |
| 5247 | ConstraintInfos = IA->ParseConstraints(); |
| 5248 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5249 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5250 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5251 | SDValue Chain, Flag; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5252 | |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5253 | // We won't need to flush pending loads if this asm doesn't touch |
| 5254 | // memory and is nonvolatile. |
| 5255 | if (hasMemory || IA->hasSideEffects()) |
Dale Johannesen | 97d14fc | 2009-04-18 00:09:40 +0000 | [diff] [blame] | 5256 | Chain = getRoot(); |
Chris Lattner | 6c14729 | 2009-04-30 00:48:50 +0000 | [diff] [blame] | 5257 | else |
| 5258 | Chain = DAG.getRoot(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5259 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5260 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5261 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 5262 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5263 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 5264 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5265 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5266 | EVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5267 | |
| 5268 | // Compute the value type for each operand. |
| 5269 | switch (OpInfo.Type) { |
| 5270 | case InlineAsm::isOutput: |
| 5271 | // Indirect outputs just consume an argument. |
| 5272 | if (OpInfo.isIndirect) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5273 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5274 | break; |
| 5275 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5276 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5277 | // The return value of the call is this value. As such, there is no |
| 5278 | // corresponding argument. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5279 | assert(!CS.getType()->isVoidTy() && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5280 | "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5281 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 5282 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 5283 | } else { |
| 5284 | assert(ResNo == 0 && "Asm only has one result!"); |
| 5285 | OpVT = TLI.getValueType(CS.getType()); |
| 5286 | } |
| 5287 | ++ResNo; |
| 5288 | break; |
| 5289 | case InlineAsm::isInput: |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5290 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5291 | break; |
| 5292 | case InlineAsm::isClobber: |
| 5293 | // Nothing to do. |
| 5294 | break; |
| 5295 | } |
| 5296 | |
| 5297 | // If this is an input or an indirect output, process the call argument. |
| 5298 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5299 | if (OpInfo.CallOperandVal) { |
Dale Johannesen | 5339c55 | 2009-07-20 23:27:39 +0000 | [diff] [blame] | 5300 | // Strip bitcasts, if any. This mostly comes up for functions. |
Dale Johannesen | 7671124 | 2009-08-06 22:45:51 +0000 | [diff] [blame] | 5301 | OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts(); |
| 5302 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5303 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5304 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5305 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5306 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5307 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5308 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5309 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5310 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5311 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5312 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5313 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5314 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5315 | // Second pass over the constraints: compute which constraint option to use |
| 5316 | // and assign registers to constraints that want a specific physreg. |
| 5317 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 5318 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5319 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5320 | // 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] | 5321 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5322 | // other is floating point, or their sizes are different, flag it as an |
| 5323 | // error. |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5324 | if (OpInfo.hasMatchingInput()) { |
| 5325 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
Chris Lattner | 87d677c | 2010-04-07 23:50:38 +0000 | [diff] [blame] | 5326 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5327 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5328 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5329 | Input.ConstraintVT.isInteger()) || |
| 5330 | (OpInfo.ConstraintVT.getSizeInBits() != |
| 5331 | Input.ConstraintVT.getSizeInBits())) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5332 | report_fatal_error("Unsupported asm: input constraint" |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 5333 | " with a matching output constraint of" |
| 5334 | " incompatible type!"); |
Evan Cheng | 09dc9c0 | 2008-12-16 18:21:39 +0000 | [diff] [blame] | 5335 | } |
| 5336 | Input.ConstraintVT = OpInfo.ConstraintVT; |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5337 | } |
| 5338 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5339 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5340 | // Compute the constraint code and ConstraintType to use. |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 5341 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5342 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5343 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5344 | // need to to provide an address for the memory input. |
| 5345 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5346 | !OpInfo.isIndirect) { |
| 5347 | assert(OpInfo.Type == InlineAsm::isInput && |
| 5348 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5349 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5350 | // Memory operands really want the address of the value. If we don't have |
| 5351 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5352 | // it to a stack slot. |
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 | // If the operand is a float, integer, or vector constant, spill to a |
| 5355 | // constant pool entry to get its address. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5356 | const Value *OpVal = OpInfo.CallOperandVal; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5357 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 5358 | isa<ConstantVector>(OpVal)) { |
| 5359 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5360 | TLI.getPointerTy()); |
| 5361 | } else { |
| 5362 | // Otherwise, create a stack slot and emit a store to it before the |
| 5363 | // asm. |
| 5364 | const Type *Ty = OpVal->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5365 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5366 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 5367 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 5368 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5369 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5370 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5371 | OpInfo.CallOperand, StackSlot, NULL, 0, |
| 5372 | false, false, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5373 | OpInfo.CallOperand = StackSlot; |
| 5374 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5375 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5376 | // There is no longer a Value* corresponding to this operand. |
| 5377 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5378 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5379 | // It is now an indirect operand. |
| 5380 | OpInfo.isIndirect = true; |
| 5381 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5382 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5383 | // If this constraint is for a specific register, allocate it before |
| 5384 | // anything else. |
| 5385 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5386 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5387 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5388 | |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5389 | ConstraintInfos.clear(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5390 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5391 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5392 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5393 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5394 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5395 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5396 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5397 | // to be. |
| 5398 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 5399 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5402 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 5403 | std::vector<SDValue> AsmNodeOperands; |
| 5404 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 5405 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 5406 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 5407 | TLI.getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5408 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5409 | // If we have a !srcloc metadata node associated with it, we want to attach |
| 5410 | // this to the ultimately generated inline asm machineinstr. To do this, we |
| 5411 | // pass in the third operand as this (potentially null) inline asm MDNode. |
| 5412 | const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc"); |
| 5413 | AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5414 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5415 | // Loop over all of the inputs, copying the operand values into the |
| 5416 | // appropriate registers and processing the output regs. |
| 5417 | RegsForValue RetValRegs; |
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 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 5420 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5421 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5422 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5423 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 5424 | |
| 5425 | switch (OpInfo.Type) { |
| 5426 | case InlineAsm::isOutput: { |
| 5427 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 5428 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 5429 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 5430 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 5431 | |
| 5432 | // Add information to the INLINEASM node to know about this output. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5433 | unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
| 5434 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5435 | TLI.getPointerTy())); |
| 5436 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 5437 | break; |
| 5438 | } |
| 5439 | |
| 5440 | // Otherwise, this is a register or register class output. |
| 5441 | |
| 5442 | // Copy the output from the appropriate register. Find a register that |
| 5443 | // we can use. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5444 | if (OpInfo.AssignedRegs.Regs.empty()) |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 5445 | report_fatal_error("Couldn't allocate output reg for constraint '" + |
| 5446 | Twine(OpInfo.ConstraintCode) + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5447 | |
| 5448 | // If this is an indirect operand, store through the pointer after the |
| 5449 | // asm. |
| 5450 | if (OpInfo.isIndirect) { |
| 5451 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 5452 | OpInfo.CallOperandVal)); |
| 5453 | } else { |
| 5454 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 5455 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5456 | // Concatenate this output onto the outputs list. |
| 5457 | RetValRegs.append(OpInfo.AssignedRegs); |
| 5458 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5459 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5460 | // Add information to the INLINEASM node to know that this register is |
| 5461 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 5462 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5463 | InlineAsm::Kind_RegDefEarlyClobber : |
| 5464 | InlineAsm::Kind_RegDef, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5465 | false, |
| 5466 | 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5467 | DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5468 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5469 | break; |
| 5470 | } |
| 5471 | case InlineAsm::isInput: { |
| 5472 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5473 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 5474 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5475 | // If this is required to match an output register we have already set, |
| 5476 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5477 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5478 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5479 | // Scan until we find the definition we already emitted of this operand. |
| 5480 | // When we find it, create a RegsForValue operand. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5481 | unsigned CurOp = InlineAsm::Op_FirstOperand; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5482 | for (; OperandNo; --OperandNo) { |
| 5483 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5484 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5485 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5486 | assert((InlineAsm::isRegDefKind(OpFlag) || |
| 5487 | InlineAsm::isRegDefEarlyClobberKind(OpFlag) || |
| 5488 | InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5489 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5492 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5493 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5494 | if (InlineAsm::isRegDefKind(OpFlag) || |
| 5495 | InlineAsm::isRegDefEarlyClobberKind(OpFlag)) { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5496 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 5497 | if (OpInfo.isIndirect) { |
| 5498 | // This happens on gcc/testsuite/gcc.dg/pr8788-1.c |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 5499 | LLVMContext &Ctx = *DAG.getContext(); |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 5500 | Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:" |
| 5501 | " don't know how to handle tied " |
| 5502 | "indirect register inputs"); |
| 5503 | } |
| 5504 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5505 | RegsForValue MatchedRegs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5506 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5507 | EVT RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5508 | MatchedRegs.RegVTs.push_back(RegVT); |
| 5509 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 5510 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5511 | i != e; ++i) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5512 | MatchedRegs.Regs.push_back |
| 5513 | (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5514 | |
| 5515 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5516 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5517 | Chain, &Flag); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5518 | MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5519 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5520 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5521 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5522 | } |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5523 | |
| 5524 | assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!"); |
| 5525 | assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 && |
| 5526 | "Unexpected number of operands"); |
| 5527 | // Add information to the INLINEASM node to know about this input. |
| 5528 | // See InlineAsm.h isUseOperandTiedToDef. |
| 5529 | OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag, |
| 5530 | OpInfo.getMatchedOperand()); |
| 5531 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
| 5532 | TLI.getPointerTy())); |
| 5533 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 5534 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5535 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5536 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5537 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5538 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5539 | "Don't know how to handle indirect other inputs yet!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5540 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5541 | std::vector<SDValue> Ops; |
| 5542 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 5543 | Ops, DAG); |
Chris Lattner | 87d677c | 2010-04-07 23:50:38 +0000 | [diff] [blame] | 5544 | if (Ops.empty()) |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 5545 | report_fatal_error("Invalid operand for inline asm constraint '" + |
| 5546 | Twine(OpInfo.ConstraintCode) + "'!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5547 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5548 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5549 | unsigned ResOpType = |
| 5550 | InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5551 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5552 | TLI.getPointerTy())); |
| 5553 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 5554 | break; |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5558 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 5559 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 5560 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5561 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5562 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5563 | unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 5564 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5565 | TLI.getPointerTy())); |
| 5566 | AsmNodeOperands.push_back(InOperandVal); |
| 5567 | break; |
| 5568 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5569 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5570 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 5571 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 5572 | "Unknown constraint type!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5573 | assert(!OpInfo.isIndirect && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5574 | "Don't know how to handle indirect register inputs yet!"); |
| 5575 | |
| 5576 | // Copy the input into the appropriate registers. |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 5577 | if (OpInfo.AssignedRegs.Regs.empty() || |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5578 | !OpInfo.AssignedRegs.areValueTypesLegal(TLI)) |
Benjamin Kramer | 1bd7335 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 5579 | report_fatal_error("Couldn't allocate input reg for constraint '" + |
| 5580 | Twine(OpInfo.ConstraintCode) + "'!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5581 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5582 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5583 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5584 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5585 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5586 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5587 | break; |
| 5588 | } |
| 5589 | case InlineAsm::isClobber: { |
| 5590 | // Add the clobbered value to the operand list, so that the register |
| 5591 | // allocator is aware that the physreg got clobbered. |
| 5592 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5593 | OpInfo.AssignedRegs.AddInlineAsmOperands( |
| 5594 | InlineAsm::Kind_RegDefEarlyClobber, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5595 | false, 0, DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5596 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5597 | break; |
| 5598 | } |
| 5599 | } |
| 5600 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5601 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 5602 | // 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] | 5603 | AsmNodeOperands[0] = Chain; |
| 5604 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5605 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5606 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5607 | DAG.getVTList(MVT::Other, MVT::Flag), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5608 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 5609 | Flag = Chain.getValue(1); |
| 5610 | |
| 5611 | // If this asm returns a register value, copy the result from that register |
| 5612 | // and set it as the value of the call. |
| 5613 | if (!RetValRegs.Regs.empty()) { |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5614 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5615 | Chain, &Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5616 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5617 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 5618 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5619 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5620 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5621 | // If any of the results of the inline asm is a vector, it may have the |
| 5622 | // wrong width/num elts. This can happen for register classes that can |
| 5623 | // contain multiple different value types. The preg or vreg allocated may |
| 5624 | // not have the same VT as was expected. Convert it to the right type |
| 5625 | // with bit_convert. |
| 5626 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5627 | Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5628 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5629 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5630 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5631 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 5632 | // If a result value was tied to an input value, the computed result may |
| 5633 | // have a wider width than the expected result. Extract the relevant |
| 5634 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 5635 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5636 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5637 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5638 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 5639 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 5640 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5641 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 5642 | // Don't need to use this as a chain in this case. |
| 5643 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 5644 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5645 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5646 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5647 | std::vector<std::pair<SDValue, const Value *> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5648 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5649 | // Process indirect outputs, first output all of the flagged copies out of |
| 5650 | // physregs. |
| 5651 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5652 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5653 | const Value *Ptr = IndirectStoresToEmit[i].second; |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5654 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5655 | Chain, &Flag); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5656 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
| 5657 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5658 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5659 | // Emit the non-flagged stores from the physregs. |
| 5660 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5661 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
| 5662 | SDValue Val = DAG.getStore(Chain, getCurDebugLoc(), |
| 5663 | StoresToEmit[i].first, |
| 5664 | getValue(StoresToEmit[i].second), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5665 | StoresToEmit[i].second, 0, |
| 5666 | false, false, 0); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5667 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5668 | } |
| 5669 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5670 | if (!OutChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5671 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5672 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5673 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5674 | DAG.setRoot(Chain); |
| 5675 | } |
| 5676 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5677 | void SelectionDAGBuilder::visitVAStart(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5678 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
| 5679 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5680 | getValue(I.getArgOperand(0)), |
| 5681 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5684 | void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) { |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 5685 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 5686 | getRoot(), getValue(I.getOperand(0)), |
| 5687 | DAG.getSrcValue(I.getOperand(0))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5688 | setValue(&I, V); |
| 5689 | DAG.setRoot(V.getValue(1)); |
| 5690 | } |
| 5691 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5692 | void SelectionDAGBuilder::visitVAEnd(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5693 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
| 5694 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5695 | getValue(I.getArgOperand(0)), |
| 5696 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5699 | void SelectionDAGBuilder::visitVACopy(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 5700 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
| 5701 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5702 | getValue(I.getArgOperand(0)), |
| 5703 | getValue(I.getArgOperand(1)), |
| 5704 | DAG.getSrcValue(I.getArgOperand(0)), |
| 5705 | DAG.getSrcValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5708 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5709 | /// implementation, which just calls LowerCall. |
| 5710 | /// FIXME: When all targets are |
| 5711 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5712 | std::pair<SDValue, SDValue> |
| 5713 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5714 | bool RetSExt, bool RetZExt, bool isVarArg, |
Tilmann Scheller | 6b61cd1 | 2009-07-03 06:44:53 +0000 | [diff] [blame] | 5715 | bool isInreg, unsigned NumFixedArgs, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 5716 | CallingConv::ID CallConv, bool isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5717 | bool isReturnValueUsed, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5718 | SDValue Callee, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5719 | ArgListTy &Args, SelectionDAG &DAG, |
| 5720 | DebugLoc dl) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5721 | // Handle all of the outgoing arguments. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5722 | SmallVector<ISD::OutputArg, 32> Outs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5723 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5724 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5725 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5726 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5727 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5728 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5729 | const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5730 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 5731 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5732 | ISD::ArgFlagsTy Flags; |
| 5733 | unsigned OriginalAlignment = |
| 5734 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5735 | |
| 5736 | if (Args[i].isZExt) |
| 5737 | Flags.setZExt(); |
| 5738 | if (Args[i].isSExt) |
| 5739 | Flags.setSExt(); |
| 5740 | if (Args[i].isInReg) |
| 5741 | Flags.setInReg(); |
| 5742 | if (Args[i].isSRet) |
| 5743 | Flags.setSRet(); |
| 5744 | if (Args[i].isByVal) { |
| 5745 | Flags.setByVal(); |
| 5746 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5747 | const Type *ElementTy = Ty->getElementType(); |
| 5748 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5749 | unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5750 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5751 | // info is not there but there are cases it cannot get right. |
| 5752 | if (Args[i].Alignment) |
| 5753 | FrameAlign = Args[i].Alignment; |
| 5754 | Flags.setByValAlign(FrameAlign); |
| 5755 | Flags.setByValSize(FrameSize); |
| 5756 | } |
| 5757 | if (Args[i].isNest) |
| 5758 | Flags.setNest(); |
| 5759 | Flags.setOrigAlign(OriginalAlignment); |
| 5760 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5761 | EVT PartVT = getRegisterType(RetTy->getContext(), VT); |
| 5762 | unsigned NumParts = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5763 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5764 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5765 | |
| 5766 | if (Args[i].isSExt) |
| 5767 | ExtendKind = ISD::SIGN_EXTEND; |
| 5768 | else if (Args[i].isZExt) |
| 5769 | ExtendKind = ISD::ZERO_EXTEND; |
| 5770 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5771 | getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 5772 | PartVT, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5773 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5774 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5775 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5776 | ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs); |
| 5777 | if (NumParts > 1 && j == 0) |
| 5778 | MyFlags.Flags.setSplit(); |
| 5779 | else if (j != 0) |
| 5780 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5781 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5782 | Outs.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5783 | } |
| 5784 | } |
| 5785 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5786 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5787 | // Handle the incoming return values from the call. |
| 5788 | SmallVector<ISD::InputArg, 32> Ins; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5789 | SmallVector<EVT, 4> RetTys; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5790 | ComputeValueVTs(*this, RetTy, RetTys); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5791 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5792 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5793 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5794 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5795 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5796 | ISD::InputArg MyFlags; |
| 5797 | MyFlags.VT = RegisterVT; |
| 5798 | MyFlags.Used = isReturnValueUsed; |
| 5799 | if (RetSExt) |
| 5800 | MyFlags.Flags.setSExt(); |
| 5801 | if (RetZExt) |
| 5802 | MyFlags.Flags.setZExt(); |
| 5803 | if (isInreg) |
| 5804 | MyFlags.Flags.setInReg(); |
| 5805 | Ins.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5806 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5809 | SmallVector<SDValue, 4> InVals; |
Evan Cheng | 022d9e1 | 2010-02-02 23:55:14 +0000 | [diff] [blame] | 5810 | Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5811 | Outs, Ins, dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5812 | |
| 5813 | // Verify that the target's LowerCall behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5814 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5815 | "LowerCall didn't return a valid chain!"); |
| 5816 | assert((!isTailCall || InVals.empty()) && |
| 5817 | "LowerCall emitted a return value for a tail call!"); |
| 5818 | assert((isTailCall || InVals.size() == Ins.size()) && |
| 5819 | "LowerCall didn't emit the correct number of values!"); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5820 | |
| 5821 | // For a tail call, the return value is merely live-out and there aren't |
| 5822 | // any nodes in the DAG representing it. Return a special value to |
| 5823 | // indicate that a tail call has been emitted and no more Instructions |
| 5824 | // should be processed in the current block. |
| 5825 | if (isTailCall) { |
| 5826 | DAG.setRoot(Chain); |
| 5827 | return std::make_pair(SDValue(), SDValue()); |
| 5828 | } |
| 5829 | |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 5830 | DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5831 | assert(InVals[i].getNode() && |
| 5832 | "LowerCall emitted a null value!"); |
| 5833 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5834 | "LowerCall emitted a value with the wrong type!"); |
| 5835 | }); |
| 5836 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5837 | // Collect the legal value parts into potentially illegal values |
| 5838 | // that correspond to the original function's return values. |
| 5839 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5840 | if (RetSExt) |
| 5841 | AssertOp = ISD::AssertSext; |
| 5842 | else if (RetZExt) |
| 5843 | AssertOp = ISD::AssertZext; |
| 5844 | SmallVector<SDValue, 4> ReturnValues; |
| 5845 | unsigned CurReg = 0; |
| 5846 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5847 | EVT VT = RetTys[I]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5848 | EVT RegisterVT = getRegisterType(RetTy->getContext(), VT); |
| 5849 | unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5850 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5851 | ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg], |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5852 | NumRegs, RegisterVT, VT, |
| 5853 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5854 | CurReg += NumRegs; |
| 5855 | } |
| 5856 | |
| 5857 | // For a function returning void, there is no return value. We can't create |
| 5858 | // such a node, so we just return a null return value in that case. In |
| 5859 | // that case, nothing will actualy look at the value. |
| 5860 | if (ReturnValues.empty()) |
| 5861 | return std::make_pair(SDValue(), Chain); |
| 5862 | |
| 5863 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, |
| 5864 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5865 | &ReturnValues[0], ReturnValues.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5866 | return std::make_pair(Res, Chain); |
| 5867 | } |
| 5868 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5869 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 5870 | SmallVectorImpl<SDValue> &Results, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5871 | SelectionDAG &DAG) const { |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 5872 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 5873 | if (Res.getNode()) |
| 5874 | Results.push_back(Res); |
| 5875 | } |
| 5876 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5877 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5878 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5879 | return SDValue(); |
| 5880 | } |
| 5881 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5882 | void |
| 5883 | SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) { |
Dan Gohman | c7bd7b7 | 2010-06-21 16:02:28 +0000 | [diff] [blame] | 5884 | SDValue Op = getValue(V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5885 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5886 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5887 | "Copy from a reg to the same reg!"); |
| 5888 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5889 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5890 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5891 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 5892 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5893 | PendingExports.push_back(Chain); |
| 5894 | } |
| 5895 | |
| 5896 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5897 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5898 | void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5899 | // If this is the entry block, emit arguments. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5900 | const Function &F = *LLVMBB->getParent(); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5901 | SelectionDAG &DAG = SDB->DAG; |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5902 | DebugLoc dl = SDB->getCurDebugLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5903 | const TargetData *TD = TLI.getTargetData(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5904 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5905 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5906 | // Check whether the function can return without sret-demotion. |
| 5907 | SmallVector<EVT, 4> OutVTs; |
| 5908 | SmallVector<ISD::ArgFlagsTy, 4> OutsFlags; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5909 | getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5910 | OutVTs, OutsFlags, TLI); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5911 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5912 | FuncInfo->CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), |
| 5913 | F.isVarArg(), |
| 5914 | OutVTs, OutsFlags, DAG); |
| 5915 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5916 | // Put in an sret pointer parameter before all the other parameters. |
| 5917 | SmallVector<EVT, 1> ValueVTs; |
| 5918 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 5919 | |
| 5920 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 5921 | // or one register. |
| 5922 | ISD::ArgFlagsTy Flags; |
| 5923 | Flags.setSRet(); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 5924 | EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5925 | ISD::InputArg RetArg(Flags, RegisterVT, true); |
| 5926 | Ins.push_back(RetArg); |
| 5927 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 5928 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5929 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5930 | unsigned Idx = 1; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5931 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5932 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5933 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5934 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 5935 | bool isArgValueUsed = !I->use_empty(); |
| 5936 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5937 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5938 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5939 | const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5940 | ISD::ArgFlagsTy Flags; |
| 5941 | unsigned OriginalAlignment = |
| 5942 | TD->getABITypeAlignment(ArgTy); |
| 5943 | |
| 5944 | if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 5945 | Flags.setZExt(); |
| 5946 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 5947 | Flags.setSExt(); |
| 5948 | if (F.paramHasAttr(Idx, Attribute::InReg)) |
| 5949 | Flags.setInReg(); |
| 5950 | if (F.paramHasAttr(Idx, Attribute::StructRet)) |
| 5951 | Flags.setSRet(); |
| 5952 | if (F.paramHasAttr(Idx, Attribute::ByVal)) { |
| 5953 | Flags.setByVal(); |
| 5954 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5955 | const Type *ElementTy = Ty->getElementType(); |
| 5956 | unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
| 5957 | unsigned FrameSize = TD->getTypeAllocSize(ElementTy); |
| 5958 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5959 | // this info is not there but there are cases it cannot get right. |
| 5960 | if (F.getParamAlignment(Idx)) |
| 5961 | FrameAlign = F.getParamAlignment(Idx); |
| 5962 | Flags.setByValAlign(FrameAlign); |
| 5963 | Flags.setByValSize(FrameSize); |
| 5964 | } |
| 5965 | if (F.paramHasAttr(Idx, Attribute::Nest)) |
| 5966 | Flags.setNest(); |
| 5967 | Flags.setOrigAlign(OriginalAlignment); |
| 5968 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5969 | EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 5970 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5971 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5972 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed); |
| 5973 | if (NumRegs > 1 && i == 0) |
| 5974 | MyFlags.Flags.setSplit(); |
| 5975 | // if it isn't first piece, alignment must be 1 |
| 5976 | else if (i > 0) |
| 5977 | MyFlags.Flags.setOrigAlign(1); |
| 5978 | Ins.push_back(MyFlags); |
| 5979 | } |
| 5980 | } |
| 5981 | } |
| 5982 | |
| 5983 | // Call the target to set up the argument values. |
| 5984 | SmallVector<SDValue, 8> InVals; |
| 5985 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 5986 | F.isVarArg(), Ins, |
| 5987 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5988 | |
| 5989 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5990 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 5991 | "LowerFormalArguments didn't return a valid chain!"); |
| 5992 | assert(InVals.size() == Ins.size() && |
| 5993 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 5994 | DEBUG({ |
| 5995 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 5996 | assert(InVals[i].getNode() && |
| 5997 | "LowerFormalArguments emitted a null value!"); |
| 5998 | assert(Ins[i].VT == InVals[i].getValueType() && |
| 5999 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 6000 | } |
| 6001 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6002 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6003 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6004 | DAG.setRoot(NewRoot); |
| 6005 | |
| 6006 | // Set up the argument values. |
| 6007 | unsigned i = 0; |
| 6008 | Idx = 1; |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6009 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6010 | // Create a virtual register for the sret pointer, and put in a copy |
| 6011 | // from the sret argument into it. |
| 6012 | SmallVector<EVT, 1> ValueVTs; |
| 6013 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 6014 | EVT VT = ValueVTs[0]; |
| 6015 | EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 6016 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6017 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6018 | RegVT, VT, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6019 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6020 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6021 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
| 6022 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)); |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6023 | FuncInfo->DemoteRegister = SRetReg; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6024 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), |
| 6025 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6026 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6027 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6028 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 6029 | // Idx indexes LLVM arguments. Don't touch it. |
| 6030 | ++i; |
| 6031 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6032 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6033 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6034 | ++I, ++Idx) { |
| 6035 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6036 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6037 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6038 | unsigned NumValues = ValueVTs.size(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 6039 | |
| 6040 | // If this argument is unused then remember its value. It is used to generate |
| 6041 | // debugging information. |
| 6042 | if (I->use_empty() && NumValues) |
| 6043 | SDB->setUnusedArgValue(I, InVals[i]); |
| 6044 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6045 | for (unsigned Value = 0; Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6046 | EVT VT = ValueVTs[Value]; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6047 | EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
| 6048 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6049 | |
| 6050 | if (!I->use_empty()) { |
| 6051 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 6052 | if (F.paramHasAttr(Idx, Attribute::SExt)) |
| 6053 | AssertOp = ISD::AssertSext; |
| 6054 | else if (F.paramHasAttr(Idx, Attribute::ZExt)) |
| 6055 | AssertOp = ISD::AssertZext; |
| 6056 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6057 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6058 | NumParts, PartVT, VT, |
| 6059 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6060 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6061 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6062 | i += NumParts; |
| 6063 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6064 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6065 | if (!I->use_empty()) { |
Evan Cheng | 8e36a5c | 2010-03-29 21:27:30 +0000 | [diff] [blame] | 6066 | SDValue Res; |
| 6067 | if (!ArgValues.empty()) |
| 6068 | Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
| 6069 | SDB->getCurDebugLoc()); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6070 | SDB->setValue(I, Res); |
| 6071 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6072 | // If this argument is live outside of the entry block, insert a copy from |
| 6073 | // 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] | 6074 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6075 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6076 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6077 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6078 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6079 | |
| 6080 | // Finally, if the target has anything special to do, allow it to do so. |
| 6081 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 6082 | EmitFunctionEntryCode(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6083 | } |
| 6084 | |
| 6085 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6086 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6087 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6088 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6089 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6090 | /// the end. |
| 6091 | /// |
| 6092 | void |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6093 | SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6094 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6095 | |
| 6096 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6097 | |
| 6098 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6099 | // from this block. |
| 6100 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6101 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6102 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6103 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6104 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6105 | // If this terminator has multiple identical successors (common for |
| 6106 | // switches), only handle each succ once. |
| 6107 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6108 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6109 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 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. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6114 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 6115 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6116 | // Ignore dead phi's. |
| 6117 | if (PN->use_empty()) continue; |
| 6118 | |
| 6119 | unsigned Reg; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6120 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6121 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6122 | if (const Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6123 | unsigned &RegOut = ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6124 | if (RegOut == 0) { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6125 | RegOut = FuncInfo.CreateRegForValue(C); |
| 6126 | CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6127 | } |
| 6128 | Reg = RegOut; |
| 6129 | } else { |
Dan Gohman | c25ad63 | 2010-07-01 01:33:21 +0000 | [diff] [blame^] | 6130 | DenseMap<const Value *, unsigned>::iterator I = |
| 6131 | FuncInfo.ValueMap.find(PHIOp); |
| 6132 | if (I != FuncInfo.ValueMap.end()) |
| 6133 | Reg = I->second; |
| 6134 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6135 | assert(isa<AllocaInst>(PHIOp) && |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6136 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6137 | "Didn't codegen value into a register!??"); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6138 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
| 6139 | CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | // Remember that this register needs to added to the machine PHI node as |
| 6144 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6145 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6146 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6147 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6148 | EVT VT = ValueVTs[vti]; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6149 | unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6150 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6151 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6152 | Reg += NumRegisters; |
| 6153 | } |
| 6154 | } |
| 6155 | } |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6156 | ConstantsOut.clear(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6157 | } |