Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements routines for translating from LLVM IR into SelectionDAG IR. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 15 | #include "SelectionDAGBuilder.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "SDNodeDbgValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Jakub Staszak | 81bfd71 | 2013-01-10 22:13:13 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ConstantFolding.h" |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Analysis.h" |
| 24 | #include "llvm/CodeGen/FastISel.h" |
| 25 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| 26 | #include "llvm/CodeGen/GCMetadata.h" |
| 27 | #include "llvm/CodeGen/GCStrategy.h" |
| 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 29 | #include "llvm/CodeGen/MachineFunction.h" |
| 30 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 31 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 34 | #include "llvm/CodeGen/SelectionDAG.h" |
Bill Wendling | 0bcbd1d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/CallingConv.h" |
| 37 | #include "llvm/IR/Constants.h" |
| 38 | #include "llvm/IR/DataLayout.h" |
| 39 | #include "llvm/IR/DerivedTypes.h" |
| 40 | #include "llvm/IR/Function.h" |
| 41 | #include "llvm/IR/GlobalVariable.h" |
| 42 | #include "llvm/IR/InlineAsm.h" |
| 43 | #include "llvm/IR/Instructions.h" |
| 44 | #include "llvm/IR/IntrinsicInst.h" |
| 45 | #include "llvm/IR/Intrinsics.h" |
| 46 | #include "llvm/IR/LLVMContext.h" |
| 47 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 48 | #include "llvm/Support/CommandLine.h" |
| 49 | #include "llvm/Support/Debug.h" |
| 50 | #include "llvm/Support/ErrorHandling.h" |
| 51 | #include "llvm/Support/IntegersSubsetMapping.h" |
| 52 | #include "llvm/Support/MathExtras.h" |
| 53 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetFrameLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 57 | #include "llvm/Target/TargetLibraryInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 58 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 59 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 60 | #include <algorithm> |
| 61 | using namespace llvm; |
| 62 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 63 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 64 | /// some float libcalls (6, 8 or 12 bits). |
| 65 | static unsigned LimitFloatPrecision; |
| 66 | |
| 67 | static cl::opt<unsigned, true> |
| 68 | LimitFPPrecision("limit-float-precision", |
| 69 | cl::desc("Generate low-precision inline sequences " |
| 70 | "for some float libcalls"), |
| 71 | cl::location(LimitFloatPrecision), |
| 72 | cl::init(0)); |
| 73 | |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 74 | // Limit the width of DAG chains. This is important in general to prevent |
| 75 | // prevent DAG-based analysis from blowing up. For example, alias analysis and |
| 76 | // load clustering may not complete in reasonable time. It is difficult to |
| 77 | // recognize and avoid this situation within each individual analysis, and |
| 78 | // future analyses are likely to have the same behavior. Limiting DAG width is |
Andrew Trick | b9e6fe1 | 2010-11-20 07:26:51 +0000 | [diff] [blame] | 79 | // the safe approach, and will be especially important with global DAGs. |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 80 | // |
| 81 | // MaxParallelChains default is arbitrarily high to avoid affecting |
| 82 | // optimization, but could be lowered to improve compile time. Any ld-ld-st-st |
Andrew Trick | b9e6fe1 | 2010-11-20 07:26:51 +0000 | [diff] [blame] | 83 | // sequence over this should have been converted to llvm.memcpy by the |
| 84 | // frontend. It easy to induce this behavior with .ll code such as: |
| 85 | // %buffer = alloca [4096 x i8] |
| 86 | // %data = load [4096 x i8]* %argPtr |
| 87 | // store [4096 x i8] %data, [4096 x i8]* %buffer |
Andrew Trick | 778583a | 2011-03-11 17:46:59 +0000 | [diff] [blame] | 88 | static const unsigned MaxParallelChains = 64; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 89 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 90 | static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 91 | const SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 92 | MVT PartVT, EVT ValueVT, const Value *V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 93 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 94 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 95 | /// combined into the value they represent. If the parts combine to a type |
| 96 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 97 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 98 | /// (ISD::AssertSext). |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 99 | static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 100 | const SDValue *Parts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 101 | unsigned NumParts, MVT PartVT, EVT ValueVT, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 102 | const Value *V, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 103 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 104 | if (ValueVT.isVector()) |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 105 | return getCopyFromPartsVector(DAG, DL, Parts, NumParts, |
| 106 | PartVT, ValueVT, V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 107 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 108 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 109 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 110 | SDValue Val = Parts[0]; |
| 111 | |
| 112 | if (NumParts > 1) { |
| 113 | // Assemble the value from multiple parts. |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 114 | if (ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 115 | unsigned PartBits = PartVT.getSizeInBits(); |
| 116 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 117 | |
| 118 | // Assemble the power of 2 part. |
| 119 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 120 | 1 << Log2_32(NumParts) : NumParts; |
| 121 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 122 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 123 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 124 | SDValue Lo, Hi; |
| 125 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 126 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 127 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 128 | if (RoundParts > 2) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 129 | Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 130 | PartVT, HalfVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 131 | Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 132 | RoundParts / 2, PartVT, HalfVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 133 | } else { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 134 | Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]); |
| 135 | Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 136 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 137 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 138 | if (TLI.isBigEndian()) |
| 139 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 141 | Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 142 | |
| 143 | if (RoundParts < NumParts) { |
| 144 | // Assemble the trailing non-power-of-2 part. |
| 145 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 146 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 147 | Hi = getCopyFromParts(DAG, DL, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 148 | Parts + RoundParts, OddParts, PartVT, OddVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 149 | |
| 150 | // Combine the round and odd parts. |
| 151 | Lo = Val; |
| 152 | if (TLI.isBigEndian()) |
| 153 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 154 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 155 | Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi); |
| 156 | Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 157 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 158 | TLI.getPointerTy())); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 159 | Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo); |
| 160 | Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 161 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 162 | } else if (PartVT.isFloatingPoint()) { |
| 163 | // FP split into multiple FP parts (for ppcf128) |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 164 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 165 | "Unexpected split"); |
| 166 | SDValue Lo, Hi; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 167 | Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]); |
| 168 | Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 169 | if (TLI.isBigEndian()) |
| 170 | std::swap(Lo, Hi); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 171 | Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 172 | } else { |
| 173 | // FP split into integer parts (soft fp) |
| 174 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 175 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 176 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 177 | Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | // There is now one part, held in Val. Correct it to match ValueVT. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 182 | EVT PartEVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 183 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 184 | if (PartEVT == ValueVT) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 185 | return Val; |
| 186 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 187 | if (PartEVT.isInteger() && ValueVT.isInteger()) { |
| 188 | if (ValueVT.bitsLT(PartEVT)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 189 | // For a truncate, see if we have any information to |
| 190 | // indicate whether the truncated bits will always be |
| 191 | // zero or sign-extension. |
| 192 | if (AssertOp != ISD::DELETED_NODE) |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 193 | Val = DAG.getNode(AssertOp, DL, PartEVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 194 | DAG.getValueType(ValueVT)); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 195 | return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 197 | return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 200 | if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 201 | // FP_ROUND's are always exact here. |
| 202 | if (ValueVT.bitsLT(Val.getValueType())) |
| 203 | return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, |
Pete Cooper | f57e1c2 | 2012-01-17 01:54:07 +0000 | [diff] [blame] | 204 | DAG.getTargetConstant(1, TLI.getPointerTy())); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 206 | return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 209 | if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits()) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 210 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 211 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 212 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 215 | /// getCopyFromPartsVector - Create a value that contains the specified legal |
| 216 | /// parts combined into the value they represent. If the parts combine to a |
| 217 | /// type larger then ValueVT then AssertOp can be used to specify whether the |
| 218 | /// extra bits are known to be zero (ISD::AssertZext) or sign extended from |
| 219 | /// ValueVT (ISD::AssertSext). |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 220 | static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 221 | const SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 222 | MVT PartVT, EVT ValueVT, const Value *V) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 223 | assert(ValueVT.isVector() && "Not a vector value"); |
| 224 | assert(NumParts > 0 && "No parts to assemble!"); |
| 225 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 226 | SDValue Val = Parts[0]; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 228 | // Handle a multi-element vector. |
| 229 | if (NumParts > 1) { |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 230 | EVT IntermediateVT; |
| 231 | MVT RegisterVT; |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 232 | unsigned NumIntermediates; |
| 233 | unsigned NumRegs = |
| 234 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
| 235 | NumIntermediates, RegisterVT); |
| 236 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 237 | NumParts = NumRegs; // Silence a compiler warning. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 238 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 239 | assert(RegisterVT == Parts[0].getSimpleValueType() && |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 240 | "Part type doesn't match part!"); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 242 | // Assemble the parts into intermediate operands. |
| 243 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 244 | if (NumIntermediates == NumParts) { |
| 245 | // If the register was not expanded, truncate or copy the value, |
| 246 | // as appropriate. |
| 247 | for (unsigned i = 0; i != NumParts; ++i) |
| 248 | Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 249 | PartVT, IntermediateVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 250 | } else if (NumParts > 0) { |
| 251 | // If the intermediate type was expanded, build the intermediate |
| 252 | // operands from the parts. |
| 253 | assert(NumParts % NumIntermediates == 0 && |
| 254 | "Must expand into a divisible number of parts!"); |
| 255 | unsigned Factor = NumParts / NumIntermediates; |
| 256 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 257 | Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 258 | PartVT, IntermediateVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 259 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 261 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 262 | // intermediate operands. |
| 263 | Val = DAG.getNode(IntermediateVT.isVector() ? |
| 264 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL, |
| 265 | ValueVT, &Ops[0], NumIntermediates); |
| 266 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 268 | // There is now one part, held in Val. Correct it to match ValueVT. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 269 | EVT PartEVT = Val.getValueType(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 270 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 271 | if (PartEVT == ValueVT) |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 272 | return Val; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 273 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 274 | if (PartEVT.isVector()) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 275 | // If the element type of the source/dest vectors are the same, but the |
| 276 | // parts vector has more elements than the value vector, then we have a |
| 277 | // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the |
| 278 | // elements we want. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 279 | if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) { |
| 280 | assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() && |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 281 | "Cannot narrow, it would be a lossy transformation"); |
| 282 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val, |
| 283 | DAG.getIntPtrConstant(0)); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 286 | // Vector/Vector bitcast. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 287 | if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 288 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
| 289 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 290 | assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() && |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 291 | "Cannot handle this kind of promotion"); |
| 292 | // Promoted vector extract |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 293 | bool Smaller = ValueVT.bitsLE(PartEVT); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 294 | return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 295 | DL, ValueVT, Val); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 296 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 297 | } |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 298 | |
Eric Christopher | 9aaa02a | 2011-06-01 19:55:10 +0000 | [diff] [blame] | 299 | // Trivial bitcast if the types are the same size and the destination |
| 300 | // vector type is legal. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 301 | if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() && |
Eric Christopher | 9aaa02a | 2011-06-01 19:55:10 +0000 | [diff] [blame] | 302 | TLI.isTypeLegal(ValueVT)) |
| 303 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 304 | |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 305 | // Handle cases such as i8 -> <1 x i1> |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 306 | if (ValueVT.getVectorNumElements() != 1) { |
| 307 | LLVMContext &Ctx = *DAG.getContext(); |
| 308 | Twine ErrMsg("non-trivial scalar-to-vector conversion"); |
| 309 | if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 310 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 311 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 312 | ErrMsg = ErrMsg + ", possible invalid constraint for vector type"; |
| 313 | Ctx.emitError(I, ErrMsg); |
| 314 | } else { |
| 315 | Ctx.emitError(ErrMsg); |
| 316 | } |
Chad Rosier | f0b0755 | 2013-05-01 19:49:26 +0000 | [diff] [blame] | 317 | return DAG.getUNDEF(ValueVT); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 318 | } |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 319 | |
| 320 | if (ValueVT.getVectorNumElements() == 1 && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 321 | ValueVT.getVectorElementType() != PartEVT) { |
| 322 | bool Smaller = ValueVT.bitsLE(PartEVT); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 323 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 324 | DL, ValueVT.getScalarType(), Val); |
| 325 | } |
| 326 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 327 | return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val); |
| 328 | } |
| 329 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 330 | static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 331 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 332 | MVT PartVT, const Value *V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 333 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 334 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 335 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 336 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 337 | static void getCopyToParts(SelectionDAG &DAG, SDLoc DL, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 338 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 339 | MVT PartVT, const Value *V, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 340 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 341 | EVT ValueVT = Val.getValueType(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 342 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 343 | // Handle the vector case separately. |
| 344 | if (ValueVT.isVector()) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 345 | return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 346 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 347 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 348 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 349 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 350 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 351 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 352 | if (NumParts == 0) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 353 | return; |
| 354 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 355 | assert(!ValueVT.isVector() && "Vector case handled elsewhere"); |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 356 | EVT PartEVT = PartVT; |
| 357 | if (PartEVT == ValueVT) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 358 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 359 | Parts[0] = Val; |
| 360 | return; |
| 361 | } |
| 362 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 363 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 364 | // If the parts cover more bits than the value has, promote the value. |
| 365 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 366 | assert(NumParts == 1 && "Do not know what to promote to!"); |
| 367 | Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val); |
| 368 | } else { |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 369 | assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && |
| 370 | ValueVT.isInteger() && |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 371 | "Unknown mismatch!"); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 372 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 373 | Val = DAG.getNode(ExtendKind, DL, ValueVT, Val); |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 374 | if (PartVT == MVT::x86mmx) |
| 375 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 376 | } |
| 377 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 378 | // Different types of the same size. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 379 | assert(NumParts == 1 && PartEVT != ValueVT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 380 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 381 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 382 | // If the parts cover less bits than value has, truncate the value. |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 383 | assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && |
| 384 | ValueVT.isInteger() && |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 385 | "Unknown mismatch!"); |
| 386 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 387 | Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 388 | if (PartVT == MVT::x86mmx) |
| 389 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // The value may have changed - recompute ValueVT. |
| 393 | ValueVT = Val.getValueType(); |
| 394 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 395 | "Failed to tile the value with PartVT!"); |
| 396 | |
| 397 | if (NumParts == 1) { |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 398 | if (PartEVT != ValueVT) { |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 399 | LLVMContext &Ctx = *DAG.getContext(); |
| 400 | Twine ErrMsg("scalar-to-vector conversion failed"); |
| 401 | if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 402 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 403 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 404 | ErrMsg = ErrMsg + ", possible invalid constraint for vector type"; |
| 405 | Ctx.emitError(I, ErrMsg); |
| 406 | } else { |
| 407 | Ctx.emitError(ErrMsg); |
| 408 | } |
| 409 | } |
| 410 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 411 | Parts[0] = Val; |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | // Expand the value into multiple parts. |
| 416 | if (NumParts & (NumParts - 1)) { |
| 417 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 418 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 419 | "Do not know what to expand to!"); |
| 420 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 421 | unsigned RoundBits = RoundParts * PartBits; |
| 422 | unsigned OddParts = NumParts - RoundParts; |
| 423 | SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val, |
| 424 | DAG.getIntPtrConstant(RoundBits)); |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 425 | getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 426 | |
| 427 | if (TLI.isBigEndian()) |
| 428 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 429 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 430 | |
| 431 | NumParts = RoundParts; |
| 432 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 433 | Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
| 434 | } |
| 435 | |
| 436 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 437 | // EXTRACT_ELEMENT. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 438 | Parts[0] = DAG.getNode(ISD::BITCAST, DL, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 439 | EVT::getIntegerVT(*DAG.getContext(), |
| 440 | ValueVT.getSizeInBits()), |
| 441 | Val); |
| 442 | |
| 443 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 444 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 445 | unsigned ThisBits = StepSize * PartBits / 2; |
| 446 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
| 447 | SDValue &Part0 = Parts[i]; |
| 448 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 449 | |
| 450 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, |
| 451 | ThisVT, Part0, DAG.getIntPtrConstant(1)); |
| 452 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, |
| 453 | ThisVT, Part0, DAG.getIntPtrConstant(0)); |
| 454 | |
| 455 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 456 | Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0); |
| 457 | Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (TLI.isBigEndian()) |
| 463 | std::reverse(Parts, Parts + OrigNumParts); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | /// getCopyToPartsVector - Create a series of nodes that contain the specified |
| 468 | /// value split into legal parts. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 469 | static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 470 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 471 | MVT PartVT, const Value *V) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 472 | EVT ValueVT = Val.getValueType(); |
| 473 | assert(ValueVT.isVector() && "Not a vector"); |
| 474 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 475 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 476 | if (NumParts == 1) { |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 477 | EVT PartEVT = PartVT; |
| 478 | if (PartEVT == ValueVT) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 479 | // Nothing to do. |
| 480 | } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
| 481 | // Bitconvert vector->vector case. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 482 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 483 | } else if (PartVT.isVector() && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 484 | PartEVT.getVectorElementType() == ValueVT.getVectorElementType() && |
| 485 | PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 486 | EVT ElementVT = PartVT.getVectorElementType(); |
| 487 | // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in |
| 488 | // undef elements. |
| 489 | SmallVector<SDValue, 16> Ops; |
| 490 | for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i) |
| 491 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
| 492 | ElementVT, Val, DAG.getIntPtrConstant(i))); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 493 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 494 | for (unsigned i = ValueVT.getVectorNumElements(), |
| 495 | e = PartVT.getVectorNumElements(); i != e; ++i) |
| 496 | Ops.push_back(DAG.getUNDEF(ElementVT)); |
| 497 | |
| 498 | Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size()); |
| 499 | |
| 500 | // FIXME: Use CONCAT for 2x -> 4x. |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 501 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 502 | //SDValue UndefElts = DAG.getUNDEF(VectorTy); |
| 503 | //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 504 | } else if (PartVT.isVector() && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 505 | PartEVT.getVectorElementType().bitsGE( |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 506 | ValueVT.getVectorElementType()) && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 507 | PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) { |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 508 | |
| 509 | // Promoted vector extract |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 510 | bool Smaller = PartEVT.bitsLE(ValueVT); |
Nadav Rotem | c6341e6 | 2011-06-19 08:49:38 +0000 | [diff] [blame] | 511 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 512 | DL, PartVT, Val); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 513 | } else{ |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 514 | // Vector -> scalar conversion. |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 515 | assert(ValueVT.getVectorNumElements() == 1 && |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 516 | "Only trivial vector-to-scalar conversions should get here!"); |
| 517 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
| 518 | PartVT, Val, DAG.getIntPtrConstant(0)); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 519 | |
| 520 | bool Smaller = ValueVT.bitsLE(PartVT); |
| 521 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 522 | DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 523 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 524 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 525 | Parts[0] = Val; |
| 526 | return; |
| 527 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 528 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 529 | // Handle a multi-element vector. |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 530 | EVT IntermediateVT; |
| 531 | MVT RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 532 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 533 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
Devang Patel | 8f09bea | 2010-08-26 20:32:32 +0000 | [diff] [blame] | 534 | IntermediateVT, |
| 535 | NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 536 | unsigned NumElements = ValueVT.getVectorNumElements(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 537 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 538 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 539 | NumParts = NumRegs; // Silence a compiler warning. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 540 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 541 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 542 | // Split the vector into intermediate operands. |
| 543 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 544 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 545 | if (IntermediateVT.isVector()) |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 546 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 547 | IntermediateVT, Val, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 548 | DAG.getIntPtrConstant(i * (NumElements / NumIntermediates))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 549 | else |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 550 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 551 | IntermediateVT, Val, DAG.getIntPtrConstant(i)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 552 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 553 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 554 | // Split the intermediate operands into legal parts. |
| 555 | if (NumParts == NumIntermediates) { |
| 556 | // If the register was not expanded, promote or copy the value, |
| 557 | // as appropriate. |
| 558 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 559 | getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 560 | } else if (NumParts > 0) { |
| 561 | // If the intermediate type was expanded, split each the value into |
| 562 | // legal parts. |
| 563 | assert(NumParts % NumIntermediates == 0 && |
| 564 | "Must expand into a divisible number of parts!"); |
| 565 | unsigned Factor = NumParts / NumIntermediates; |
| 566 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 567 | getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 571 | namespace { |
| 572 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 573 | /// that a particular set of values is assigned, and the type information |
| 574 | /// about the value. The most common situation is to represent one value at a |
| 575 | /// time, but struct or array values are handled element-wise as multiple |
| 576 | /// values. The splitting of aggregates is performed recursively, so that we |
| 577 | /// never have aggregate-typed registers. The values at this point do not |
| 578 | /// necessarily have legal types, so each value may require one or more |
| 579 | /// registers of some legal type. |
| 580 | /// |
| 581 | struct RegsForValue { |
| 582 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 583 | /// may need be promoted or synthesized from one or more registers. |
| 584 | /// |
| 585 | SmallVector<EVT, 4> ValueVTs; |
| 586 | |
| 587 | /// RegVTs - The value types of the registers. This is the same size as |
| 588 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 589 | /// register or registers are. (Individual values are never synthesized |
| 590 | /// from more than one type of register.) |
| 591 | /// |
| 592 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 593 | /// getRegisterType member function, however when with physical registers |
| 594 | /// it is necessary to have a separate record of the types. |
| 595 | /// |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 596 | SmallVector<MVT, 4> RegVTs; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 597 | |
| 598 | /// Regs - This list holds the registers assigned to the values. |
| 599 | /// Each legal or promoted value requires one register, and each |
| 600 | /// expanded value requires multiple registers. |
| 601 | /// |
| 602 | SmallVector<unsigned, 4> Regs; |
| 603 | |
| 604 | RegsForValue() {} |
| 605 | |
| 606 | RegsForValue(const SmallVector<unsigned, 4> ®s, |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 607 | MVT regvt, EVT valuevt) |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 608 | : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 609 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 610 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 611 | unsigned Reg, Type *Ty) { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 612 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 613 | |
| 614 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 615 | EVT ValueVT = ValueVTs[Value]; |
| 616 | unsigned NumRegs = tli.getNumRegisters(Context, ValueVT); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 617 | MVT RegisterVT = tli.getRegisterType(Context, ValueVT); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 618 | for (unsigned i = 0; i != NumRegs; ++i) |
| 619 | Regs.push_back(Reg + i); |
| 620 | RegVTs.push_back(RegisterVT); |
| 621 | Reg += NumRegs; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 626 | bool areValueTypesLegal(const TargetLowering &TLI) { |
| 627 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 628 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 629 | if (!TLI.isTypeLegal(RegisterVT)) |
| 630 | return false; |
| 631 | } |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | /// append - Add the specified values to this one. |
| 636 | void append(const RegsForValue &RHS) { |
| 637 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 638 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 639 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 640 | } |
| 641 | |
| 642 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 643 | /// this value and returns the result as a ValueVTs value. This uses |
| 644 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 645 | /// If the Flag pointer is NULL, no flag is used. |
| 646 | SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 647 | SDLoc dl, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 648 | SDValue &Chain, SDValue *Flag, |
| 649 | const Value *V = 0) const; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 650 | |
| 651 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 652 | /// specified value into the registers specified by this object. This uses |
| 653 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 654 | /// If the Flag pointer is NULL, no flag is used. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 655 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 656 | SDValue &Chain, SDValue *Flag, const Value *V) const; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 657 | |
| 658 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 659 | /// operand list. This adds the code marker, matching input operand index |
| 660 | /// (if applicable), and includes the number of values added into it. |
| 661 | void AddInlineAsmOperands(unsigned Kind, |
| 662 | bool HasMatching, unsigned MatchingIdx, |
| 663 | SelectionDAG &DAG, |
| 664 | std::vector<SDValue> &Ops) const; |
| 665 | }; |
| 666 | } |
| 667 | |
| 668 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 669 | /// this value and returns the result as a ValueVT value. This uses |
| 670 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 671 | /// If the Flag pointer is NULL, no flag is used. |
| 672 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
| 673 | FunctionLoweringInfo &FuncInfo, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 674 | SDLoc dl, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 675 | SDValue &Chain, SDValue *Flag, |
| 676 | const Value *V) const { |
Dan Gohman | 7da5d3f | 2010-07-26 18:15:41 +0000 | [diff] [blame] | 677 | // A Value with type {} or [0 x %t] needs no registers. |
| 678 | if (ValueVTs.empty()) |
| 679 | return SDValue(); |
| 680 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 681 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 682 | |
| 683 | // Assemble the legal parts into the final values. |
| 684 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 685 | SmallVector<SDValue, 8> Parts; |
| 686 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 687 | // Copy the legal parts from the registers. |
| 688 | EVT ValueVT = ValueVTs[Value]; |
| 689 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 690 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 691 | |
| 692 | Parts.resize(NumRegs); |
| 693 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 694 | SDValue P; |
| 695 | if (Flag == 0) { |
| 696 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
| 697 | } else { |
| 698 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
| 699 | *Flag = P.getValue(2); |
| 700 | } |
| 701 | |
| 702 | Chain = P.getValue(1); |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 703 | Parts[i] = P; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 704 | |
| 705 | // If the source register was virtual and if we know something about it, |
| 706 | // add an assert node. |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 707 | if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) || |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 708 | !RegisterVT.isInteger() || RegisterVT.isVector()) |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 709 | continue; |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 710 | |
| 711 | const FunctionLoweringInfo::LiveOutInfo *LOI = |
| 712 | FuncInfo.GetLiveOutRegInfo(Regs[Part+i]); |
| 713 | if (!LOI) |
| 714 | continue; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 715 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 716 | unsigned RegSize = RegisterVT.getSizeInBits(); |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 717 | unsigned NumSignBits = LOI->NumSignBits; |
| 718 | unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes(); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 719 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 720 | // FIXME: We capture more information than the dag can represent. For |
| 721 | // now, just use the tightest assertzext/assertsext possible. |
| 722 | bool isSExt = true; |
| 723 | EVT FromVT(MVT::Other); |
| 724 | if (NumSignBits == RegSize) |
| 725 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 726 | else if (NumZeroBits >= RegSize-1) |
| 727 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 728 | else if (NumSignBits > RegSize-8) |
| 729 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
| 730 | else if (NumZeroBits >= RegSize-8) |
| 731 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 732 | else if (NumSignBits > RegSize-16) |
| 733 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
| 734 | else if (NumZeroBits >= RegSize-16) |
| 735 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
| 736 | else if (NumSignBits > RegSize-32) |
| 737 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
| 738 | else if (NumZeroBits >= RegSize-32) |
| 739 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
| 740 | else |
| 741 | continue; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 742 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 743 | // Add an assertion node. |
| 744 | assert(FromVT != MVT::Other); |
| 745 | Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
| 746 | RegisterVT, P, DAG.getValueType(FromVT)); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 750 | NumRegs, RegisterVT, ValueVT, V); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 751 | Part += NumRegs; |
| 752 | Parts.clear(); |
| 753 | } |
| 754 | |
| 755 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 756 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 757 | &Values[0], ValueVTs.size()); |
| 758 | } |
| 759 | |
| 760 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 761 | /// specified value into the registers specified by this object. This uses |
| 762 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 763 | /// If the Flag pointer is NULL, no flag is used. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 764 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 765 | SDValue &Chain, SDValue *Flag, |
| 766 | const Value *V) const { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 767 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 768 | |
| 769 | // Get the list of the values's legal parts. |
| 770 | unsigned NumRegs = Regs.size(); |
| 771 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 772 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 773 | EVT ValueVT = ValueVTs[Value]; |
| 774 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 775 | MVT RegisterVT = RegVTs[Value]; |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 776 | ISD::NodeType ExtendKind = |
| 777 | TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 779 | getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 780 | &Parts[Part], NumParts, RegisterVT, V, ExtendKind); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 781 | Part += NumParts; |
| 782 | } |
| 783 | |
| 784 | // Copy the parts into the registers. |
| 785 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 786 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 787 | SDValue Part; |
| 788 | if (Flag == 0) { |
| 789 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
| 790 | } else { |
| 791 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
| 792 | *Flag = Part.getValue(1); |
| 793 | } |
| 794 | |
| 795 | Chains[i] = Part.getValue(0); |
| 796 | } |
| 797 | |
| 798 | if (NumRegs == 1 || Flag) |
| 799 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
| 800 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 801 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 802 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 803 | // user as well as a successor (the TF operands are flagged to the user). |
| 804 | // c1, f1 = CopyToReg |
| 805 | // c2, f2 = CopyToReg |
| 806 | // c3 = TokenFactor c1, c2 |
| 807 | // ... |
| 808 | // = op c3, ..., f2 |
| 809 | Chain = Chains[NumRegs-1]; |
| 810 | else |
| 811 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
| 812 | } |
| 813 | |
| 814 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 815 | /// operand list. This adds the code marker and includes the number of |
| 816 | /// values added into it. |
| 817 | void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching, |
| 818 | unsigned MatchingIdx, |
| 819 | SelectionDAG &DAG, |
| 820 | std::vector<SDValue> &Ops) const { |
| 821 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 822 | |
| 823 | unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size()); |
| 824 | if (HasMatching) |
| 825 | Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx); |
Jakob Stoklund Olesen | 459b74b | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 826 | else if (!Regs.empty() && |
| 827 | TargetRegisterInfo::isVirtualRegister(Regs.front())) { |
| 828 | // Put the register class of the virtual registers in the flag word. That |
| 829 | // way, later passes can recompute register class constraints for inline |
| 830 | // assembly as well as normal instructions. |
| 831 | // Don't do this for tied operands that can use the regclass information |
| 832 | // from the def. |
| 833 | const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 834 | const TargetRegisterClass *RC = MRI.getRegClass(Regs.front()); |
| 835 | Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID()); |
| 836 | } |
| 837 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 838 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
| 839 | Ops.push_back(Res); |
| 840 | |
| 841 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 842 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 843 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 844 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 845 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
| 846 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
| 847 | } |
| 848 | } |
| 849 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 850 | |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 851 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa, |
| 852 | const TargetLibraryInfo *li) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 853 | AA = &aa; |
| 854 | GFI = gfi; |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 855 | LibInfo = li; |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 856 | TD = DAG.getTarget().getDataLayout(); |
Richard Smith | cb1f68d | 2012-08-22 00:42:39 +0000 | [diff] [blame] | 857 | Context = DAG.getContext(); |
Bill Wendling | 4ed1fb0 | 2011-10-15 01:00:26 +0000 | [diff] [blame] | 858 | LPadToCallSiteMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 861 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 862 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 863 | /// for a new block. This doesn't clear out information about |
| 864 | /// additional blocks that are needed to complete switch lowering |
| 865 | /// or PHI node updating; that information is cleared out as it is |
| 866 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 867 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 868 | NodeMap.clear(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 869 | UnusedArgNodeMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 870 | PendingLoads.clear(); |
| 871 | PendingExports.clear(); |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 872 | CurInst = NULL; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 873 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 876 | /// clearDanglingDebugInfo - Clear the dangling debug information |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 877 | /// map. This function is separated from the clear so that debug |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 878 | /// information that is dangling in a basic block can be properly |
| 879 | /// resolved in a different basic block. This allows the |
| 880 | /// SelectionDAG to resolve dangling debug information attached |
| 881 | /// to PHI nodes. |
| 882 | void SelectionDAGBuilder::clearDanglingDebugInfo() { |
| 883 | DanglingDebugInfoMap.clear(); |
| 884 | } |
| 885 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 886 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 887 | /// flushing any PendingLoad items. This must be done before emitting |
| 888 | /// a store or any other node that may need to be ordered after any |
| 889 | /// prior load instructions. |
| 890 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 891 | SDValue SelectionDAGBuilder::getRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 892 | if (PendingLoads.empty()) |
| 893 | return DAG.getRoot(); |
| 894 | |
| 895 | if (PendingLoads.size() == 1) { |
| 896 | SDValue Root = PendingLoads[0]; |
| 897 | DAG.setRoot(Root); |
| 898 | PendingLoads.clear(); |
| 899 | return Root; |
| 900 | } |
| 901 | |
| 902 | // Otherwise, we have to make a token factor node. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 903 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 904 | &PendingLoads[0], PendingLoads.size()); |
| 905 | PendingLoads.clear(); |
| 906 | DAG.setRoot(Root); |
| 907 | return Root; |
| 908 | } |
| 909 | |
| 910 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 911 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 912 | /// to do this before emitting a terminator instruction. |
| 913 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 914 | SDValue SelectionDAGBuilder::getControlRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 915 | SDValue Root = DAG.getRoot(); |
| 916 | |
| 917 | if (PendingExports.empty()) |
| 918 | return Root; |
| 919 | |
| 920 | // Turn all of the CopyToReg chains into one factored node. |
| 921 | if (Root.getOpcode() != ISD::EntryToken) { |
| 922 | unsigned i = 0, e = PendingExports.size(); |
| 923 | for (; i != e; ++i) { |
| 924 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 925 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 926 | break; // Don't add the root if we already indirectly depend on it. |
| 927 | } |
| 928 | |
| 929 | if (i == e) |
| 930 | PendingExports.push_back(Root); |
| 931 | } |
| 932 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 933 | Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 934 | &PendingExports[0], |
| 935 | PendingExports.size()); |
| 936 | PendingExports.clear(); |
| 937 | DAG.setRoot(Root); |
| 938 | return Root; |
| 939 | } |
| 940 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 941 | void SelectionDAGBuilder::visit(const Instruction &I) { |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 942 | // Set up outgoing PHI node register values before emitting the terminator. |
| 943 | if (isa<TerminatorInst>(&I)) |
| 944 | HandlePHINodesInSuccessorBlocks(I.getParent()); |
| 945 | |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 946 | ++SDNodeOrder; |
| 947 | |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 948 | CurInst = &I; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 949 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 950 | visit(I.getOpcode(), I); |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 951 | |
Dan Gohman | 92884f7 | 2010-04-20 15:03:56 +0000 | [diff] [blame] | 952 | if (!isa<TerminatorInst>(&I) && !HasTailCall) |
| 953 | CopyToExportRegsIfNeeded(&I); |
| 954 | |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 955 | CurInst = NULL; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 958 | void SelectionDAGBuilder::visitPHI(const PHINode &) { |
| 959 | llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!"); |
| 960 | } |
| 961 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 962 | void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 963 | // Note: this doesn't use InstVisitor, because it has to work with |
| 964 | // ConstantExpr's in addition to instructions. |
| 965 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 966 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 967 | // Build the switch statement using the Instruction.def file. |
| 968 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Galina Kistanova | 72ea0c9 | 2012-07-19 04:50:12 +0000 | [diff] [blame] | 969 | case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break; |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 970 | #include "llvm/IR/Instruction.def" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 971 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 972 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 973 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 974 | // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, |
| 975 | // generate the debug data structures now that we've seen its definition. |
| 976 | void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V, |
| 977 | SDValue Val) { |
| 978 | DanglingDebugInfo &DDI = DanglingDebugInfoMap[V]; |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 979 | if (DDI.getDI()) { |
| 980 | const DbgValueInst *DI = DDI.getDI(); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 981 | DebugLoc dl = DDI.getdl(); |
| 982 | unsigned DbgSDNodeOrder = DDI.getSDNodeOrder(); |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 983 | MDNode *Variable = DI->getVariable(); |
| 984 | uint64_t Offset = DI->getOffset(); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 985 | SDDbgValue *SDV; |
| 986 | if (Val.getNode()) { |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 987 | if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 988 | SDV = DAG.getDbgValue(Variable, Val.getNode(), |
| 989 | Val.getResNo(), Offset, dl, DbgSDNodeOrder); |
| 990 | DAG.AddDbgValue(SDV, Val.getNode(), false); |
| 991 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 992 | } else |
Adrian Prantl | 5da4e4f | 2013-05-22 18:02:19 +0000 | [diff] [blame] | 993 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 994 | DanglingDebugInfoMap[V] = DanglingDebugInfo(); |
| 995 | } |
| 996 | } |
| 997 | |
Nick Lewycky | 8de3400 | 2011-09-30 22:19:53 +0000 | [diff] [blame] | 998 | /// getValue - Return an SDValue for the given Value. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 999 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1000 | // If we already have an SDValue for this value, use it. It's important |
| 1001 | // to do this first, so that we don't create a CopyFromReg if we already |
| 1002 | // have a regular SDValue. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1003 | SDValue &N = NodeMap[V]; |
| 1004 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1005 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1006 | // If there's a virtual register allocated and initialized for this |
| 1007 | // value, use it. |
| 1008 | DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V); |
| 1009 | if (It != FuncInfo.ValueMap.end()) { |
| 1010 | unsigned InReg = It->second; |
| 1011 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
| 1012 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1013 | N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V); |
Devang Patel | 8f31428 | 2011-01-25 18:09:58 +0000 | [diff] [blame] | 1014 | resolveDanglingDebugInfo(V, N); |
| 1015 | return N; |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Otherwise create a new SDValue and remember it. |
| 1019 | SDValue Val = getValueImpl(V); |
| 1020 | NodeMap[V] = Val; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1021 | resolveDanglingDebugInfo(V, Val); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1022 | return Val; |
| 1023 | } |
| 1024 | |
| 1025 | /// getNonRegisterValue - Return an SDValue for the given Value, but |
| 1026 | /// don't look in FuncInfo.ValueMap for a virtual register. |
| 1027 | SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) { |
| 1028 | // If we already have an SDValue for this value, use it. |
| 1029 | SDValue &N = NodeMap[V]; |
| 1030 | if (N.getNode()) return N; |
| 1031 | |
| 1032 | // Otherwise create a new SDValue and remember it. |
| 1033 | SDValue Val = getValueImpl(V); |
| 1034 | NodeMap[V] = Val; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1035 | resolveDanglingDebugInfo(V, Val); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1036 | return Val; |
| 1037 | } |
| 1038 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1039 | /// getValueImpl - Helper function for getValue and getNonRegisterValue. |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1040 | /// Create an SDValue for the given value. |
| 1041 | SDValue SelectionDAGBuilder::getValueImpl(const Value *V) { |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1042 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1043 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1044 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1045 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1046 | return DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1047 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1048 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1049 | return DAG.getGlobalAddress(GV, getCurSDLoc(), VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1050 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1051 | if (isa<ConstantPointerNull>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1052 | return DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1053 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1054 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1055 | return DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1056 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1057 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1058 | return DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1059 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1060 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1061 | visit(CE->getOpcode(), *CE); |
| 1062 | SDValue N1 = NodeMap[V]; |
Dan Gohman | ac7d05c | 2010-04-16 16:55:18 +0000 | [diff] [blame] | 1063 | assert(N1.getNode() && "visit didn't populate the NodeMap!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1064 | return N1; |
| 1065 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1066 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1067 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 1068 | SmallVector<SDValue, 4> Constants; |
| 1069 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 1070 | OI != OE; ++OI) { |
| 1071 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 1072 | // If the operand is an empty aggregate, there are no values. |
| 1073 | if (!Val) continue; |
| 1074 | // Add each leaf value from the operand to the Constants list |
| 1075 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1076 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 1077 | Constants.push_back(SDValue(Val, i)); |
| 1078 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1079 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1080 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1081 | getCurSDLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1082 | } |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1083 | |
| 1084 | if (const ConstantDataSequential *CDS = |
| 1085 | dyn_cast<ConstantDataSequential>(C)) { |
| 1086 | SmallVector<SDValue, 4> Ops; |
Chris Lattner | 0f193b8 | 2012-01-25 01:27:20 +0000 | [diff] [blame] | 1087 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1088 | SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode(); |
| 1089 | // Add each leaf value from the operand to the Constants list |
| 1090 | // to form a flattened list of all the values. |
| 1091 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 1092 | Ops.push_back(SDValue(Val, i)); |
| 1093 | } |
| 1094 | |
| 1095 | if (isa<ArrayType>(CDS->getType())) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1096 | return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc()); |
| 1097 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1098 | VT, &Ops[0], Ops.size()); |
| 1099 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1100 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1101 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1102 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 1103 | "Unknown struct or array constant!"); |
| 1104 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1105 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1106 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 1107 | unsigned NumElts = ValueVTs.size(); |
| 1108 | if (NumElts == 0) |
| 1109 | return SDValue(); // empty struct |
| 1110 | SmallVector<SDValue, 4> Constants(NumElts); |
| 1111 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1112 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1113 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1114 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1115 | else if (EltVT.isFloatingPoint()) |
| 1116 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 1117 | else |
| 1118 | Constants[i] = DAG.getConstant(0, EltVT); |
| 1119 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1120 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1121 | return DAG.getMergeValues(&Constants[0], NumElts, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1122 | getCurSDLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1125 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1126 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1128 | VectorType *VecTy = cast<VectorType>(V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1129 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1130 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1131 | // Now that we know the number and type of the elements, get that number of |
| 1132 | // elements into the Ops array based on what kind of constant it is. |
| 1133 | SmallVector<SDValue, 16> Ops; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1134 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1135 | for (unsigned i = 0; i != NumElements; ++i) |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1136 | Ops.push_back(getValue(CV->getOperand(i))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1137 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1138 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1139 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1140 | |
| 1141 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1142 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1143 | Op = DAG.getConstantFP(0, EltVT); |
| 1144 | else |
| 1145 | Op = DAG.getConstant(0, EltVT); |
| 1146 | Ops.assign(NumElements, Op); |
| 1147 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1148 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1149 | // Create a BUILD_VECTOR node. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1150 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1151 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1152 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1153 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1154 | // If this is a static alloca, generate it as the frameindex instead of |
| 1155 | // computation. |
| 1156 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 1157 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 1158 | FuncInfo.StaticAllocaMap.find(AI); |
| 1159 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 1160 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 1161 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1162 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1163 | // If this is an instruction which fast-isel has deferred, select it now. |
| 1164 | if (const Instruction *Inst = dyn_cast<Instruction>(V)) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1165 | unsigned InReg = FuncInfo.InitializeRegForValue(Inst); |
| 1166 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, Inst->getType()); |
| 1167 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1168 | return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1169 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1170 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1171 | llvm_unreachable("Can't get register for value!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1174 | void SelectionDAGBuilder::visitRet(const ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1175 | SDValue Chain = getControlRoot(); |
| 1176 | SmallVector<ISD::OutputArg, 8> Outs; |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1177 | SmallVector<SDValue, 8> OutVals; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1178 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 1179 | if (!FuncInfo.CanLowerReturn) { |
| 1180 | unsigned DemoteReg = FuncInfo.DemoteRegister; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1181 | const Function *F = I.getParent()->getParent(); |
| 1182 | |
| 1183 | // Emit a store of the return value through the virtual register. |
| 1184 | // Leave Outs empty so that LowerReturn won't try to load return |
| 1185 | // registers the usual way. |
| 1186 | SmallVector<EVT, 1> PtrValueVTs; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1187 | ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1188 | PtrValueVTs); |
| 1189 | |
| 1190 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 1191 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1192 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1193 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1194 | SmallVector<uint64_t, 4> Offsets; |
| 1195 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1196 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1197 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1198 | SmallVector<SDValue, 4> Chains(NumValues); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1199 | for (unsigned i = 0; i != NumValues; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1200 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 1201 | RetPtr.getValueType(), RetPtr, |
| 1202 | DAG.getIntPtrConstant(Offsets[i])); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1203 | Chains[i] = |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1204 | DAG.getStore(Chain, getCurSDLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1205 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 1206 | // FIXME: better loc info would be nice. |
| 1207 | Add, MachinePointerInfo(), false, false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1210 | Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1211 | MVT::Other, &Chains[0], NumValues); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 1212 | } else if (I.getNumOperands() != 0) { |
| 1213 | SmallVector<EVT, 4> ValueVTs; |
| 1214 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs); |
| 1215 | unsigned NumValues = ValueVTs.size(); |
| 1216 | if (NumValues) { |
| 1217 | SDValue RetOp = getValue(I.getOperand(0)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1218 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 1219 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1220 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1221 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1222 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1223 | const Function *F = I.getParent()->getParent(); |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1224 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1225 | Attribute::SExt)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1226 | ExtendKind = ISD::SIGN_EXTEND; |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1227 | else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1228 | Attribute::ZExt)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1229 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1230 | |
Cameron Zwarich | 7bbf0ee | 2011-03-17 14:53:37 +0000 | [diff] [blame] | 1231 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) |
Patrik Hagglund | e5c6591 | 2012-12-19 12:02:25 +0000 | [diff] [blame] | 1232 | VT = TLI.getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1233 | |
| 1234 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 1235 | MVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1236 | SmallVector<SDValue, 4> Parts(NumParts); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1237 | getCopyToParts(DAG, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1238 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 1239 | &Parts[0], NumParts, PartVT, &I, ExtendKind); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1240 | |
| 1241 | // 'inreg' on function refers to return value |
| 1242 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1243 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1244 | Attribute::InReg)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1245 | Flags.setInReg(); |
| 1246 | |
| 1247 | // Propagate extension type if any |
Cameron Zwarich | 8df6bf5 | 2011-03-16 22:20:07 +0000 | [diff] [blame] | 1248 | if (ExtendKind == ISD::SIGN_EXTEND) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1249 | Flags.setSExt(); |
Cameron Zwarich | 8df6bf5 | 2011-03-16 22:20:07 +0000 | [diff] [blame] | 1250 | else if (ExtendKind == ISD::ZERO_EXTEND) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1251 | Flags.setZExt(); |
| 1252 | |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1253 | for (unsigned i = 0; i < NumParts; ++i) { |
| 1254 | Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(), |
Manman Ren | 0a1544d | 2012-11-01 23:49:58 +0000 | [diff] [blame] | 1255 | /*isfixed=*/true, 0, 0)); |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1256 | OutVals.push_back(Parts[i]); |
| 1257 | } |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1258 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1259 | } |
| 1260 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1261 | |
| 1262 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1263 | CallingConv::ID CallConv = |
| 1264 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1265 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1266 | Outs, OutVals, getCurSDLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1267 | |
| 1268 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1269 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1270 | "LowerReturn didn't return a valid chain!"); |
| 1271 | |
| 1272 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1273 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1276 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1277 | /// created for it, emit nodes to copy the value into the virtual |
| 1278 | /// registers. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1279 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) { |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 1280 | // Skip empty types |
| 1281 | if (V->getType()->isEmptyTy()) |
| 1282 | return; |
| 1283 | |
Dan Gohman | 33b7a29 | 2010-04-16 17:15:02 +0000 | [diff] [blame] | 1284 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1285 | if (VMI != FuncInfo.ValueMap.end()) { |
| 1286 | assert(!V->use_empty() && "Unused value assigned virtual registers!"); |
| 1287 | CopyValueToVirtualRegister(V, VMI->second); |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1291 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1292 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1293 | /// CopyTo/FromReg. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1294 | void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1295 | // No need to export constants. |
| 1296 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1297 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1298 | // Already exported? |
| 1299 | if (FuncInfo.isExportedInst(V)) return; |
| 1300 | |
| 1301 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1302 | CopyValueToVirtualRegister(V, Reg); |
| 1303 | } |
| 1304 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1305 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1306 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1307 | // The operands of the setcc have to be in this block. We don't know |
| 1308 | // how to export them from some other block. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1309 | if (const Instruction *VI = dyn_cast<Instruction>(V)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1310 | // Can export from current BB. |
| 1311 | if (VI->getParent() == FromBB) |
| 1312 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1313 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1314 | // Is already exported, noop. |
| 1315 | return FuncInfo.isExportedInst(V); |
| 1316 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1317 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1318 | // If this is an argument, we can export it if the BB is the entry block or |
| 1319 | // if it is already exported. |
| 1320 | if (isa<Argument>(V)) { |
| 1321 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1322 | return true; |
| 1323 | |
| 1324 | // Otherwise, can only export this if it is already exported. |
| 1325 | return FuncInfo.isExportedInst(V); |
| 1326 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1327 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1328 | // Otherwise, constants can always be exported. |
| 1329 | return true; |
| 1330 | } |
| 1331 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1332 | /// Return branch probability calculated by BranchProbabilityInfo for IR blocks. |
Jakub Staszak | 25101bb | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 1333 | uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src, |
| 1334 | const MachineBasicBlock *Dst) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1335 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
| 1336 | if (!BPI) |
| 1337 | return 0; |
Jakub Staszak | 95ece8e | 2011-07-29 20:05:36 +0000 | [diff] [blame] | 1338 | const BasicBlock *SrcBB = Src->getBasicBlock(); |
| 1339 | const BasicBlock *DstBB = Dst->getBasicBlock(); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1340 | return BPI->getEdgeWeight(SrcBB, DstBB); |
| 1341 | } |
| 1342 | |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 1343 | void SelectionDAGBuilder:: |
| 1344 | addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst, |
| 1345 | uint32_t Weight /* = 0 */) { |
| 1346 | if (!Weight) |
| 1347 | Weight = getEdgeWeight(Src, Dst); |
| 1348 | Src->addSuccessor(Dst, Weight); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1352 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1353 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1354 | return I->getParent() == BB; |
| 1355 | return true; |
| 1356 | } |
| 1357 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1358 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1359 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1360 | /// AND operator tree. |
| 1361 | /// |
| 1362 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1363 | SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1364 | MachineBasicBlock *TBB, |
| 1365 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1366 | MachineBasicBlock *CurBB, |
| 1367 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1368 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1369 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1370 | // If the leaf of the tree is a comparison, merge the condition into |
| 1371 | // the caseblock. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1372 | if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1373 | // The operands of the cmp have to be in this block. We don't know |
| 1374 | // how to export them from some other block. If this is the first block |
| 1375 | // of the sequence, no exporting is needed. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1376 | if (CurBB == SwitchBB || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1377 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1378 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1379 | ISD::CondCode Condition; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1380 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1381 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1382 | } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1383 | Condition = getFCmpCondCode(FC->getPredicate()); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1384 | if (TM.Options.NoNaNsFPMath) |
| 1385 | Condition = getFCmpCodeWithoutNaN(Condition); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1386 | } else { |
| 1387 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1388 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1389 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1390 | |
| 1391 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1392 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1393 | SwitchCases.push_back(CB); |
| 1394 | return; |
| 1395 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1399 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1400 | NULL, TBB, FBB, CurBB); |
| 1401 | SwitchCases.push_back(CB); |
| 1402 | } |
| 1403 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1404 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1405 | void SelectionDAGBuilder::FindMergedConditions(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1406 | MachineBasicBlock *TBB, |
| 1407 | MachineBasicBlock *FBB, |
| 1408 | MachineBasicBlock *CurBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1409 | MachineBasicBlock *SwitchBB, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1410 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1411 | // 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] | 1412 | const Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1413 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1414 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1415 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1416 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1417 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1418 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1421 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1422 | // Create TmpBB after CurBB. |
| 1423 | MachineFunction::iterator BBI = CurBB; |
| 1424 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1425 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1426 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1427 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1428 | if (Opc == Instruction::Or) { |
| 1429 | // Codegen X | Y as: |
| 1430 | // jmp_if_X TBB |
| 1431 | // jmp TmpBB |
| 1432 | // TmpBB: |
| 1433 | // jmp_if_Y TBB |
| 1434 | // jmp FBB |
| 1435 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1436 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1437 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1438 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1439 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1440 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1441 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1442 | } else { |
| 1443 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1444 | // Codegen X & Y as: |
| 1445 | // jmp_if_X TmpBB |
| 1446 | // jmp FBB |
| 1447 | // TmpBB: |
| 1448 | // jmp_if_Y TBB |
| 1449 | // jmp FBB |
| 1450 | // |
| 1451 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1452 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1453 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1454 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1455 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1456 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1457 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1462 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1463 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1464 | bool |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1465 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1466 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1467 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1468 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1469 | // will get folded into a single comparison, so don't emit two blocks. |
| 1470 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1471 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1472 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1473 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1474 | return false; |
| 1475 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1476 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1477 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1478 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1479 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1480 | Cases[0].CC == Cases[1].CC && |
| 1481 | isa<Constant>(Cases[0].CmpRHS) && |
| 1482 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1483 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1484 | return false; |
| 1485 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1486 | return false; |
| 1487 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 1488 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1489 | return true; |
| 1490 | } |
| 1491 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1492 | void SelectionDAGBuilder::visitBr(const BranchInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1493 | MachineBasicBlock *BrMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1494 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1495 | // Update machine-CFG edges. |
| 1496 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1497 | |
| 1498 | // Figure out which block is immediately after the current one. |
| 1499 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1500 | MachineFunction::iterator BBI = BrMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1501 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1502 | NextBlock = BBI; |
| 1503 | |
| 1504 | if (I.isUnconditional()) { |
| 1505 | // Update machine-CFG edges. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1506 | BrMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1507 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1508 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1509 | if (Succ0MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1510 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1511 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1512 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1513 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1514 | return; |
| 1515 | } |
| 1516 | |
| 1517 | // If this condition is one of the special cases we handle, do special stuff |
| 1518 | // now. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1519 | const Value *CondVal = I.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1520 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1521 | |
| 1522 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1523 | // this as a sequence of branches instead of setcc's with and/or operations. |
Chris Lattner | de189be | 2010-11-30 18:12:52 +0000 | [diff] [blame] | 1524 | // As long as jumps are not expensive, this should improve performance. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1525 | // For example, instead of something like: |
| 1526 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1527 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1528 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1529 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1530 | // or C, F |
| 1531 | // jnz foo |
| 1532 | // Emit: |
| 1533 | // cmp A, B |
| 1534 | // je foo |
| 1535 | // cmp D, E |
| 1536 | // jle foo |
| 1537 | // |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1538 | if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1539 | if (!TLI.isJumpExpensive() && |
Chris Lattner | de189be | 2010-11-30 18:12:52 +0000 | [diff] [blame] | 1540 | BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1541 | (BOp->getOpcode() == Instruction::And || |
| 1542 | BOp->getOpcode() == Instruction::Or)) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1543 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, |
| 1544 | BOp->getOpcode()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1545 | // If the compares in later blocks need to use values not currently |
| 1546 | // exported from this block, export them now. This block should always |
| 1547 | // be the first entry. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1548 | assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1549 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1550 | // Allow some cases to be rejected. |
| 1551 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1552 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1553 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1554 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1555 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1556 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1557 | // Emit the branch for this block. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1558 | visitSwitchCase(SwitchCases[0], BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1559 | SwitchCases.erase(SwitchCases.begin()); |
| 1560 | return; |
| 1561 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1562 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1563 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1564 | // SwitchCases. |
| 1565 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1566 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1567 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1568 | SwitchCases.clear(); |
| 1569 | } |
| 1570 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1571 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1572 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1573 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1574 | NULL, Succ0MBB, Succ1MBB, BrMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1575 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1576 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1577 | // cond branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1578 | visitSwitchCase(CB, BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1582 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1583 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB, |
| 1584 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1585 | SDValue Cond; |
| 1586 | SDValue CondLHS = getValue(CB.CmpLHS); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1587 | SDLoc dl = getCurSDLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1588 | |
| 1589 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1590 | if (CB.CmpMHS == NULL) { |
| 1591 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1592 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1593 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1594 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1595 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1596 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1597 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1598 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1599 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1600 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1601 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1602 | } else { |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1603 | assert(CB.CC == ISD::SETCC_INVALID && |
| 1604 | "Condition is undefined for to-the-range belonging check."); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1605 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1606 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1607 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1608 | |
| 1609 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1610 | EVT VT = CmpOp.getValueType(); |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1611 | |
| 1612 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(false)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1613 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1614 | ISD::SETULE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1615 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1616 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1617 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1618 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1619 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1620 | } |
| 1621 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1622 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1623 | // Update successor info |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 1624 | addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight); |
Jakob Stoklund Olesen | e7fdef4 | 2012-08-20 21:39:52 +0000 | [diff] [blame] | 1625 | // TrueBB and FalseBB are always different unless the incoming IR is |
| 1626 | // degenerate. This only happens when running llc on weird IR. |
| 1627 | if (CB.TrueBB != CB.FalseBB) |
| 1628 | addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1629 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1630 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1631 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1632 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1633 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1634 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1635 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1636 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1637 | // If the lhs block is the next block, invert the condition so that we can |
| 1638 | // fall through to the lhs instead of the rhs block. |
| 1639 | if (CB.TrueBB == NextBlock) { |
| 1640 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1641 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1642 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1643 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1644 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1645 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1646 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1647 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1648 | |
Evan Cheng | 266a99d | 2010-09-23 06:51:55 +0000 | [diff] [blame] | 1649 | // Insert the false branch. Do this even if it's a fall through branch, |
| 1650 | // this makes it easier to do DAG optimizations which require inverting |
| 1651 | // the branch condition. |
| 1652 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1653 | DAG.getBasicBlock(CB.FalseBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1654 | |
| 1655 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1659 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1660 | // Emit the code for the jump table |
| 1661 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1662 | EVT PTy = TLI.getPointerTy(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1663 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1664 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1665 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1666 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(), |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1667 | MVT::Other, Index.getValue(1), |
| 1668 | Table, Index); |
| 1669 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1673 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1674 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1675 | JumpTableHeader &JTH, |
| 1676 | MachineBasicBlock *SwitchBB) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1677 | // Subtract the lowest switch case value from the value being switched on and |
| 1678 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1679 | // difference between smallest and largest cases. |
| 1680 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1681 | EVT VT = SwitchOp.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1682 | SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1683 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1684 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1685 | // 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] | 1686 | // 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] | 1687 | // can be used as an index into the jump table in a subsequent basic block. |
| 1688 | // This value may be smaller or larger than the target's pointer type, and |
| 1689 | // therefore require extension or truncating. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1690 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1691 | |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 1692 | unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1693 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1694 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1695 | JT.Reg = JumpTableReg; |
| 1696 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1697 | // Emit the range check for the jump table, and branch to the default block |
| 1698 | // for the switch statement if the value being switched on exceeds the largest |
| 1699 | // case in the switch. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1700 | SDValue CMP = DAG.getSetCC(getCurSDLoc(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1701 | TLI.getSetCCResultType(*DAG.getContext(), |
| 1702 | Sub.getValueType()), |
| 1703 | Sub, |
| 1704 | DAG.getConstant(JTH.Last - JTH.First,VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1705 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1706 | |
| 1707 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1708 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1709 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1710 | MachineFunction::iterator BBI = SwitchBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1711 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1712 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1713 | NextBlock = BBI; |
| 1714 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1715 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1716 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1717 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1718 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1719 | if (JT.MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1720 | BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond, |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1721 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1722 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1723 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1727 | /// suitable for "bit tests" |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1728 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B, |
| 1729 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1730 | // Subtract the minimum value |
| 1731 | SDValue SwitchOp = getValue(B.SValue); |
Patrik Hagglund | 34525f9 | 2012-12-11 11:14:33 +0000 | [diff] [blame] | 1732 | EVT VT = SwitchOp.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1733 | SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1734 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1735 | |
| 1736 | // Check range |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1737 | SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1738 | TLI.getSetCCResultType(*DAG.getContext(), |
| 1739 | Sub.getValueType()), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1740 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1741 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1742 | |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1743 | // Determine the type of the test operands. |
| 1744 | bool UsePtrType = false; |
| 1745 | if (!TLI.isTypeLegal(VT)) |
| 1746 | UsePtrType = true; |
| 1747 | else { |
| 1748 | for (unsigned i = 0, e = B.Cases.size(); i != e; ++i) |
Eli Friedman | 5c75af6 | 2011-10-12 22:46:45 +0000 | [diff] [blame] | 1749 | if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) { |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1750 | // Switch table case range are encoded into series of masks. |
| 1751 | // Just use pointer type, it's guaranteed to fit. |
| 1752 | UsePtrType = true; |
| 1753 | break; |
| 1754 | } |
| 1755 | } |
| 1756 | if (UsePtrType) { |
| 1757 | VT = TLI.getPointerTy(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1758 | Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT); |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1759 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1760 | |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1761 | B.RegVT = VT.getSimpleVT(); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 1762 | B.Reg = FuncInfo.CreateReg(B.RegVT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1763 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1764 | B.Reg, Sub); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1765 | |
| 1766 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1767 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1768 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1769 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1770 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1771 | NextBlock = BBI; |
| 1772 | |
| 1773 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1774 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1775 | addSuccessorWithWeight(SwitchBB, B.Default); |
| 1776 | addSuccessorWithWeight(SwitchBB, MBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1777 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1778 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1779 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1780 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1781 | |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1782 | if (MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1783 | BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo, |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1784 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1785 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1786 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | /// visitBitTestCase - this function produces one "bit test" |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1790 | void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB, |
| 1791 | MachineBasicBlock* NextMBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1792 | uint32_t BranchWeightToNext, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1793 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1794 | BitTestCase &B, |
| 1795 | MachineBasicBlock *SwitchBB) { |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 1796 | MVT VT = BB.RegVT; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1797 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1798 | Reg, VT); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1799 | SDValue Cmp; |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1800 | unsigned PopCount = CountPopulation_64(B.Mask); |
| 1801 | if (PopCount == 1) { |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1802 | // Testing for a single bit; just compare the shift count with what it |
| 1803 | // would need to be to shift a 1 bit in that position. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1804 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1805 | TLI.getSetCCResultType(*DAG.getContext(), VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1806 | ShiftOp, |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1807 | DAG.getConstant(countTrailingZeros(B.Mask), VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1808 | ISD::SETEQ); |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1809 | } else if (PopCount == BB.Range) { |
| 1810 | // There is only one zero bit in the range, test for it directly. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1811 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1812 | TLI.getSetCCResultType(*DAG.getContext(), VT), |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1813 | ShiftOp, |
| 1814 | DAG.getConstant(CountTrailingOnes_64(B.Mask), VT), |
| 1815 | ISD::SETNE); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1816 | } else { |
| 1817 | // Make desired shift |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1818 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT, |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1819 | DAG.getConstant(1, VT), ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1820 | |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1821 | // Emit bit tests and jumps |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1822 | SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1823 | VT, SwitchVal, DAG.getConstant(B.Mask, VT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1824 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1825 | TLI.getSetCCResultType(*DAG.getContext(), VT), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1826 | AndOp, DAG.getConstant(0, VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1827 | ISD::SETNE); |
| 1828 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1829 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1830 | // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight. |
| 1831 | addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight); |
| 1832 | // The branch weight from SwitchBB to NextMBB is BranchWeightToNext. |
| 1833 | addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1834 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1835 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1836 | MVT::Other, getControlRoot(), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1837 | Cmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1840 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1841 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1842 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1843 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1844 | NextBlock = BBI; |
| 1845 | |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1846 | if (NextMBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1847 | BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd, |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1848 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1849 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1850 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1853 | void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1854 | MachineBasicBlock *InvokeMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1855 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1856 | // Retrieve successors. |
| 1857 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1858 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1859 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1860 | const Value *Callee(I.getCalledValue()); |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1861 | const Function *Fn = dyn_cast<Function>(Callee); |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1862 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1863 | visitInlineAsm(&I); |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1864 | else if (Fn && Fn->isIntrinsic()) { |
| 1865 | assert(Fn->getIntrinsicID() == Intrinsic::donothing); |
Nuno Lopes | 4532bf6 | 2012-07-18 00:07:17 +0000 | [diff] [blame] | 1866 | // Ignore invokes to @llvm.donothing: jump directly to the next BB. |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1867 | } else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1868 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1869 | |
| 1870 | // If the value of the invoke is used outside of its defining block, make it |
| 1871 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1872 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Update successor info |
Chandler Carruth | f264568 | 2011-11-22 11:37:46 +0000 | [diff] [blame] | 1875 | addSuccessorWithWeight(InvokeMBB, Return); |
| 1876 | addSuccessorWithWeight(InvokeMBB, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Drop into normal successor. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1879 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1880 | MVT::Other, getControlRoot(), |
| 1881 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 1884 | void SelectionDAGBuilder::visitResume(const ResumeInst &RI) { |
| 1885 | llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!"); |
| 1886 | } |
| 1887 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1888 | void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) { |
| 1889 | assert(FuncInfo.MBB->isLandingPad() && |
| 1890 | "Call to landingpad not in landing pad!"); |
| 1891 | |
| 1892 | MachineBasicBlock *MBB = FuncInfo.MBB; |
| 1893 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 1894 | AddLandingPadInfo(LP, MMI, MBB); |
| 1895 | |
Bill Wendling | bdf9db6 | 2012-02-13 23:47:16 +0000 | [diff] [blame] | 1896 | // If there aren't registers to copy the values into (e.g., during SjLj |
| 1897 | // exceptions), then don't bother to create these DAG nodes. |
Lang Hames | 0796134 | 2012-02-14 04:45:49 +0000 | [diff] [blame] | 1898 | if (TLI.getExceptionPointerRegister() == 0 && |
Bill Wendling | bdf9db6 | 2012-02-13 23:47:16 +0000 | [diff] [blame] | 1899 | TLI.getExceptionSelectorRegister() == 0) |
| 1900 | return; |
| 1901 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1902 | SmallVector<EVT, 2> ValueVTs; |
| 1903 | ComputeValueVTs(TLI, LP.getType(), ValueVTs); |
| 1904 | |
| 1905 | // Insert the EXCEPTIONADDR instruction. |
| 1906 | assert(FuncInfo.MBB->isLandingPad() && |
| 1907 | "Call to eh.exception not in landing pad!"); |
| 1908 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 1909 | SDValue Ops[2]; |
| 1910 | Ops[0] = DAG.getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1911 | SDValue Op1 = DAG.getNode(ISD::EXCEPTIONADDR, getCurSDLoc(), VTs, Ops, 1); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1912 | SDValue Chain = Op1.getValue(1); |
| 1913 | |
| 1914 | // Insert the EHSELECTION instruction. |
| 1915 | VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 1916 | Ops[0] = Op1; |
| 1917 | Ops[1] = Chain; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1918 | SDValue Op2 = DAG.getNode(ISD::EHSELECTION, getCurSDLoc(), VTs, Ops, 2); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1919 | Chain = Op2.getValue(1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1920 | Op2 = DAG.getSExtOrTrunc(Op2, getCurSDLoc(), MVT::i32); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1921 | |
| 1922 | Ops[0] = Op1; |
| 1923 | Ops[1] = Op2; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1924 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1925 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 1926 | &Ops[0], 2); |
| 1927 | |
| 1928 | std::pair<SDValue, SDValue> RetPair = std::make_pair(Res, Chain); |
| 1929 | setValue(&LP, RetPair.first); |
| 1930 | DAG.setRoot(RetPair.second); |
| 1931 | } |
| 1932 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1933 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1934 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1935 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1936 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1937 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1938 | MachineBasicBlock *Default, |
| 1939 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1940 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1941 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1942 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1943 | return false; |
| 1944 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1945 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1946 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1947 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1948 | |
| 1949 | // Figure out which block is immediately after the current one. |
| 1950 | MachineBasicBlock *NextBlock = 0; |
| 1951 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1952 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1953 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1954 | NextBlock = BBI; |
| 1955 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1956 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1957 | // If any two of the cases has the same destination, and if one value |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1958 | // is the same as the other, but has one bit unset that the other has set, |
| 1959 | // use bit manipulation to do two compares at once. For example: |
| 1960 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1961 | // TODO: This could be extended to merge any 2 cases in switches with 3 cases. |
| 1962 | // TODO: Handle cases where CR.CaseBB != SwitchBB. |
| 1963 | if (Size == 2 && CR.CaseBB == SwitchBB) { |
| 1964 | Case &Small = *CR.Range.first; |
| 1965 | Case &Big = *(CR.Range.second-1); |
| 1966 | |
| 1967 | if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) { |
| 1968 | const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue(); |
| 1969 | const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue(); |
| 1970 | |
| 1971 | // Check that there is only one bit different. |
| 1972 | if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 && |
| 1973 | (SmallValue | BigValue) == BigValue) { |
| 1974 | // Isolate the common bit. |
| 1975 | APInt CommonBit = BigValue & ~SmallValue; |
| 1976 | assert((SmallValue | CommonBit) == BigValue && |
| 1977 | CommonBit.countPopulation() == 1 && "Not a common bit?"); |
| 1978 | |
| 1979 | SDValue CondLHS = getValue(SV); |
| 1980 | EVT VT = CondLHS.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1981 | SDLoc DL = getCurSDLoc(); |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1982 | |
| 1983 | SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS, |
| 1984 | DAG.getConstant(CommonBit, VT)); |
| 1985 | SDValue Cond = DAG.getSetCC(DL, MVT::i1, |
| 1986 | Or, DAG.getConstant(BigValue, VT), |
| 1987 | ISD::SETEQ); |
| 1988 | |
| 1989 | // Update successor info. |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1990 | // Both Small and Big will jump to Small.BB, so we sum up the weights. |
| 1991 | addSuccessorWithWeight(SwitchBB, Small.BB, |
| 1992 | Small.ExtraWeight + Big.ExtraWeight); |
| 1993 | addSuccessorWithWeight(SwitchBB, Default, |
| 1994 | // The default destination is the first successor in IR. |
| 1995 | BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0); |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1996 | |
| 1997 | // Insert the true branch. |
| 1998 | SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other, |
| 1999 | getControlRoot(), Cond, |
| 2000 | DAG.getBasicBlock(Small.BB)); |
| 2001 | |
| 2002 | // Insert the false branch. |
| 2003 | BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond, |
| 2004 | DAG.getBasicBlock(Default)); |
| 2005 | |
| 2006 | DAG.setRoot(BrCond); |
| 2007 | return true; |
| 2008 | } |
| 2009 | } |
| 2010 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2011 | |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2012 | // Order cases by weight so the most likely case will be checked first. |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2013 | uint32_t UnhandledWeights = 0; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2014 | if (BPI) { |
| 2015 | for (CaseItr I = CR.Range.first, IE = CR.Range.second; I != IE; ++I) { |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2016 | uint32_t IWeight = I->ExtraWeight; |
| 2017 | UnhandledWeights += IWeight; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2018 | for (CaseItr J = CR.Range.first; J < I; ++J) { |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2019 | uint32_t JWeight = J->ExtraWeight; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2020 | if (IWeight > JWeight) |
| 2021 | std::swap(*I, *J); |
| 2022 | } |
| 2023 | } |
| 2024 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2025 | // Rearrange the case blocks so that the last one falls through if possible. |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2026 | Case &BackCase = *(CR.Range.second-1); |
Benjamin Kramer | 5db954d | 2012-05-26 21:19:12 +0000 | [diff] [blame] | 2027 | if (Size > 1 && |
| 2028 | NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2029 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 2030 | // branches in this order. See if rearranging a case value would help. |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2031 | // We start at the bottom as it's the case with the least weight. |
Benjamin Kramer | cf1d69d | 2012-05-27 10:56:55 +0000 | [diff] [blame] | 2032 | for (Case *I = &*(CR.Range.second-2), *E = &*CR.Range.first-1; I != E; --I){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2033 | if (I->BB == NextBlock) { |
| 2034 | std::swap(*I, BackCase); |
| 2035 | break; |
| 2036 | } |
| 2037 | } |
| 2038 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2039 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2040 | // Create a CaseBlock record representing a conditional branch to |
| 2041 | // the Case's target mbb if the value being switched on SV is equal |
| 2042 | // to C. |
| 2043 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 2044 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 2045 | MachineBasicBlock *FallThrough; |
| 2046 | if (I != E-1) { |
| 2047 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 2048 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Put SV in a virtual register to make it available from the new blocks. |
| 2051 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2052 | } else { |
| 2053 | // If the last case doesn't match, go to the default block. |
| 2054 | FallThrough = Default; |
| 2055 | } |
| 2056 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2057 | const Value *RHS, *LHS, *MHS; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2058 | ISD::CondCode CC; |
| 2059 | if (I->High == I->Low) { |
| 2060 | // This is just small small case range :) containing exactly 1 case |
| 2061 | CC = ISD::SETEQ; |
| 2062 | LHS = SV; RHS = I->High; MHS = NULL; |
| 2063 | } else { |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2064 | CC = ISD::SETCC_INVALID; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2065 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 2066 | } |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2067 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2068 | // The false weight should be sum of all un-handled cases. |
| 2069 | UnhandledWeights -= I->ExtraWeight; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2070 | CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough, |
| 2071 | /* me */ CurBlock, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2072 | /* trueweight */ I->ExtraWeight, |
| 2073 | /* falseweight */ UnhandledWeights); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2074 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2075 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 2076 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 2077 | // vector to be later processed by SDISel, and insert the node's MBB |
| 2078 | // before the next MBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2079 | if (CurBlock == SwitchBB) |
| 2080 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2081 | else |
| 2082 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2083 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2084 | CurBlock = FallThrough; |
| 2085 | } |
| 2086 | |
| 2087 | return true; |
| 2088 | } |
| 2089 | |
| 2090 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 2091 | return TLI.supportJumpTables() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2092 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 2093 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2094 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2095 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2096 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2097 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2098 | APInt LastExt = Last.zext(BitWidth), FirstExt = First.zext(BitWidth); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2099 | return (LastExt - FirstExt + 1ULL); |
| 2100 | } |
| 2101 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2102 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Chris Lattner | c3ab388 | 2011-09-09 22:06:59 +0000 | [diff] [blame] | 2103 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR, |
| 2104 | CaseRecVector &WorkList, |
| 2105 | const Value *SV, |
| 2106 | MachineBasicBlock *Default, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2107 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2108 | Case& FrontCase = *CR.Range.first; |
| 2109 | Case& BackCase = *(CR.Range.second-1); |
| 2110 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2111 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2112 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2113 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2114 | APInt TSize(First.getBitWidth(), 0); |
Chris Lattner | c3ab388 | 2011-09-09 22:06:59 +0000 | [diff] [blame] | 2115 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2116 | TSize += I->size(); |
| 2117 | |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2118 | if (!areJTsAllowed(TLI) || TSize.ult(TLI.getMinimumJumpTableEntries())) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2119 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2120 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2121 | APInt Range = ComputeRange(First, Last); |
Jakob Stoklund Olesen | 7944391 | 2011-10-26 01:47:48 +0000 | [diff] [blame] | 2122 | // The density is TSize / Range. Require at least 40%. |
| 2123 | // It should not be possible for IntTSize to saturate for sane code, but make |
| 2124 | // sure we handle Range saturation correctly. |
| 2125 | uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10); |
| 2126 | uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10); |
| 2127 | if (IntTSize * 10 < IntRange * 4) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2128 | return false; |
| 2129 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2130 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2131 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
Jakob Stoklund Olesen | 7944391 | 2011-10-26 01:47:48 +0000 | [diff] [blame] | 2132 | << "Range: " << Range << ". Size: " << TSize << ".\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2133 | |
| 2134 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2135 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2136 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2137 | |
| 2138 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2139 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 2140 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2141 | |
| 2142 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2143 | |
| 2144 | // Create a new basic block to hold the code for loading the address |
| 2145 | // of the jump table, and jumping to it. Update successor information; |
| 2146 | // we will either branch to the default case for the switch, or the jump |
| 2147 | // table. |
| 2148 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2149 | CurMF->insert(BBI, JumpTableBB); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 2150 | |
| 2151 | addSuccessorWithWeight(CR.CaseBB, Default); |
| 2152 | addSuccessorWithWeight(CR.CaseBB, JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2153 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2154 | // Build a vector of destination BBs, corresponding to each target |
| 2155 | // of the jump table. If the value of the jump table slot corresponds to |
| 2156 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 2157 | // the default BB. |
| 2158 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2159 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2160 | 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] | 2161 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 2162 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2163 | |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2164 | if (Low.ule(TEI) && TEI.ule(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2165 | DestBBs.push_back(I->BB); |
| 2166 | if (TEI==High) |
| 2167 | ++I; |
| 2168 | } else { |
| 2169 | DestBBs.push_back(Default); |
| 2170 | } |
| 2171 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2172 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2173 | // Calculate weight for each unique destination in CR. |
| 2174 | DenseMap<MachineBasicBlock*, uint32_t> DestWeights; |
| 2175 | if (FuncInfo.BPI) |
| 2176 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 2177 | DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr = |
| 2178 | DestWeights.find(I->BB); |
| 2179 | if (Itr != DestWeights.end()) |
| 2180 | Itr->second += I->ExtraWeight; |
| 2181 | else |
| 2182 | DestWeights[I->BB] = I->ExtraWeight; |
| 2183 | } |
| 2184 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2185 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2186 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 2187 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2188 | E = DestBBs.end(); I != E; ++I) { |
| 2189 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 2190 | SuccsHandled[(*I)->getNumber()] = true; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2191 | DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr = |
| 2192 | DestWeights.find(*I); |
| 2193 | addSuccessorWithWeight(JumpTableBB, *I, |
| 2194 | Itr != DestWeights.end() ? Itr->second : 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2195 | } |
| 2196 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2197 | |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 2198 | // Create a jump table index for this jump table. |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 2199 | unsigned JTEncoding = TLI.getJumpTableEncoding(); |
| 2200 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 2201 | ->createJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2202 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2203 | // Set the jump table information so that we can codegen it as a second |
| 2204 | // MachineBasicBlock |
| 2205 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2206 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB)); |
| 2207 | if (CR.CaseBB == SwitchBB) |
| 2208 | visitJumpTableHeader(JT, JTH, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2209 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2210 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2211 | return true; |
| 2212 | } |
| 2213 | |
| 2214 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 2215 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2216 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 2217 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2218 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2219 | MachineBasicBlock *Default, |
| 2220 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2221 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2222 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2223 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2224 | |
| 2225 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2226 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 2227 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2228 | |
| 2229 | Case& FrontCase = *CR.Range.first; |
| 2230 | Case& BackCase = *(CR.Range.second-1); |
| 2231 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2232 | |
| 2233 | // Size is the number of Cases represented by this range. |
| 2234 | unsigned Size = CR.Range.second - CR.Range.first; |
| 2235 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2236 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2237 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2238 | double FMetric = 0; |
| 2239 | CaseItr Pivot = CR.Range.first + Size/2; |
| 2240 | |
| 2241 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 2242 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2243 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2244 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2245 | I!=E; ++I) |
| 2246 | TSize += I->size(); |
| 2247 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2248 | APInt LSize = FrontCase.size(); |
| 2249 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2250 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2251 | << "First: " << First << ", Last: " << Last <<'\n' |
| 2252 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2253 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 2254 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2255 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 2256 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2257 | APInt Range = ComputeRange(LEnd, RBegin); |
Stepan Dyatkovskiy | c2c52a6 | 2012-05-15 06:50:18 +0000 | [diff] [blame] | 2258 | assert((Range - 2ULL).isNonNegative() && |
| 2259 | "Invalid case distance"); |
Chris Lattner | c3e4e59 | 2011-04-09 06:57:13 +0000 | [diff] [blame] | 2260 | // Use volatile double here to avoid excess precision issues on some hosts, |
| 2261 | // e.g. that use 80-bit X87 registers. |
| 2262 | volatile double LDensity = |
| 2263 | (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2264 | (LEnd - First + 1ULL).roundToDouble(); |
Chris Lattner | c3e4e59 | 2011-04-09 06:57:13 +0000 | [diff] [blame] | 2265 | volatile double RDensity = |
| 2266 | (double)RSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2267 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2268 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2269 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2270 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2271 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 2272 | << "LDensity: " << LDensity |
| 2273 | << ", RDensity: " << RDensity << '\n' |
| 2274 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2275 | if (FMetric < Metric) { |
| 2276 | Pivot = J; |
| 2277 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2278 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | LSize += J->size(); |
| 2282 | RSize -= J->size(); |
| 2283 | } |
| 2284 | if (areJTsAllowed(TLI)) { |
| 2285 | // If our case is dense we *really* should handle it earlier! |
| 2286 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 2287 | } else { |
| 2288 | Pivot = CR.Range.first + Size/2; |
| 2289 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2290 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2291 | CaseRange LHSR(CR.Range.first, Pivot); |
| 2292 | CaseRange RHSR(Pivot, CR.Range.second); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 2293 | const Constant *C = Pivot->Low; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2294 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2295 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2296 | // 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] | 2297 | // 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] | 2298 | // 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] | 2299 | // 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] | 2300 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 2301 | // rather than creating a leaf node for it. |
| 2302 | if ((LHSR.second - LHSR.first) == 1 && |
| 2303 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2304 | cast<ConstantInt>(C)->getValue() == |
| 2305 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2306 | TrueBB = LHSR.first->BB; |
| 2307 | } else { |
| 2308 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2309 | CurMF->insert(BBI, TrueBB); |
| 2310 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2311 | |
| 2312 | // Put SV in a virtual register to make it available from the new blocks. |
| 2313 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2314 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2315 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2316 | // Similar to the optimization above, if the Value being switched on is |
| 2317 | // known to be less than the Constant CR.LT, and the current Case Value |
| 2318 | // is CR.LT - 1, then we can branch directly to the target block for |
| 2319 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 2320 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2321 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 2322 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2323 | FalseBB = RHSR.first->BB; |
| 2324 | } else { |
| 2325 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2326 | CurMF->insert(BBI, FalseBB); |
| 2327 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2328 | |
| 2329 | // Put SV in a virtual register to make it available from the new blocks. |
| 2330 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2334 | // 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] | 2335 | // Otherwise, branch to LHS. |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2336 | CaseBlock CB(ISD::SETULT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2337 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2338 | if (CR.CaseBB == SwitchBB) |
| 2339 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2340 | else |
| 2341 | SwitchCases.push_back(CB); |
| 2342 | |
| 2343 | return true; |
| 2344 | } |
| 2345 | |
| 2346 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 2347 | /// range span less, than machine word bitwidth, encode case range into series |
| 2348 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2349 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 2350 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2351 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2352 | MachineBasicBlock* Default, |
| 2353 | MachineBasicBlock *SwitchBB){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2354 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2355 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2356 | |
| 2357 | Case& FrontCase = *CR.Range.first; |
| 2358 | Case& BackCase = *(CR.Range.second-1); |
| 2359 | |
| 2360 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2361 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2362 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2363 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 2364 | // If target does not have legal shift left, do not emit bit tests at all. |
| 2365 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 2366 | return false; |
| 2367 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2368 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2369 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2370 | I!=E; ++I) { |
| 2371 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2372 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2373 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2374 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2375 | // Count unique destinations |
| 2376 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 2377 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2378 | Dests.insert(I->BB); |
| 2379 | if (Dests.size() > 3) |
| 2380 | // Don't bother the code below, if there are too much unique destinations |
| 2381 | return false; |
| 2382 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2383 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2384 | << Dests.size() << '\n' |
| 2385 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2386 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2387 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2388 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2389 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2390 | APInt cmpRange = maxValue - minValue; |
| 2391 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2392 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2393 | << "Low bound: " << minValue << '\n' |
| 2394 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2395 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 2396 | if (cmpRange.uge(IntPtrBits) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2397 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 2398 | !(Dests.size() == 2 && numCmps >= 5) && |
| 2399 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 2400 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2401 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2402 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2403 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 2404 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2405 | // Optimize the case where all the case values fit in a |
| 2406 | // word without having to subtract minValue. In this case, |
| 2407 | // we can optimize away the subtraction. |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2408 | if (maxValue.ult(IntPtrBits)) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2409 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2410 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2411 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2412 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2413 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2414 | CaseBitsVector CasesBits; |
| 2415 | unsigned i, count = 0; |
| 2416 | |
| 2417 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2418 | MachineBasicBlock* Dest = I->BB; |
| 2419 | for (i = 0; i < count; ++i) |
| 2420 | if (Dest == CasesBits[i].BB) |
| 2421 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2422 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2423 | if (i == count) { |
| 2424 | assert((count < 3) && "Too much destinations to test!"); |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2425 | CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2426 | count++; |
| 2427 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2428 | |
| 2429 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 2430 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 2431 | |
| 2432 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 2433 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2434 | CasesBits[i].ExtraWeight += I->ExtraWeight; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2435 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2436 | for (uint64_t j = lo; j <= hi; j++) { |
| 2437 | CasesBits[i].Mask |= 1ULL << j; |
| 2438 | CasesBits[i].Bits++; |
| 2439 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2440 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2441 | } |
| 2442 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2443 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2444 | BitTestInfo BTC; |
| 2445 | |
| 2446 | // Figure out which block is immediately after the current one. |
| 2447 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2448 | ++BBI; |
| 2449 | |
| 2450 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2451 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2452 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2453 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2454 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2455 | << ", Bits: " << CasesBits[i].Bits |
| 2456 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2457 | |
| 2458 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2459 | CurMF->insert(BBI, CaseBB); |
| 2460 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2461 | CaseBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2462 | CasesBits[i].BB, CasesBits[i].ExtraWeight)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2463 | |
| 2464 | // Put SV in a virtual register to make it available from the new blocks. |
| 2465 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2466 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2467 | |
| 2468 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 2469 | -1U, MVT::Other, (CR.CaseBB == SwitchBB), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2470 | CR.CaseBB, Default, BTC); |
| 2471 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2472 | if (CR.CaseBB == SwitchBB) |
| 2473 | visitBitTestHeader(BTB, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2474 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2475 | BitTestCases.push_back(BTB); |
| 2476 | |
| 2477 | return true; |
| 2478 | } |
| 2479 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2480 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2481 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 2482 | const SwitchInst& SI) { |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2483 | |
| 2484 | /// Use a shorter form of declaration, and also |
| 2485 | /// show the we want to use CRSBuilder as Clusterifier. |
Stepan Dyatkovskiy | 4319a55 | 2012-06-02 07:26:00 +0000 | [diff] [blame] | 2486 | typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier; |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2487 | |
| 2488 | Clusterifier TheClusterifier; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2489 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2490 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2491 | // Start with "simple" cases |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 2492 | for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 2493 | i != e; ++i) { |
| 2494 | const BasicBlock *SuccBB = i.getCaseSuccessor(); |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2495 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB]; |
| 2496 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2497 | TheClusterifier.add(i.getCaseValueEx(), SMBB, |
| 2498 | BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2499 | } |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2500 | |
| 2501 | TheClusterifier.optimize(); |
| 2502 | |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2503 | size_t numCmps = 0; |
| 2504 | for (Clusterifier::RangeIterator i = TheClusterifier.begin(), |
| 2505 | e = TheClusterifier.end(); i != e; ++i, ++numCmps) { |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2506 | Clusterifier::Cluster &C = *i; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2507 | // Update edge weight for the cluster. |
| 2508 | unsigned W = C.first.Weight; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2509 | |
Stepan Dyatkovskiy | 484fc93 | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 2510 | // FIXME: Currently work with ConstantInt based numbers. |
| 2511 | // Changing it to APInt based is a pretty heavy for this commit. |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2512 | Cases.push_back(Case(C.first.getLow().toConstantInt(), |
| 2513 | C.first.getHigh().toConstantInt(), C.second, W)); |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2514 | |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2515 | if (C.first.getLow() != C.first.getHigh()) |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2516 | // A range counts double, since it requires two compares. |
| 2517 | ++numCmps; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | return numCmps; |
| 2521 | } |
| 2522 | |
Jakob Stoklund Olesen | 2622f46 | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 2523 | void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First, |
| 2524 | MachineBasicBlock *Last) { |
| 2525 | // Update JTCases. |
| 2526 | for (unsigned i = 0, e = JTCases.size(); i != e; ++i) |
| 2527 | if (JTCases[i].first.HeaderBB == First) |
| 2528 | JTCases[i].first.HeaderBB = Last; |
| 2529 | |
| 2530 | // Update BitTestCases. |
| 2531 | for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i) |
| 2532 | if (BitTestCases[i].Parent == First) |
| 2533 | BitTestCases[i].Parent = Last; |
| 2534 | } |
| 2535 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2536 | void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 2537 | MachineBasicBlock *SwitchMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2538 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2539 | // Figure out which block is immediately after the current one. |
| 2540 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2541 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2542 | |
| 2543 | // If there is only the default destination, branch to it if it is not the |
| 2544 | // next basic block. Otherwise, just fall through. |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 2545 | if (!SI.getNumCases()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2546 | // Update machine-CFG edges. |
| 2547 | |
| 2548 | // If this is not a fall-through branch, emit the branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2549 | SwitchMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2550 | if (Default != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2551 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2552 | MVT::Other, getControlRoot(), |
| 2553 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2554 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2555 | return; |
| 2556 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2557 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2558 | // If there are any non-default case statements, create a vector of Cases |
| 2559 | // representing each one, and sort the vector so that we can efficiently |
| 2560 | // create a binary search tree from them. |
| 2561 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2562 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2563 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2564 | << ". Total compares: " << numCmps << '\n'); |
Duncan Sands | 17001ce | 2011-10-18 12:44:00 +0000 | [diff] [blame] | 2565 | (void)numCmps; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2566 | |
| 2567 | // Get the Value to be switched on and default basic blocks, which will be |
| 2568 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2569 | // search tree. |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 2570 | const Value *SV = SI.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2571 | |
| 2572 | // Push the initial CaseRec onto the worklist |
| 2573 | CaseRecVector WorkList; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2574 | WorkList.push_back(CaseRec(SwitchMBB,0,0, |
| 2575 | CaseRange(Cases.begin(),Cases.end()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2576 | |
| 2577 | while (!WorkList.empty()) { |
| 2578 | // Grab a record representing a case range to process off the worklist |
| 2579 | CaseRec CR = WorkList.back(); |
| 2580 | WorkList.pop_back(); |
| 2581 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2582 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2583 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2584 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2585 | // If the range has few cases (two or less) emit a series of specific |
| 2586 | // tests. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2587 | if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2588 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2589 | |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2590 | // If the switch has more than N blocks, and is at least 40% dense, and the |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2591 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2592 | // lowering the switch to a binary tree of conditional branches. |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2593 | // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries(). |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2594 | if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2595 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2596 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2597 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2598 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2599 | handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2603 | void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 2604 | MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2605 | |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2606 | // Update machine-CFG edges with unique successors. |
Nadav Rotem | ee0ce15 | 2012-10-23 21:05:33 +0000 | [diff] [blame] | 2607 | SmallSet<BasicBlock*, 32> Done; |
| 2608 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) { |
| 2609 | BasicBlock *BB = I.getSuccessor(i); |
| 2610 | bool Inserted = Done.insert(BB); |
| 2611 | if (!Inserted) |
| 2612 | continue; |
| 2613 | |
| 2614 | MachineBasicBlock *Succ = FuncInfo.MBBMap[BB]; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 2615 | addSuccessorWithWeight(IndirectBrMBB, Succ); |
| 2616 | } |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2617 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2618 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2619 | MVT::Other, getControlRoot(), |
| 2620 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2621 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2622 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2623 | void SelectionDAGBuilder::visitFSub(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2624 | // -0.0 - X --> fneg |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2625 | Type *Ty = I.getType(); |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2626 | if (isa<Constant>(I.getOperand(0)) && |
| 2627 | I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) { |
| 2628 | SDValue Op2 = getValue(I.getOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2629 | setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(), |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2630 | Op2.getValueType(), Op2)); |
| 2631 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2632 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2633 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2634 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2637 | void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2638 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2639 | SDValue Op2 = getValue(I.getOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2640 | setValue(&I, DAG.getNode(OpCode, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2641 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2644 | void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2645 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2646 | SDValue Op2 = getValue(I.getOperand(1)); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2647 | |
Michael Liao | a6b20ce | 2013-03-01 18:40:30 +0000 | [diff] [blame] | 2648 | EVT ShiftTy = TLI.getShiftAmountTy(Op2.getValueType()); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2649 | |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2650 | // Coerce the shift amount to the right type if we can. |
| 2651 | if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) { |
Chris Lattner | 915eeb4 | 2011-02-13 09:10:56 +0000 | [diff] [blame] | 2652 | unsigned ShiftSize = ShiftTy.getSizeInBits(); |
| 2653 | unsigned Op2Size = Op2.getValueType().getSizeInBits(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2654 | SDLoc DL = getCurSDLoc(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2655 | |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2656 | // If the operand is smaller than the shift count type, promote it. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2657 | if (ShiftSize > Op2Size) |
| 2658 | Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2659 | |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2660 | // If the operand is larger than the shift count type but the shift |
| 2661 | // count type has enough bits to represent any shift value, truncate |
| 2662 | // it now. This is a common case and it exposes the truncate to |
| 2663 | // optimization early. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2664 | else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2665 | Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2); |
| 2666 | // Otherwise we'll need to temporarily settle for some other convenient |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 2667 | // type. Type legalization will make adjustments once the shiftee is split. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2668 | else |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 2669 | Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2670 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2671 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2672 | setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2673 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2676 | void SelectionDAGBuilder::visitSDiv(const User &I) { |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2677 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2678 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2679 | |
| 2680 | // Turn exact SDivs into multiplications. |
| 2681 | // FIXME: This should be in DAGCombiner, but it doesn't have access to the |
| 2682 | // exact bit. |
Benjamin Kramer | 3492a4a | 2011-07-08 12:08:24 +0000 | [diff] [blame] | 2683 | if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() && |
| 2684 | !isa<ConstantSDNode>(Op1) && |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2685 | isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2686 | setValue(&I, TLI.BuildExactSDIV(Op1, Op2, getCurSDLoc(), DAG)); |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2687 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2688 | setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2689 | Op1, Op2)); |
| 2690 | } |
| 2691 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2692 | void SelectionDAGBuilder::visitICmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2693 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2694 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2695 | predicate = IC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2696 | else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2697 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2698 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2699 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2700 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2701 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2702 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2703 | setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2706 | void SelectionDAGBuilder::visitFCmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2707 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2708 | if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2709 | predicate = FC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2710 | else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2711 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2712 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2713 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2714 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2715 | if (TM.Options.NoNaNsFPMath) |
| 2716 | Condition = getFCmpCodeWithoutNaN(Condition); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2717 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2718 | setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2721 | void SelectionDAGBuilder::visitSelect(const User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2722 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2723 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2724 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2725 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2726 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2727 | SmallVector<SDValue, 4> Values(NumValues); |
| 2728 | SDValue Cond = getValue(I.getOperand(0)); |
| 2729 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2730 | SDValue FalseVal = getValue(I.getOperand(2)); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 2731 | ISD::NodeType OpCode = Cond.getValueType().isVector() ? |
| 2732 | ISD::VSELECT : ISD::SELECT; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2733 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2734 | for (unsigned i = 0; i != NumValues; ++i) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2735 | Values[i] = DAG.getNode(OpCode, getCurSDLoc(), |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 2736 | TrueVal.getNode()->getValueType(TrueVal.getResNo()+i), |
Chris Lattner | b3e87b2 | 2010-03-12 07:15:36 +0000 | [diff] [blame] | 2737 | Cond, |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2738 | SDValue(TrueVal.getNode(), |
| 2739 | TrueVal.getResNo() + i), |
| 2740 | SDValue(FalseVal.getNode(), |
| 2741 | FalseVal.getResNo() + i)); |
| 2742 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2743 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2744 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2745 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2746 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2747 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2748 | void SelectionDAGBuilder::visitTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2749 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2750 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2751 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2752 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2755 | void SelectionDAGBuilder::visitZExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2756 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2757 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2758 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2759 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2760 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2763 | void SelectionDAGBuilder::visitSExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2764 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2765 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2766 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2767 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2768 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2771 | void SelectionDAGBuilder::visitFPTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2772 | // FPTrunc is never a no-op cast, no need to check |
| 2773 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2774 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2775 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(), |
Pete Cooper | f57e1c2 | 2012-01-17 01:54:07 +0000 | [diff] [blame] | 2776 | DestVT, N, |
| 2777 | DAG.getTargetConstant(0, TLI.getPointerTy()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2780 | void SelectionDAGBuilder::visitFPExt(const User &I){ |
Hal Finkel | 46bb70c | 2011-10-18 03:51:57 +0000 | [diff] [blame] | 2781 | // FPExt is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2782 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2783 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2784 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2787 | void SelectionDAGBuilder::visitFPToUI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2788 | // FPToUI is never a no-op cast, no need to check |
| 2789 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2790 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2791 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2794 | void SelectionDAGBuilder::visitFPToSI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2795 | // FPToSI is never a no-op cast, no need to check |
| 2796 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2797 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2798 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2801 | void SelectionDAGBuilder::visitUIToFP(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2802 | // UIToFP is never a no-op cast, no need to check |
| 2803 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2804 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2805 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2808 | void SelectionDAGBuilder::visitSIToFP(const User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2809 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2810 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2811 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2812 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2815 | void SelectionDAGBuilder::visitPtrToInt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2816 | // What to do depends on the size of the integer and the size of the pointer. |
| 2817 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2818 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2819 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2820 | setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2823 | void SelectionDAGBuilder::visitIntToPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2824 | // What to do depends on the size of the integer and the size of the pointer. |
| 2825 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2826 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2827 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2828 | setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2831 | void SelectionDAGBuilder::visitBitCast(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2832 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2833 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2834 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2835 | // BitCast assures us that source and destination are the same size so this is |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2836 | // either a BITCAST or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2837 | if (DestVT != N.getValueType()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2838 | setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2839 | DestVT, N)); // convert types. |
| 2840 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2841 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2844 | void SelectionDAGBuilder::visitInsertElement(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2845 | SDValue InVec = getValue(I.getOperand(0)); |
| 2846 | SDValue InVal = getValue(I.getOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2847 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2848 | TLI.getPointerTy(), |
| 2849 | getValue(I.getOperand(2))); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2850 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2851 | TLI.getValueType(I.getType()), |
| 2852 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2855 | void SelectionDAGBuilder::visitExtractElement(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2856 | SDValue InVec = getValue(I.getOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2857 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2858 | TLI.getPointerTy(), |
| 2859 | getValue(I.getOperand(1))); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2860 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2861 | TLI.getValueType(I.getType()), InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2864 | // Utility for visitShuffleVector - Return true if every element in Mask, |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 2865 | // beginning from position Pos and ending in Pos+Size, falls within the |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2866 | // specified sequential range [L, L+Pos). or is undef. |
| 2867 | static bool isSequentialInRange(const SmallVectorImpl<int> &Mask, |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 2868 | unsigned Pos, unsigned Size, int Low) { |
| 2869 | for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low) |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2870 | if (Mask[i] >= 0 && Mask[i] != Low) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2871 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2872 | return true; |
| 2873 | } |
| 2874 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2875 | void SelectionDAGBuilder::visitShuffleVector(const User &I) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2876 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2877 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2878 | |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 2879 | SmallVector<int, 8> Mask; |
| 2880 | ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask); |
| 2881 | unsigned MaskNumElts = Mask.size(); |
| 2882 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2883 | EVT VT = TLI.getValueType(I.getType()); |
| 2884 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2885 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2886 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2887 | if (SrcNumElts == MaskNumElts) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2888 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2889 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2890 | return; |
| 2891 | } |
| 2892 | |
| 2893 | // 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] | 2894 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2895 | // Mask is longer than the source vectors and is a multiple of the source |
| 2896 | // 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] | 2897 | // lengths match. |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2898 | if (SrcNumElts*2 == MaskNumElts) { |
| 2899 | // First check for Src1 in low and Src2 in high |
| 2900 | if (isSequentialInRange(Mask, 0, SrcNumElts, 0) && |
| 2901 | isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) { |
| 2902 | // The shuffle is concatenating two vectors together. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2903 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(), |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2904 | VT, Src1, Src2)); |
| 2905 | return; |
| 2906 | } |
| 2907 | // Then check for Src2 in low and Src1 in high |
| 2908 | if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) && |
| 2909 | isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) { |
| 2910 | // The shuffle is concatenating two vectors together. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2911 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(), |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2912 | VT, Src2, Src1)); |
| 2913 | return; |
| 2914 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2917 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2918 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2919 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2920 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2921 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2922 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2923 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2924 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2925 | MOps1[0] = Src1; |
| 2926 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2927 | |
| 2928 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2929 | getCurSDLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2930 | &MOps1[0], NumConcat); |
| 2931 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2932 | getCurSDLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2933 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2934 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2935 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2936 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2937 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2938 | int Idx = Mask[i]; |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 2939 | if (Idx >= (int)SrcNumElts) |
| 2940 | Idx -= SrcNumElts - MaskNumElts; |
| 2941 | MappedOps.push_back(Idx); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2942 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2943 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2944 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2945 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2946 | return; |
| 2947 | } |
| 2948 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2949 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2950 | // Analyze the access pattern of the vector to see if we can extract |
| 2951 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2952 | // the range of elements the mask access on both vectors. |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2953 | int MinRange[2] = { static_cast<int>(SrcNumElts), |
| 2954 | static_cast<int>(SrcNumElts)}; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2955 | int MaxRange[2] = {-1, -1}; |
| 2956 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2957 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2958 | int Idx = Mask[i]; |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2959 | unsigned Input = 0; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2960 | if (Idx < 0) |
| 2961 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2962 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2963 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2964 | Input = 1; |
| 2965 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2966 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2967 | if (Idx > MaxRange[Input]) |
| 2968 | MaxRange[Input] = Idx; |
| 2969 | if (Idx < MinRange[Input]) |
| 2970 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2971 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2972 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2973 | // Check if the access is smaller than the vector size and can we find |
| 2974 | // a reasonable extract index. |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2975 | int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not |
| 2976 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2977 | int StartIdx[2]; // StartIdx to extract from |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2978 | for (unsigned Input = 0; Input < 2; ++Input) { |
| 2979 | if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2980 | RangeUse[Input] = 0; // Unused |
| 2981 | StartIdx[Input] = 0; |
Craig Topper | f873dde | 2012-04-08 17:53:33 +0000 | [diff] [blame] | 2982 | continue; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2983 | } |
Craig Topper | f873dde | 2012-04-08 17:53:33 +0000 | [diff] [blame] | 2984 | |
| 2985 | // Find a good start index that is a multiple of the mask length. Then |
| 2986 | // see if the rest of the elements are in range. |
| 2987 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
| 2988 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
| 2989 | StartIdx[Input] + MaskNumElts <= SrcNumElts) |
| 2990 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 2993 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2994 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2995 | return; |
| 2996 | } |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2997 | if (RangeUse[0] >= 0 && RangeUse[1] >= 0) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2998 | // Extract appropriate subvector and generate a vector shuffle |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2999 | for (unsigned Input = 0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 3000 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3001 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3002 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3003 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3004 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3005 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3006 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3007 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3008 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3009 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3010 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3011 | int Idx = Mask[i]; |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3012 | if (Idx >= 0) { |
| 3013 | if (Idx < (int)SrcNumElts) |
| 3014 | Idx -= StartIdx[0]; |
| 3015 | else |
| 3016 | Idx -= SrcNumElts + StartIdx[1] - MaskNumElts; |
| 3017 | } |
| 3018 | MappedOps.push_back(Idx); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3019 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3020 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3021 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3022 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3023 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3024 | } |
| 3025 | } |
| 3026 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3027 | // We can't use either concat vectors or extract subvectors so fall back to |
| 3028 | // replacing the shuffle with extract and build vector. |
| 3029 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3030 | EVT EltVT = VT.getVectorElementType(); |
| 3031 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3032 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3033 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3034 | int Idx = Mask[i]; |
| 3035 | SDValue Res; |
| 3036 | |
| 3037 | if (Idx < 0) { |
| 3038 | Res = DAG.getUNDEF(EltVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3039 | } else { |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3040 | SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2; |
| 3041 | if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3042 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3043 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(), |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3044 | EltVT, Src, DAG.getConstant(Idx, PtrVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3045 | } |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3046 | |
| 3047 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3048 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3049 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3050 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3051 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3054 | void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3055 | const Value *Op0 = I.getOperand(0); |
| 3056 | const Value *Op1 = I.getOperand(1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3057 | Type *AggTy = I.getType(); |
| 3058 | Type *ValTy = Op1->getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3059 | bool IntoUndef = isa<UndefValue>(Op0); |
| 3060 | bool FromUndef = isa<UndefValue>(Op1); |
| 3061 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3062 | unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3063 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3064 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3065 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3066 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3067 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 3068 | |
| 3069 | unsigned NumAggValues = AggValueVTs.size(); |
| 3070 | unsigned NumValValues = ValValueVTs.size(); |
| 3071 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 3072 | |
| 3073 | SDValue Agg = getValue(Op0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3074 | unsigned i = 0; |
| 3075 | // Copy the beginning value(s) from the original aggregate. |
| 3076 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3077 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3078 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 3079 | // Copy values from the inserted value(s). |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 3080 | if (NumValValues) { |
| 3081 | SDValue Val = getValue(Op1); |
| 3082 | for (; i != LinearIndex + NumValValues; ++i) |
| 3083 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
| 3084 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 3085 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3086 | // Copy remaining value(s) from the original aggregate. |
| 3087 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3088 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3089 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 3090 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3091 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3092 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 3093 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3096 | void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3097 | const Value *Op0 = I.getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3098 | Type *AggTy = Op0->getType(); |
| 3099 | Type *ValTy = I.getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3100 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 3101 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3102 | unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3103 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3104 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3105 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 3106 | |
| 3107 | unsigned NumValValues = ValValueVTs.size(); |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 3108 | |
| 3109 | // Ignore a extractvalue that produces an empty object |
| 3110 | if (!NumValValues) { |
| 3111 | setValue(&I, DAG.getUNDEF(MVT(MVT::Other))); |
| 3112 | return; |
| 3113 | } |
| 3114 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3115 | SmallVector<SDValue, 4> Values(NumValValues); |
| 3116 | |
| 3117 | SDValue Agg = getValue(Op0); |
| 3118 | // Copy out the selected value(s). |
| 3119 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 3120 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 3121 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3122 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 3123 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3124 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3125 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3126 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 3127 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3130 | void SelectionDAGBuilder::visitGetElementPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3131 | SDValue N = getValue(I.getOperand(0)); |
Nadav Rotem | 1c23920 | 2012-02-28 14:13:19 +0000 | [diff] [blame] | 3132 | // Note that the pointer operand may be a vector of pointers. Take the scalar |
| 3133 | // element which holds a pointer. |
| 3134 | Type *Ty = I.getOperand(0)->getType()->getScalarType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3135 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3136 | 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] | 3137 | OI != E; ++OI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3138 | const Value *Idx = *OI; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3139 | if (StructType *StTy = dyn_cast<StructType>(Ty)) { |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3140 | unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3141 | if (Field) { |
| 3142 | // N = N + Offset |
| 3143 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3144 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3145 | DAG.getConstant(Offset, N.getValueType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3146 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3147 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3148 | Ty = StTy->getElementType(Field); |
| 3149 | } else { |
| 3150 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 3151 | |
| 3152 | // If this is a constant subscript, handle it quickly. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3153 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 3154 | if (CI->isZero()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3155 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 3156 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 3157 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3158 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 3159 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3160 | if (PtrBits < 64) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3161 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 3162 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3163 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3164 | else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 3165 | OffsVal = DAG.getIntPtrConstant(Offs); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3166 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3167 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 3168 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3169 | continue; |
| 3170 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3171 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3172 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 3173 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 3174 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3175 | SDValue IdxN = getValue(Idx); |
| 3176 | |
| 3177 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 3178 | // it. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3179 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3180 | |
| 3181 | // If this is a multiply by a power of two, turn it into a shl |
| 3182 | // immediately. This is a very common case. |
| 3183 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 3184 | if (ElementSize.isPowerOf2()) { |
| 3185 | unsigned Amt = ElementSize.logBase2(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3186 | IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3187 | N.getValueType(), IdxN, |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3188 | DAG.getConstant(Amt, IdxN.getValueType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3189 | } else { |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3190 | SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3191 | IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3192 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3193 | } |
| 3194 | } |
| 3195 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3196 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3197 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3198 | } |
| 3199 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3200 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3201 | setValue(&I, N); |
| 3202 | } |
| 3203 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3204 | void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3205 | // If this is a fixed sized alloca in the entry block of the function, |
| 3206 | // allocate it statically on the stack. |
| 3207 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 3208 | return; // getValue will auto-populate this. |
| 3209 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3210 | Type *Ty = I.getAllocatedType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3211 | uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3212 | unsigned Align = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3213 | std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3214 | I.getAlignment()); |
| 3215 | |
| 3216 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3217 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3218 | EVT IntPtr = TLI.getPointerTy(); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3219 | if (AllocSize.getValueType() != IntPtr) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3220 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3221 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3222 | AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr, |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3223 | AllocSize, |
| 3224 | DAG.getConstant(TySize, IntPtr)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3225 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3226 | // Handle alignment. If the requested alignment is less than or equal to |
| 3227 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 3228 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 3229 | unsigned StackAlign = TM.getFrameLowering()->getStackAlignment(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3230 | if (Align <= StackAlign) |
| 3231 | Align = 0; |
| 3232 | |
| 3233 | // Round the size of the allocation up to the stack alignment size |
| 3234 | // by add SA-1 to the size. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3235 | AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3236 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3237 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3238 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3239 | // Mask out the low bits for alignment purposes. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3240 | AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3241 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3242 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 3243 | |
| 3244 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3245 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3246 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3247 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3248 | setValue(&I, DSA); |
| 3249 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3250 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3251 | // Inform the Frame Information that we have just allocated a variable-sized |
| 3252 | // object. |
Bob Wilson | 8f637ad | 2013-02-08 20:35:15 +0000 | [diff] [blame] | 3253 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3256 | void SelectionDAGBuilder::visitLoad(const LoadInst &I) { |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3257 | if (I.isAtomic()) |
| 3258 | return visitAtomicLoad(I); |
| 3259 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3260 | const Value *SV = I.getOperand(0); |
| 3261 | SDValue Ptr = getValue(SV); |
| 3262 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3263 | Type *Ty = I.getType(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3264 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3265 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3266 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 3267 | bool isInvariant = I.getMetadata("invariant.load") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3268 | unsigned Alignment = I.getAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3269 | const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa); |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 3270 | const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3271 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3272 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3273 | SmallVector<uint64_t, 4> Offsets; |
| 3274 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 3275 | unsigned NumValues = ValueVTs.size(); |
| 3276 | if (NumValues == 0) |
| 3277 | return; |
| 3278 | |
| 3279 | SDValue Root; |
| 3280 | bool ConstantMemory = false; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3281 | if (I.isVolatile() || NumValues > MaxParallelChains) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3282 | // Serialize volatile loads with other side effects. |
| 3283 | Root = getRoot(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3284 | else if (AA->pointsToConstantMemory( |
| 3285 | AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3286 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 3287 | Root = DAG.getEntryNode(); |
| 3288 | ConstantMemory = true; |
| 3289 | } else { |
| 3290 | // Do not serialize non-volatile loads against each other. |
| 3291 | Root = DAG.getRoot(); |
| 3292 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3293 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3294 | SmallVector<SDValue, 4> Values(NumValues); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3295 | SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains), |
| 3296 | NumValues)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3297 | EVT PtrVT = Ptr.getValueType(); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3298 | unsigned ChainI = 0; |
| 3299 | for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { |
| 3300 | // Serializing loads here may result in excessive register pressure, and |
| 3301 | // TokenFactor places arbitrary choke points on the scheduler. SD scheduling |
| 3302 | // could recover a bit by hoisting nodes upward in the chain by recognizing |
| 3303 | // they are side-effect free or do not alias. The optimizer should really |
| 3304 | // avoid this case by converting large object/array copies to llvm.memcpy |
| 3305 | // (MaxParallelChains should always remain as failsafe). |
| 3306 | if (ChainI == MaxParallelChains) { |
| 3307 | assert(PendingLoads.empty() && "PendingLoads must be serialized first"); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3308 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3309 | MVT::Other, &Chains[0], ChainI); |
| 3310 | Root = Chain; |
| 3311 | ChainI = 0; |
| 3312 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3313 | SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3314 | PtrVT, Ptr, |
| 3315 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3316 | SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root, |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 3317 | A, MachinePointerInfo(SV, Offsets[i]), isVolatile, |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 3318 | isNonTemporal, isInvariant, Alignment, TBAAInfo, |
| 3319 | Ranges); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3320 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3321 | Values[i] = L; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3322 | Chains[ChainI] = L.getValue(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3323 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3324 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3325 | if (!ConstantMemory) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3326 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3327 | MVT::Other, &Chains[0], ChainI); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3328 | if (isVolatile) |
| 3329 | DAG.setRoot(Chain); |
| 3330 | else |
| 3331 | PendingLoads.push_back(Chain); |
| 3332 | } |
| 3333 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3334 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3335 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 3336 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3337 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3338 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3339 | void SelectionDAGBuilder::visitStore(const StoreInst &I) { |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3340 | if (I.isAtomic()) |
| 3341 | return visitAtomicStore(I); |
| 3342 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3343 | const Value *SrcV = I.getOperand(0); |
| 3344 | const Value *PtrV = I.getOperand(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3345 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3346 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3347 | SmallVector<uint64_t, 4> Offsets; |
| 3348 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 3349 | unsigned NumValues = ValueVTs.size(); |
| 3350 | if (NumValues == 0) |
| 3351 | return; |
| 3352 | |
| 3353 | // Get the lowered operands. Note that we do this after |
| 3354 | // checking if NumResults is zero, because with zero results |
| 3355 | // the operands won't have values in the map. |
| 3356 | SDValue Src = getValue(SrcV); |
| 3357 | SDValue Ptr = getValue(PtrV); |
| 3358 | |
| 3359 | SDValue Root = getRoot(); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3360 | SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains), |
| 3361 | NumValues)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3362 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3363 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3364 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3365 | unsigned Alignment = I.getAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3366 | const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3367 | |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3368 | unsigned ChainI = 0; |
| 3369 | for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { |
| 3370 | // See visitLoad comments. |
| 3371 | if (ChainI == MaxParallelChains) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3372 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3373 | MVT::Other, &Chains[0], ChainI); |
| 3374 | Root = Chain; |
| 3375 | ChainI = 0; |
| 3376 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3377 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr, |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3378 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3379 | SDValue St = DAG.getStore(Root, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3380 | SDValue(Src.getNode(), Src.getResNo() + i), |
| 3381 | Add, MachinePointerInfo(PtrV, Offsets[i]), |
| 3382 | isVolatile, isNonTemporal, Alignment, TBAAInfo); |
| 3383 | Chains[ChainI] = St; |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3386 | SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3387 | MVT::Other, &Chains[0], ChainI); |
Devang Patel | 7e13efa | 2010-10-26 22:14:52 +0000 | [diff] [blame] | 3388 | DAG.setRoot(StoreNode); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3391 | static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3392 | SynchronizationScope Scope, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3393 | bool Before, SDLoc dl, |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3394 | SelectionDAG &DAG, |
| 3395 | const TargetLowering &TLI) { |
| 3396 | // Fence, if necessary |
| 3397 | if (Before) { |
Eli Friedman | 069e2ed | 2011-08-26 02:59:24 +0000 | [diff] [blame] | 3398 | if (Order == AcquireRelease || Order == SequentiallyConsistent) |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3399 | Order = Release; |
| 3400 | else if (Order == Acquire || Order == Monotonic) |
| 3401 | return Chain; |
| 3402 | } else { |
| 3403 | if (Order == AcquireRelease) |
| 3404 | Order = Acquire; |
| 3405 | else if (Order == Release || Order == Monotonic) |
| 3406 | return Chain; |
| 3407 | } |
| 3408 | SDValue Ops[3]; |
| 3409 | Ops[0] = Chain; |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3410 | Ops[1] = DAG.getConstant(Order, TLI.getPointerTy()); |
| 3411 | Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy()); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3412 | return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3); |
| 3413 | } |
| 3414 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3415 | void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3416 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3417 | AtomicOrdering Order = I.getOrdering(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3418 | SynchronizationScope Scope = I.getSynchScope(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3419 | |
| 3420 | SDValue InChain = getRoot(); |
| 3421 | |
| 3422 | if (TLI.getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3423 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
| 3424 | DAG, TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3425 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3426 | SDValue L = |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3427 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3428 | getValue(I.getCompareOperand()).getValueType().getSimpleVT(), |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3429 | InChain, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3430 | getValue(I.getPointerOperand()), |
| 3431 | getValue(I.getCompareOperand()), |
| 3432 | getValue(I.getNewValOperand()), |
| 3433 | MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3434 | TLI.getInsertFencesForAtomic() ? Monotonic : Order, |
| 3435 | Scope); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3436 | |
| 3437 | SDValue OutChain = L.getValue(1); |
| 3438 | |
| 3439 | if (TLI.getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3440 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
| 3441 | DAG, TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3442 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3443 | setValue(&I, L); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3444 | DAG.setRoot(OutChain); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3448 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3449 | ISD::NodeType NT; |
| 3450 | switch (I.getOperation()) { |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 3451 | default: llvm_unreachable("Unknown atomicrmw operation"); |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3452 | case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break; |
| 3453 | case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break; |
| 3454 | case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break; |
| 3455 | case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break; |
| 3456 | case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break; |
| 3457 | case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break; |
| 3458 | case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break; |
| 3459 | case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break; |
| 3460 | case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break; |
| 3461 | case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break; |
| 3462 | case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break; |
| 3463 | } |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3464 | AtomicOrdering Order = I.getOrdering(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3465 | SynchronizationScope Scope = I.getSynchScope(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3466 | |
| 3467 | SDValue InChain = getRoot(); |
| 3468 | |
| 3469 | if (TLI.getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3470 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
| 3471 | DAG, TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3472 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3473 | SDValue L = |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3474 | DAG.getAtomic(NT, dl, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3475 | getValue(I.getValOperand()).getValueType().getSimpleVT(), |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3476 | InChain, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3477 | getValue(I.getPointerOperand()), |
| 3478 | getValue(I.getValOperand()), |
| 3479 | I.getPointerOperand(), 0 /* Alignment */, |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3480 | TLI.getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3481 | Scope); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3482 | |
| 3483 | SDValue OutChain = L.getValue(1); |
| 3484 | |
| 3485 | if (TLI.getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3486 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
| 3487 | DAG, TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3488 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3489 | setValue(&I, L); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3490 | DAG.setRoot(OutChain); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3493 | void SelectionDAGBuilder::visitFence(const FenceInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3494 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 3495 | SDValue Ops[3]; |
| 3496 | Ops[0] = getRoot(); |
| 3497 | Ops[1] = DAG.getConstant(I.getOrdering(), TLI.getPointerTy()); |
| 3498 | Ops[2] = DAG.getConstant(I.getSynchScope(), TLI.getPointerTy()); |
| 3499 | DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3)); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3502 | void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3503 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3504 | AtomicOrdering Order = I.getOrdering(); |
| 3505 | SynchronizationScope Scope = I.getSynchScope(); |
| 3506 | |
| 3507 | SDValue InChain = getRoot(); |
| 3508 | |
Eli Friedman | fd45fa1 | 2012-08-17 23:24:29 +0000 | [diff] [blame] | 3509 | EVT VT = TLI.getValueType(I.getType()); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3510 | |
Evan Cheng | 607acd6 | 2013-02-06 02:06:33 +0000 | [diff] [blame] | 3511 | if (I.getAlignment() < VT.getSizeInBits() / 8) |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3512 | report_fatal_error("Cannot generate unaligned atomic load"); |
| 3513 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3514 | SDValue L = |
| 3515 | DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain, |
| 3516 | getValue(I.getPointerOperand()), |
| 3517 | I.getPointerOperand(), I.getAlignment(), |
| 3518 | TLI.getInsertFencesForAtomic() ? Monotonic : Order, |
| 3519 | Scope); |
| 3520 | |
| 3521 | SDValue OutChain = L.getValue(1); |
| 3522 | |
| 3523 | if (TLI.getInsertFencesForAtomic()) |
| 3524 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
| 3525 | DAG, TLI); |
| 3526 | |
| 3527 | setValue(&I, L); |
| 3528 | DAG.setRoot(OutChain); |
| 3529 | } |
| 3530 | |
| 3531 | void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3532 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3533 | |
| 3534 | AtomicOrdering Order = I.getOrdering(); |
| 3535 | SynchronizationScope Scope = I.getSynchScope(); |
| 3536 | |
| 3537 | SDValue InChain = getRoot(); |
| 3538 | |
Eli Friedman | fd45fa1 | 2012-08-17 23:24:29 +0000 | [diff] [blame] | 3539 | EVT VT = TLI.getValueType(I.getValueOperand()->getType()); |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3540 | |
Evan Cheng | 607acd6 | 2013-02-06 02:06:33 +0000 | [diff] [blame] | 3541 | if (I.getAlignment() < VT.getSizeInBits() / 8) |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3542 | report_fatal_error("Cannot generate unaligned atomic store"); |
| 3543 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3544 | if (TLI.getInsertFencesForAtomic()) |
| 3545 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
| 3546 | DAG, TLI); |
| 3547 | |
| 3548 | SDValue OutChain = |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3549 | DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3550 | InChain, |
| 3551 | getValue(I.getPointerOperand()), |
| 3552 | getValue(I.getValueOperand()), |
| 3553 | I.getPointerOperand(), I.getAlignment(), |
| 3554 | TLI.getInsertFencesForAtomic() ? Monotonic : Order, |
| 3555 | Scope); |
| 3556 | |
| 3557 | if (TLI.getInsertFencesForAtomic()) |
| 3558 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
| 3559 | DAG, TLI); |
| 3560 | |
| 3561 | DAG.setRoot(OutChain); |
| 3562 | } |
| 3563 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3564 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 3565 | /// node. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3566 | void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3567 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3568 | bool HasChain = !I.doesNotAccessMemory(); |
| 3569 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 3570 | |
| 3571 | // Build the operand list. |
| 3572 | SmallVector<SDValue, 8> Ops; |
| 3573 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 3574 | if (OnlyLoad) { |
| 3575 | // We don't need to serialize loads against other loads. |
| 3576 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3577 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3578 | Ops.push_back(getRoot()); |
| 3579 | } |
| 3580 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3581 | |
| 3582 | // Info is set by getTgtMemInstrinsic |
| 3583 | TargetLowering::IntrinsicInfo Info; |
| 3584 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 3585 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3586 | // Add the intrinsic ID as an integer operand if it's not a target intrinsic. |
Bob Wilson | 65ffec4 | 2010-09-21 17:56:22 +0000 | [diff] [blame] | 3587 | if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID || |
| 3588 | Info.opc == ISD::INTRINSIC_W_CHAIN) |
Pete Cooper | bf42139 | 2012-01-16 04:08:12 +0000 | [diff] [blame] | 3589 | Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3590 | |
| 3591 | // Add all operands of the call to the operand list. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3592 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 3593 | SDValue Op = getValue(I.getArgOperand(i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3594 | Ops.push_back(Op); |
| 3595 | } |
| 3596 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3597 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 3598 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3599 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3600 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3601 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3602 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 3603 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3604 | |
| 3605 | // Create the node. |
| 3606 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3607 | if (IsTgtIntrinsic) { |
| 3608 | // This is target intrinsic that touches memory |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3609 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3610 | VTs, &Ops[0], Ops.size(), |
Chris Lattner | e9ba5dd | 2010-09-21 04:57:15 +0000 | [diff] [blame] | 3611 | Info.memVT, |
| 3612 | MachinePointerInfo(Info.ptrVal, Info.offset), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3613 | Info.align, Info.vol, |
| 3614 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3615 | } else if (!HasChain) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3616 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3617 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3618 | } else if (!I.getType()->isVoidTy()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3619 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3620 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3621 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3622 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3623 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3626 | if (HasChain) { |
| 3627 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 3628 | if (OnlyLoad) |
| 3629 | PendingLoads.push_back(Chain); |
| 3630 | else |
| 3631 | DAG.setRoot(Chain); |
| 3632 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3633 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3634 | if (!I.getType()->isVoidTy()) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3635 | if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3636 | EVT VT = TLI.getValueType(PTy); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3637 | Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3638 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3639 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3640 | setValue(&I, Result); |
| 3641 | } |
| 3642 | } |
| 3643 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3644 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3645 | /// number with exponent of 1: |
| 3646 | /// |
| 3647 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3648 | /// |
Matt Beaumont-Gay | 50e75bf | 2013-02-25 18:11:18 +0000 | [diff] [blame] | 3649 | /// where Op is the hexadecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3650 | static SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3651 | GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3652 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3653 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 3654 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 3655 | DAG.getConstant(0x3f800000, MVT::i32)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3656 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3659 | /// GetExponent - Get the exponent: |
| 3660 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3661 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3662 | /// |
Matt Beaumont-Gay | 50e75bf | 2013-02-25 18:11:18 +0000 | [diff] [blame] | 3663 | /// where Op is the hexadecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3664 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3665 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3666 | SDLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3667 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3668 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 3669 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3670 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3671 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 3672 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3673 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3676 | /// getF32Constant - Get 32-bit floating point constant. |
| 3677 | static SDValue |
| 3678 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3679 | return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)), |
| 3680 | MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3683 | /// expandExp - Lower an exp intrinsic. Handles the special sequences for |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3684 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3685 | static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3686 | const TargetLowering &TLI) { |
| 3687 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3688 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3689 | |
| 3690 | // Put the exponent in the right bit position for later addition to the |
| 3691 | // final result: |
| 3692 | // |
| 3693 | // #define LOG2OFe 1.4426950f |
| 3694 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3695 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3696 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3697 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3698 | |
| 3699 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3700 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3701 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3702 | |
| 3703 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3704 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3705 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3706 | |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3707 | SDValue TwoToFracPartOfX; |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3708 | if (LimitFloatPrecision <= 6) { |
| 3709 | // For floating-point precision of 6: |
| 3710 | // |
| 3711 | // TwoToFractionalPartOfX = |
| 3712 | // 0.997535578f + |
| 3713 | // (0.735607626f + 0.252464424f * x) * x; |
| 3714 | // |
| 3715 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3716 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3717 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3718 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3719 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3720 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3721 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3722 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3723 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3724 | // For floating-point precision of 12: |
| 3725 | // |
| 3726 | // TwoToFractionalPartOfX = |
| 3727 | // 0.999892986f + |
| 3728 | // (0.696457318f + |
| 3729 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3730 | // |
| 3731 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3732 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3733 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3734 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3735 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3736 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3737 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3738 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3739 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3740 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3741 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3742 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3743 | // For floating-point precision of 18: |
| 3744 | // |
| 3745 | // TwoToFractionalPartOfX = |
| 3746 | // 0.999999982f + |
| 3747 | // (0.693148872f + |
| 3748 | // (0.240227044f + |
| 3749 | // (0.554906021e-1f + |
| 3750 | // (0.961591928e-2f + |
| 3751 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3752 | // |
| 3753 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3754 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3755 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3756 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3757 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3758 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3759 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3760 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3761 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3762 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3763 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3764 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3765 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3766 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3767 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3768 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3769 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3770 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3771 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 3772 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3773 | } |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3774 | |
| 3775 | // Add the exponent into the result in integer domain. |
| 3776 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX); |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3777 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 3778 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 3779 | t13, IntegerPartOfX)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3782 | // No special expansion. |
| 3783 | return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3786 | /// expandLog - Lower a log intrinsic. Handles the special sequences for |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3787 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3788 | static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3789 | const TargetLowering &TLI) { |
| 3790 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3791 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3792 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3793 | |
| 3794 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3795 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3796 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3797 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3798 | |
| 3799 | // Get the significand and build it into a floating-point number with |
| 3800 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3801 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3802 | |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3803 | SDValue LogOfMantissa; |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3804 | if (LimitFloatPrecision <= 6) { |
| 3805 | // For floating-point precision of 6: |
| 3806 | // |
| 3807 | // LogofMantissa = |
| 3808 | // -1.1609546f + |
| 3809 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3810 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3811 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3812 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3813 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3814 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3815 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3816 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3817 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3818 | getF32Constant(DAG, 0x3f949a29)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3819 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3820 | // For floating-point precision of 12: |
| 3821 | // |
| 3822 | // LogOfMantissa = |
| 3823 | // -1.7417939f + |
| 3824 | // (2.8212026f + |
| 3825 | // (-1.4699568f + |
| 3826 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3827 | // |
| 3828 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3829 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3830 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3831 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3832 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3833 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3834 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3835 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3836 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3837 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3838 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3839 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3840 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3841 | getF32Constant(DAG, 0x3fdef31a)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3842 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3843 | // For floating-point precision of 18: |
| 3844 | // |
| 3845 | // LogOfMantissa = |
| 3846 | // -2.1072184f + |
| 3847 | // (4.2372794f + |
| 3848 | // (-3.7029485f + |
| 3849 | // (2.2781945f + |
| 3850 | // (-0.87823314f + |
| 3851 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3852 | // |
| 3853 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3854 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3855 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3856 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3857 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3858 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3859 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3860 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3861 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3862 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3863 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3864 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3865 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3866 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3867 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3868 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3869 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3870 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3871 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3872 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3873 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3874 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3875 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3878 | // No special expansion. |
| 3879 | return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3882 | /// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3883 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3884 | static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3885 | const TargetLowering &TLI) { |
| 3886 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3887 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3888 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3889 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3890 | // Get the exponent. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3891 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3892 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3893 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3894 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3895 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3896 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3897 | // Different possible minimax approximations of significand in |
| 3898 | // floating-point for various degrees of accuracy over [1,2]. |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3899 | SDValue Log2ofMantissa; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3900 | if (LimitFloatPrecision <= 6) { |
| 3901 | // For floating-point precision of 6: |
| 3902 | // |
| 3903 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3904 | // |
| 3905 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3906 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3907 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3908 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3909 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3910 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3911 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3912 | getF32Constant(DAG, 0x3fd6633d)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3913 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3914 | // For floating-point precision of 12: |
| 3915 | // |
| 3916 | // Log2ofMantissa = |
| 3917 | // -2.51285454f + |
| 3918 | // (4.07009056f + |
| 3919 | // (-2.12067489f + |
| 3920 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3921 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3922 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3923 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3924 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3925 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3926 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3927 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3928 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3929 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3930 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3931 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3932 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3933 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3934 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3935 | getF32Constant(DAG, 0x4020d29c)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3936 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3937 | // For floating-point precision of 18: |
| 3938 | // |
| 3939 | // Log2ofMantissa = |
| 3940 | // -3.0400495f + |
| 3941 | // (6.1129976f + |
| 3942 | // (-5.3420409f + |
| 3943 | // (3.2865683f + |
| 3944 | // (-1.2669343f + |
| 3945 | // (0.27515199f - |
| 3946 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3947 | // |
| 3948 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3949 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3950 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3951 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3952 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3953 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3954 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3955 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3956 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3957 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3958 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3959 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3960 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3961 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3962 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3963 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3964 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3965 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3966 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3967 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3968 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3969 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3970 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa); |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3971 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3972 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3973 | // No special expansion. |
| 3974 | return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3977 | /// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3978 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3979 | static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3980 | const TargetLowering &TLI) { |
| 3981 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3982 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3983 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3984 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3985 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3986 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3987 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3988 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3989 | |
| 3990 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3991 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3992 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3993 | |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3994 | SDValue Log10ofMantissa; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3995 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3996 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3997 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3998 | // Log10ofMantissa = |
| 3999 | // -0.50419619f + |
| 4000 | // (0.60948995f - 0.10380950f * x) * x; |
| 4001 | // |
| 4002 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4003 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4004 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4005 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4006 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4007 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4008 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 4009 | getF32Constant(DAG, 0x3f011300)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4010 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4011 | // For floating-point precision of 12: |
| 4012 | // |
| 4013 | // Log10ofMantissa = |
| 4014 | // -0.64831180f + |
| 4015 | // (0.91751397f + |
| 4016 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 4017 | // |
| 4018 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4019 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4020 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4021 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4022 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4023 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4024 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4025 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4026 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4027 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
| 4028 | getF32Constant(DAG, 0x3f25f7c3)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4029 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4030 | // For floating-point precision of 18: |
| 4031 | // |
| 4032 | // Log10ofMantissa = |
| 4033 | // -0.84299375f + |
| 4034 | // (1.5327582f + |
| 4035 | // (-1.0688956f + |
| 4036 | // (0.49102474f + |
| 4037 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 4038 | // |
| 4039 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4040 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4041 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4042 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4043 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4044 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4045 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4046 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4047 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4048 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4049 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4050 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4051 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4052 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4053 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4054 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
| 4055 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4056 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4057 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4058 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa); |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 4059 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4060 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4061 | // No special expansion. |
| 4062 | return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4065 | /// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4066 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4067 | static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4068 | const TargetLowering &TLI) { |
| 4069 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4070 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4071 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4072 | |
| 4073 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4074 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 4075 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4076 | |
| 4077 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4078 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 4079 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4080 | |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4081 | SDValue TwoToFractionalPartOfX; |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4082 | if (LimitFloatPrecision <= 6) { |
| 4083 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4084 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4085 | // TwoToFractionalPartOfX = |
| 4086 | // 0.997535578f + |
| 4087 | // (0.735607626f + 0.252464424f * x) * x; |
| 4088 | // |
| 4089 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4090 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4091 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4092 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4093 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4094 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4095 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 4096 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4097 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4098 | // For floating-point precision of 12: |
| 4099 | // |
| 4100 | // TwoToFractionalPartOfX = |
| 4101 | // 0.999892986f + |
| 4102 | // (0.696457318f + |
| 4103 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 4104 | // |
| 4105 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4106 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4107 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4108 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4109 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4110 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4111 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4112 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4113 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4114 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 4115 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4116 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4117 | // For floating-point precision of 18: |
| 4118 | // |
| 4119 | // TwoToFractionalPartOfX = |
| 4120 | // 0.999999982f + |
| 4121 | // (0.693148872f + |
| 4122 | // (0.240227044f + |
| 4123 | // (0.554906021e-1f + |
| 4124 | // (0.961591928e-2f + |
| 4125 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 4126 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4127 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4128 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4129 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4130 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4131 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4132 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4133 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4134 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4135 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4136 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4137 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4138 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4139 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4140 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 4141 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4142 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4143 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4144 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 4145 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4146 | } |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4147 | |
| 4148 | // Add the exponent into the result in integer domain. |
| 4149 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, |
| 4150 | TwoToFractionalPartOfX); |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4151 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 4152 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 4153 | t13, IntegerPartOfX)); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4154 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4155 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4156 | // No special expansion. |
| 4157 | return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4160 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 4161 | /// limited-precision mode with x == 10.0f. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4162 | static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS, |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4163 | SelectionDAG &DAG, const TargetLowering &TLI) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4164 | bool IsExp10 = false; |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4165 | if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4166 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4167 | if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) { |
| 4168 | APFloat Ten(10.0f); |
| 4169 | IsExp10 = LHSC->isExactlyValue(Ten); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4170 | } |
| 4171 | } |
| 4172 | |
Craig Topper | c1aa638 | 2012-11-25 00:48:58 +0000 | [diff] [blame] | 4173 | if (IsExp10) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4174 | // Put the exponent in the right bit position for later addition to the |
| 4175 | // final result: |
| 4176 | // |
| 4177 | // #define LOG2OF10 3.3219281f |
| 4178 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4179 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4180 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4181 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4182 | |
| 4183 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4184 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 4185 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4186 | |
| 4187 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4188 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 4189 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4190 | |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4191 | SDValue TwoToFractionalPartOfX; |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4192 | if (LimitFloatPrecision <= 6) { |
| 4193 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4194 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4195 | // twoToFractionalPartOfX = |
| 4196 | // 0.997535578f + |
| 4197 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4198 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4199 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4200 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4201 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4202 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4203 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4204 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4205 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 4206 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4207 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4208 | // For floating-point precision of 12: |
| 4209 | // |
| 4210 | // TwoToFractionalPartOfX = |
| 4211 | // 0.999892986f + |
| 4212 | // (0.696457318f + |
| 4213 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 4214 | // |
| 4215 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4216 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4217 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4218 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4219 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4220 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4221 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4222 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4223 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4224 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 4225 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4226 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4227 | // For floating-point precision of 18: |
| 4228 | // |
| 4229 | // TwoToFractionalPartOfX = |
| 4230 | // 0.999999982f + |
| 4231 | // (0.693148872f + |
| 4232 | // (0.240227044f + |
| 4233 | // (0.554906021e-1f + |
| 4234 | // (0.961591928e-2f + |
| 4235 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 4236 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4237 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4238 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4239 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4240 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4241 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4242 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4243 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4244 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4245 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4246 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4247 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4248 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4249 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4250 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 4251 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4252 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4253 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4254 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 4255 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4256 | } |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4257 | |
| 4258 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX); |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4259 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 4260 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 4261 | t13, IntegerPartOfX)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4264 | // No special expansion. |
| 4265 | return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4268 | |
| 4269 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4270 | static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4271 | SelectionDAG &DAG) { |
| 4272 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 4273 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 4274 | // optimizing for size, we only want to do this if the expansion would produce |
| 4275 | // a small number of multiplies, otherwise we do the full expansion. |
| 4276 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 4277 | // Get the exponent as a positive value. |
| 4278 | unsigned Val = RHSC->getSExtValue(); |
| 4279 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4280 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4281 | // powi(x, 0) -> 1.0 |
| 4282 | if (Val == 0) |
| 4283 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 4284 | |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 4285 | const Function *F = DAG.getMachineFunction().getFunction(); |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 4286 | if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 4287 | Attribute::OptimizeForSize) || |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4288 | // If optimizing for size, don't insert too many multiplies. This |
| 4289 | // inserts up to 5 multiplies. |
| 4290 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 4291 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4292 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4293 | // powi(x,15) generates one more multiply than it should), but this has |
| 4294 | // the benefit of being both really simple and much better than a libcall. |
| 4295 | SDValue Res; // Logically starts equal to 1.0 |
| 4296 | SDValue CurSquare = LHS; |
| 4297 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 4298 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4299 | if (Res.getNode()) |
| 4300 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 4301 | else |
| 4302 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 4303 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4304 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4305 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 4306 | CurSquare, CurSquare); |
| 4307 | Val >>= 1; |
| 4308 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4309 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4310 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 4311 | if (RHSC->getSExtValue() < 0) |
| 4312 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 4313 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 4314 | return Res; |
| 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | // Otherwise, expand to a libcall. |
| 4319 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 4320 | } |
| 4321 | |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4322 | // getTruncatedArgReg - Find underlying register used for an truncated |
| 4323 | // argument. |
| 4324 | static unsigned getTruncatedArgReg(const SDValue &N) { |
| 4325 | if (N.getOpcode() != ISD::TRUNCATE) |
| 4326 | return 0; |
| 4327 | |
| 4328 | const SDValue &Ext = N.getOperand(0); |
| 4329 | if (Ext.getOpcode() == ISD::AssertZext || Ext.getOpcode() == ISD::AssertSext){ |
| 4330 | const SDValue &CFR = Ext.getOperand(0); |
| 4331 | if (CFR.getOpcode() == ISD::CopyFromReg) |
| 4332 | return cast<RegisterSDNode>(CFR.getOperand(1))->getReg(); |
Craig Topper | 7eb46d8 | 2012-04-11 04:55:51 +0000 | [diff] [blame] | 4333 | if (CFR.getOpcode() == ISD::TRUNCATE) |
| 4334 | return getTruncatedArgReg(CFR); |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4335 | } |
| 4336 | return 0; |
| 4337 | } |
| 4338 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4339 | /// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function |
| 4340 | /// argument, create the corresponding DBG_VALUE machine instruction for it now. |
| 4341 | /// 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] | 4342 | bool |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 4343 | SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable, |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4344 | int64_t Offset, |
Dan Gohman | 5d11ea3 | 2010-05-01 00:33:16 +0000 | [diff] [blame] | 4345 | const SDValue &N) { |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4346 | const Argument *Arg = dyn_cast<Argument>(V); |
| 4347 | if (!Arg) |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4348 | return false; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4349 | |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 4350 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4351 | const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo(); |
| 4352 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 4353 | |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 4354 | // Ignore inlined function arguments here. |
| 4355 | DIVariable DV(Variable); |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 4356 | if (DV.isInlinedFnArgument(MF.getFunction())) |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 4357 | return false; |
| 4358 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4359 | unsigned Reg = 0; |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 4360 | // Some arguments' frame index is recorded during argument lowering. |
| 4361 | Offset = FuncInfo.getArgumentFrameIndex(Arg); |
| 4362 | if (Offset) |
Craig Topper | 7eb46d8 | 2012-04-11 04:55:51 +0000 | [diff] [blame] | 4363 | Reg = TRI->getFrameRegister(MF); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4364 | |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 4365 | if (!Reg && N.getNode()) { |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4366 | if (N.getOpcode() == ISD::CopyFromReg) |
| 4367 | Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
| 4368 | else |
| 4369 | Reg = getTruncatedArgReg(N); |
| 4370 | if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4371 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 4372 | unsigned PR = RegInfo.getLiveInPhysReg(Reg); |
| 4373 | if (PR) |
| 4374 | Reg = PR; |
| 4375 | } |
| 4376 | } |
| 4377 | |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4378 | if (!Reg) { |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4379 | // Check if ValueMap has reg number. |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4380 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4381 | if (VMI != FuncInfo.ValueMap.end()) |
| 4382 | Reg = VMI->second; |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4383 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4384 | |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4385 | if (!Reg && N.getNode()) { |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4386 | // Check if frame index is available. |
| 4387 | if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode())) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4388 | if (FrameIndexSDNode *FINode = |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4389 | dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) { |
| 4390 | Reg = TRI->getFrameRegister(MF); |
| 4391 | Offset = FINode->getIndex(); |
| 4392 | } |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | if (!Reg) |
| 4396 | return false; |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4397 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4398 | MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(), |
Adrian Prantl | 86a87d9 | 2013-04-30 22:35:14 +0000 | [diff] [blame] | 4399 | TII->get(TargetOpcode::DBG_VALUE)) |
| 4400 | .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4401 | FuncInfo.ArgDbgValues.push_back(&*MIB); |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4402 | return true; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4403 | } |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 4405 | // VisualStudio defines setjmp as _setjmp |
Michael J. Spencer | 1f40960 | 2010-09-24 19:48:47 +0000 | [diff] [blame] | 4406 | #if defined(_MSC_VER) && defined(setjmp) && \ |
| 4407 | !defined(setjmp_undefined_for_msvc) |
| 4408 | # pragma push_macro("setjmp") |
| 4409 | # undef setjmp |
| 4410 | # define setjmp_undefined_for_msvc |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 4411 | #endif |
| 4412 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4413 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 4414 | /// we want to emit this as a call to a named external function, return the name |
| 4415 | /// otherwise lower it and return null. |
| 4416 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4417 | SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4418 | SDLoc sdl = getCurSDLoc(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4419 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4420 | SDValue Res; |
| 4421 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4422 | switch (Intrinsic) { |
| 4423 | default: |
| 4424 | // By default, turn this into a target intrinsic node. |
| 4425 | visitTargetIntrinsic(I, Intrinsic); |
| 4426 | return 0; |
| 4427 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 4428 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 4429 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 4430 | case Intrinsic::returnaddress: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4431 | setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI.getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4432 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4433 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 4434 | case Intrinsic::frameaddress: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4435 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI.getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4436 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4437 | return 0; |
| 4438 | case Intrinsic::setjmp: |
Bill Wendling | c27facc | 2012-03-05 19:29:36 +0000 | [diff] [blame] | 4439 | return &"_setjmp"[!TLI.usesUnderscoreSetJmp()]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4440 | case Intrinsic::longjmp: |
Bill Wendling | c27facc | 2012-03-05 19:29:36 +0000 | [diff] [blame] | 4441 | return &"_longjmp"[!TLI.usesUnderscoreLongJmp()]; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4442 | case Intrinsic::memcpy: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4443 | // Assert for address < 256 since we support only user defined address |
| 4444 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4445 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4446 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4447 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4448 | < 256 && |
| 4449 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4450 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4451 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4452 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4453 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4454 | if (!Align) |
| 4455 | Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4456 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4457 | DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4458 | MachinePointerInfo(I.getArgOperand(0)), |
| 4459 | MachinePointerInfo(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4460 | return 0; |
| 4461 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4462 | case Intrinsic::memset: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4463 | // Assert for address < 256 since we support only user defined address |
| 4464 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4465 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4466 | < 256 && |
| 4467 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4468 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4469 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4470 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4471 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4472 | if (!Align) |
| 4473 | Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4474 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4475 | DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4476 | MachinePointerInfo(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4477 | return 0; |
| 4478 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4479 | case Intrinsic::memmove: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4480 | // Assert for address < 256 since we support only user defined address |
| 4481 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4482 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4483 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4484 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4485 | < 256 && |
| 4486 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4487 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4488 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4489 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4490 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4491 | if (!Align) |
| 4492 | Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4493 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4494 | DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4495 | MachinePointerInfo(I.getArgOperand(0)), |
| 4496 | MachinePointerInfo(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4497 | return 0; |
| 4498 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4499 | case Intrinsic::dbg_declare: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4500 | const DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 4501 | MDNode *Variable = DI.getVariable(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4502 | const Value *Address = DI.getAddress(); |
Eric Christopher | 8f2a88d | 2012-03-15 21:33:41 +0000 | [diff] [blame] | 4503 | if (!Address || !DIVariable(Variable).Verify()) { |
| 4504 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Dale Johannesen | 8ac38f2 | 2010-02-08 21:53:27 +0000 | [diff] [blame] | 4505 | return 0; |
Eric Christopher | 8f2a88d | 2012-03-15 21:33:41 +0000 | [diff] [blame] | 4506 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4507 | |
Devang Patel | 3f74a11 | 2010-09-02 21:29:42 +0000 | [diff] [blame] | 4508 | // Check if address has undef value. |
| 4509 | if (isa<UndefValue>(Address) || |
| 4510 | (Address->use_empty() && !isa<Argument>(Address))) { |
Eric Christopher | 2441367 | 2012-02-23 03:39:39 +0000 | [diff] [blame] | 4511 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Devang Patel | 3f74a11 | 2010-09-02 21:29:42 +0000 | [diff] [blame] | 4512 | return 0; |
| 4513 | } |
| 4514 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4515 | SDValue &N = NodeMap[Address]; |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4516 | if (!N.getNode() && isa<Argument>(Address)) |
| 4517 | // Check unused arguments map. |
| 4518 | N = UnusedArgNodeMap[Address]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4519 | SDDbgValue *SDV; |
| 4520 | if (N.getNode()) { |
Devang Patel | 8e741ed | 2010-09-02 21:02:27 +0000 | [diff] [blame] | 4521 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 4522 | Address = BCI->getOperand(0); |
Eric Christopher | 178606d | 2012-02-24 01:59:08 +0000 | [diff] [blame] | 4523 | // Parameters are handled specially. |
| 4524 | bool isParameter = |
| 4525 | (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable || |
| 4526 | isa<Argument>(Address)); |
| 4527 | |
Devang Patel | 8e741ed | 2010-09-02 21:02:27 +0000 | [diff] [blame] | 4528 | const AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 4529 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4530 | if (isParameter && !AI) { |
| 4531 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode()); |
| 4532 | if (FINode) |
| 4533 | // Byval parameter. We have a frame index at this point. |
| 4534 | SDV = DAG.getDbgValue(Variable, FINode->getIndex(), |
| 4535 | 0, dl, SDNodeOrder); |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4536 | else { |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4537 | // Address is an argument, so try to emit its dbg value using |
| 4538 | // virtual register info from the FuncInfo.ValueMap. |
| 4539 | EmitFuncArgumentDbgValue(Address, Variable, 0, N); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4540 | return 0; |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4541 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4542 | } else if (AI) |
| 4543 | SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(), |
| 4544 | 0, dl, SDNodeOrder); |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4545 | else { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4546 | // Can't do anything with other non-AI cases yet. |
Eric Christopher | 2441367 | 2012-02-23 03:39:39 +0000 | [diff] [blame] | 4547 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Eric Christopher | 178606d | 2012-02-24 01:59:08 +0000 | [diff] [blame] | 4548 | DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t"); |
| 4549 | DEBUG(Address->dump()); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4550 | return 0; |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4551 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4552 | DAG.AddDbgValue(SDV, N.getNode(), isParameter); |
| 4553 | } else { |
Gabor Greif | fb4032f | 2010-10-01 10:32:19 +0000 | [diff] [blame] | 4554 | // If Address is an argument then try to emit its dbg value using |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4555 | // virtual register info from the FuncInfo.ValueMap. |
Devang Patel | 6cd467b | 2010-08-26 22:53:27 +0000 | [diff] [blame] | 4556 | if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) { |
Devang Patel | 1397fdc | 2010-09-15 14:48:53 +0000 | [diff] [blame] | 4557 | // If variable is pinned by a alloca in dominating bb then |
| 4558 | // use StaticAllocaMap. |
| 4559 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
Devang Patel | 27ede1b | 2010-09-15 18:13:55 +0000 | [diff] [blame] | 4560 | if (AI->getParent() != DI.getParent()) { |
| 4561 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4562 | FuncInfo.StaticAllocaMap.find(AI); |
| 4563 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 4564 | SDV = DAG.getDbgValue(Variable, SI->second, |
| 4565 | 0, dl, SDNodeOrder); |
| 4566 | DAG.AddDbgValue(SDV, 0, false); |
| 4567 | return 0; |
| 4568 | } |
Devang Patel | 1397fdc | 2010-09-15 14:48:53 +0000 | [diff] [blame] | 4569 | } |
| 4570 | } |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 4571 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Devang Patel | 6cd467b | 2010-08-26 22:53:27 +0000 | [diff] [blame] | 4572 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4573 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4574 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4575 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4576 | case Intrinsic::dbg_value: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4577 | const DbgValueInst &DI = cast<DbgValueInst>(I); |
Devang Patel | 02f0dbd | 2010-05-07 22:04:20 +0000 | [diff] [blame] | 4578 | if (!DIVariable(DI.getVariable()).Verify()) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4579 | return 0; |
| 4580 | |
| 4581 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4582 | uint64_t Offset = DI.getOffset(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4583 | const Value *V = DI.getValue(); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4584 | if (!V) |
| 4585 | return 0; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4586 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4587 | SDDbgValue *SDV; |
Devang Patel | 5787124 | 2011-08-03 23:13:55 +0000 | [diff] [blame] | 4588 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4589 | SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder); |
| 4590 | DAG.AddDbgValue(SDV, 0, false); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4591 | } else { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 4592 | // Do not use getValue() in here; we don't want to generate code at |
| 4593 | // this point if it hasn't been done yet. |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 4594 | SDValue N = NodeMap[V]; |
| 4595 | if (!N.getNode() && isa<Argument>(V)) |
| 4596 | // Check unused arguments map. |
| 4597 | N = UnusedArgNodeMap[V]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4598 | if (N.getNode()) { |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 4599 | if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) { |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4600 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 4601 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 4602 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 4603 | } |
Devang Patel | a778f5c | 2011-02-18 22:43:42 +0000 | [diff] [blame] | 4604 | } else if (!V->use_empty() ) { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 4605 | // Do not call getValue(V) yet, as we don't want to generate code. |
| 4606 | // Remember it for later. |
| 4607 | DanglingDebugInfo DDI(&DI, dl, SDNodeOrder); |
| 4608 | DanglingDebugInfoMap[V] = DDI; |
Devang Patel | 0991dfb | 2010-08-27 22:25:51 +0000 | [diff] [blame] | 4609 | } else { |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4610 | // We may expand this to cover more cases. One case where we have no |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4611 | // data available is an unreferenced parameter. |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 4612 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4613 | } |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | // Build a debug info table entry. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4617 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4618 | V = BCI->getOperand(0); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4619 | const AllocaInst *AI = dyn_cast<AllocaInst>(V); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4620 | // Don't handle byval struct arguments or VLAs, for example. |
Eric Christopher | 7e1e18f | 2012-03-26 06:10:32 +0000 | [diff] [blame] | 4621 | if (!AI) { |
Eric Christopher | 9fc5c83 | 2012-03-28 07:34:36 +0000 | [diff] [blame] | 4622 | DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n"); |
| 4623 | DEBUG(dbgs() << " Last seen at:\n " << *V << "\n"); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4624 | return 0; |
Eric Christopher | 7e1e18f | 2012-03-26 06:10:32 +0000 | [diff] [blame] | 4625 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4626 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4627 | FuncInfo.StaticAllocaMap.find(AI); |
| 4628 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 4629 | return 0; // VLAs. |
| 4630 | int FI = SI->second; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4631 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4632 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 4633 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 4634 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4635 | return 0; |
| 4636 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4637 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4638 | case Intrinsic::eh_typeid_for: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4639 | // Find the type id for the given typeinfo. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4640 | GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0)); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4641 | unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV); |
| 4642 | Res = DAG.getConstant(TypeID, MVT::i32); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4643 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4644 | return 0; |
| 4645 | } |
| 4646 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4647 | case Intrinsic::eh_return_i32: |
| 4648 | case Intrinsic::eh_return_i64: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4649 | DAG.getMachineFunction().getMMI().setCallsEHReturn(true); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4650 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl, |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4651 | MVT::Other, |
| 4652 | getControlRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4653 | getValue(I.getArgOperand(0)), |
| 4654 | getValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4655 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4656 | case Intrinsic::eh_unwind_init: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4657 | DAG.getMachineFunction().getMMI().setCallsUnwindInit(true); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4658 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4659 | case Intrinsic::eh_dwarf_cfa: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4660 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl, |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 4661 | TLI.getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4662 | SDValue Offset = DAG.getNode(ISD::ADD, sdl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4663 | TLI.getPointerTy(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4664 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4665 | TLI.getPointerTy()), |
| 4666 | CfaArg); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4667 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4668 | TLI.getPointerTy(), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4669 | DAG.getConstant(0, TLI.getPointerTy())); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4670 | setValue(&I, DAG.getNode(ISD::ADD, sdl, TLI.getPointerTy(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4671 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4672 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4673 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4674 | case Intrinsic::eh_sjlj_callsite: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4675 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4676 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0)); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4677 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4678 | assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4679 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4680 | MMI.setCurrentCallSite(CI->getZExtValue()); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4681 | return 0; |
| 4682 | } |
Bill Wendling | 6ef9417 | 2011-09-28 03:36:43 +0000 | [diff] [blame] | 4683 | case Intrinsic::eh_sjlj_functioncontext: { |
| 4684 | // Get and store the index of the function context. |
| 4685 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
Bill Wendling | adbf7b2 | 2011-09-28 03:52:41 +0000 | [diff] [blame] | 4686 | AllocaInst *FnCtx = |
| 4687 | cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts()); |
Bill Wendling | 6ef9417 | 2011-09-28 03:36:43 +0000 | [diff] [blame] | 4688 | int FI = FuncInfo.StaticAllocaMap[FnCtx]; |
| 4689 | MFI->setFunctionContextIndex(FI); |
| 4690 | return 0; |
| 4691 | } |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4692 | case Intrinsic::eh_sjlj_setjmp: { |
Bill Wendling | ce370cf | 2011-10-07 21:25:38 +0000 | [diff] [blame] | 4693 | SDValue Ops[2]; |
| 4694 | Ops[0] = getRoot(); |
| 4695 | Ops[1] = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4696 | SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl, |
Bill Wendling | ce370cf | 2011-10-07 21:25:38 +0000 | [diff] [blame] | 4697 | DAG.getVTList(MVT::i32, MVT::Other), |
| 4698 | Ops, 2); |
| 4699 | setValue(&I, Op.getValue(0)); |
| 4700 | DAG.setRoot(Op.getValue(1)); |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4701 | return 0; |
| 4702 | } |
Jim Grosbach | 5eb1951 | 2010-05-22 01:06:18 +0000 | [diff] [blame] | 4703 | case Intrinsic::eh_sjlj_longjmp: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4704 | DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other, |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 4705 | getRoot(), getValue(I.getArgOperand(0)))); |
| 4706 | return 0; |
| 4707 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4708 | |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4709 | case Intrinsic::x86_mmx_pslli_w: |
| 4710 | case Intrinsic::x86_mmx_pslli_d: |
| 4711 | case Intrinsic::x86_mmx_pslli_q: |
| 4712 | case Intrinsic::x86_mmx_psrli_w: |
| 4713 | case Intrinsic::x86_mmx_psrli_d: |
| 4714 | case Intrinsic::x86_mmx_psrli_q: |
| 4715 | case Intrinsic::x86_mmx_psrai_w: |
| 4716 | case Intrinsic::x86_mmx_psrai_d: { |
| 4717 | SDValue ShAmt = getValue(I.getArgOperand(1)); |
| 4718 | if (isa<ConstantSDNode>(ShAmt)) { |
| 4719 | visitTargetIntrinsic(I, Intrinsic); |
| 4720 | return 0; |
| 4721 | } |
| 4722 | unsigned NewIntrinsic = 0; |
| 4723 | EVT ShAmtVT = MVT::v2i32; |
| 4724 | switch (Intrinsic) { |
| 4725 | case Intrinsic::x86_mmx_pslli_w: |
| 4726 | NewIntrinsic = Intrinsic::x86_mmx_psll_w; |
| 4727 | break; |
| 4728 | case Intrinsic::x86_mmx_pslli_d: |
| 4729 | NewIntrinsic = Intrinsic::x86_mmx_psll_d; |
| 4730 | break; |
| 4731 | case Intrinsic::x86_mmx_pslli_q: |
| 4732 | NewIntrinsic = Intrinsic::x86_mmx_psll_q; |
| 4733 | break; |
| 4734 | case Intrinsic::x86_mmx_psrli_w: |
| 4735 | NewIntrinsic = Intrinsic::x86_mmx_psrl_w; |
| 4736 | break; |
| 4737 | case Intrinsic::x86_mmx_psrli_d: |
| 4738 | NewIntrinsic = Intrinsic::x86_mmx_psrl_d; |
| 4739 | break; |
| 4740 | case Intrinsic::x86_mmx_psrli_q: |
| 4741 | NewIntrinsic = Intrinsic::x86_mmx_psrl_q; |
| 4742 | break; |
| 4743 | case Intrinsic::x86_mmx_psrai_w: |
| 4744 | NewIntrinsic = Intrinsic::x86_mmx_psra_w; |
| 4745 | break; |
| 4746 | case Intrinsic::x86_mmx_psrai_d: |
| 4747 | NewIntrinsic = Intrinsic::x86_mmx_psra_d; |
| 4748 | break; |
| 4749 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 4750 | } |
| 4751 | |
| 4752 | // The vector shift intrinsics with scalars uses 32b shift amounts but |
| 4753 | // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits |
| 4754 | // to be zero. |
| 4755 | // We must do this early because v2i32 is not a legal type. |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4756 | SDValue ShOps[2]; |
| 4757 | ShOps[0] = ShAmt; |
| 4758 | ShOps[1] = DAG.getConstant(0, MVT::i32); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4759 | ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2); |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4760 | EVT DestVT = TLI.getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4761 | ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt); |
| 4762 | Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT, |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4763 | DAG.getConstant(NewIntrinsic, MVT::i32), |
| 4764 | getValue(I.getArgOperand(0)), ShAmt); |
| 4765 | setValue(&I, Res); |
| 4766 | return 0; |
| 4767 | } |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4768 | case Intrinsic::x86_avx_vinsertf128_pd_256: |
| 4769 | case Intrinsic::x86_avx_vinsertf128_ps_256: |
Craig Topper | b45c969 | 2012-04-07 22:32:29 +0000 | [diff] [blame] | 4770 | case Intrinsic::x86_avx_vinsertf128_si_256: |
| 4771 | case Intrinsic::x86_avx2_vinserti128: { |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4772 | EVT DestVT = TLI.getValueType(I.getType()); |
| 4773 | EVT ElVT = TLI.getValueType(I.getArgOperand(1)->getType()); |
| 4774 | uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) * |
| 4775 | ElVT.getVectorNumElements(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4776 | Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT, |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4777 | getValue(I.getArgOperand(0)), |
| 4778 | getValue(I.getArgOperand(1)), |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4779 | DAG.getIntPtrConstant(Idx)); |
| 4780 | setValue(&I, Res); |
| 4781 | return 0; |
| 4782 | } |
| 4783 | case Intrinsic::x86_avx_vextractf128_pd_256: |
| 4784 | case Intrinsic::x86_avx_vextractf128_ps_256: |
| 4785 | case Intrinsic::x86_avx_vextractf128_si_256: |
| 4786 | case Intrinsic::x86_avx2_vextracti128: { |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4787 | EVT DestVT = TLI.getValueType(I.getType()); |
| 4788 | uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) * |
| 4789 | DestVT.getVectorNumElements(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4790 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT, |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4791 | getValue(I.getArgOperand(0)), |
| 4792 | DAG.getIntPtrConstant(Idx)); |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4793 | setValue(&I, Res); |
| 4794 | return 0; |
| 4795 | } |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4796 | case Intrinsic::convertff: |
| 4797 | case Intrinsic::convertfsi: |
| 4798 | case Intrinsic::convertfui: |
| 4799 | case Intrinsic::convertsif: |
| 4800 | case Intrinsic::convertuif: |
| 4801 | case Intrinsic::convertss: |
| 4802 | case Intrinsic::convertsu: |
| 4803 | case Intrinsic::convertus: |
| 4804 | case Intrinsic::convertuu: { |
| 4805 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4806 | switch (Intrinsic) { |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 4807 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4808 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4809 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4810 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4811 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4812 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4813 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4814 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4815 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4816 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4817 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4818 | EVT DestVT = TLI.getValueType(I.getType()); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4819 | const Value *Op1 = I.getArgOperand(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4820 | Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4821 | DAG.getValueType(DestVT), |
| 4822 | DAG.getValueType(getValue(Op1).getValueType()), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4823 | getValue(I.getArgOperand(1)), |
| 4824 | getValue(I.getArgOperand(2)), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4825 | Code); |
| 4826 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4827 | return 0; |
| 4828 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4829 | case Intrinsic::powi: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4830 | setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4831 | getValue(I.getArgOperand(1)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4832 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4833 | case Intrinsic::log: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4834 | setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4835 | return 0; |
| 4836 | case Intrinsic::log2: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4837 | setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4838 | return 0; |
| 4839 | case Intrinsic::log10: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4840 | setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4841 | return 0; |
| 4842 | case Intrinsic::exp: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4843 | setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4844 | return 0; |
| 4845 | case Intrinsic::exp2: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4846 | setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4847 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4848 | case Intrinsic::pow: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4849 | setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)), |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4850 | getValue(I.getArgOperand(1)), DAG, TLI)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4851 | return 0; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4852 | case Intrinsic::sqrt: |
Peter Collingbourne | b34d3aa | 2012-05-28 21:48:37 +0000 | [diff] [blame] | 4853 | case Intrinsic::fabs: |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4854 | case Intrinsic::sin: |
| 4855 | case Intrinsic::cos: |
Dan Gohman | 27db99f | 2012-07-26 17:43:27 +0000 | [diff] [blame] | 4856 | case Intrinsic::floor: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4857 | case Intrinsic::ceil: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4858 | case Intrinsic::trunc: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4859 | case Intrinsic::rint: |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4860 | case Intrinsic::nearbyint: { |
| 4861 | unsigned Opcode; |
| 4862 | switch (Intrinsic) { |
| 4863 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 4864 | case Intrinsic::sqrt: Opcode = ISD::FSQRT; break; |
| 4865 | case Intrinsic::fabs: Opcode = ISD::FABS; break; |
| 4866 | case Intrinsic::sin: Opcode = ISD::FSIN; break; |
| 4867 | case Intrinsic::cos: Opcode = ISD::FCOS; break; |
| 4868 | case Intrinsic::floor: Opcode = ISD::FFLOOR; break; |
| 4869 | case Intrinsic::ceil: Opcode = ISD::FCEIL; break; |
| 4870 | case Intrinsic::trunc: Opcode = ISD::FTRUNC; break; |
| 4871 | case Intrinsic::rint: Opcode = ISD::FRINT; break; |
| 4872 | case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break; |
| 4873 | } |
| 4874 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4875 | setValue(&I, DAG.getNode(Opcode, sdl, |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4876 | getValue(I.getArgOperand(0)).getValueType(), |
| 4877 | getValue(I.getArgOperand(0)))); |
| 4878 | return 0; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4879 | } |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 4880 | case Intrinsic::fma: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4881 | setValue(&I, DAG.getNode(ISD::FMA, sdl, |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 4882 | getValue(I.getArgOperand(0)).getValueType(), |
| 4883 | getValue(I.getArgOperand(0)), |
| 4884 | getValue(I.getArgOperand(1)), |
| 4885 | getValue(I.getArgOperand(2)))); |
| 4886 | return 0; |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4887 | case Intrinsic::fmuladd: { |
| 4888 | EVT VT = TLI.getValueType(I.getType()); |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 4889 | if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict && |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 4890 | TLI.isFMAFasterThanMulAndAdd(VT)){ |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4891 | setValue(&I, DAG.getNode(ISD::FMA, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4892 | getValue(I.getArgOperand(0)).getValueType(), |
| 4893 | getValue(I.getArgOperand(0)), |
| 4894 | getValue(I.getArgOperand(1)), |
| 4895 | getValue(I.getArgOperand(2)))); |
| 4896 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4897 | SDValue Mul = DAG.getNode(ISD::FMUL, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4898 | getValue(I.getArgOperand(0)).getValueType(), |
| 4899 | getValue(I.getArgOperand(0)), |
| 4900 | getValue(I.getArgOperand(1))); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4901 | SDValue Add = DAG.getNode(ISD::FADD, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4902 | getValue(I.getArgOperand(0)).getValueType(), |
| 4903 | Mul, |
| 4904 | getValue(I.getArgOperand(2))); |
| 4905 | setValue(&I, Add); |
| 4906 | } |
| 4907 | return 0; |
| 4908 | } |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4909 | case Intrinsic::convert_to_fp16: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4910 | setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4911 | MVT::i16, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4912 | return 0; |
| 4913 | case Intrinsic::convert_from_fp16: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4914 | setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4915 | MVT::f32, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4916 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4917 | case Intrinsic::pcmarker: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4918 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4919 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4920 | return 0; |
| 4921 | } |
| 4922 | case Intrinsic::readcyclecounter: { |
| 4923 | SDValue Op = getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4924 | Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl, |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4925 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4926 | &Op, 1); |
| 4927 | setValue(&I, Res); |
| 4928 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4929 | return 0; |
| 4930 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4931 | case Intrinsic::bswap: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4932 | setValue(&I, DAG.getNode(ISD::BSWAP, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4933 | getValue(I.getArgOperand(0)).getValueType(), |
| 4934 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4935 | return 0; |
| 4936 | case Intrinsic::cttz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4937 | SDValue Arg = getValue(I.getArgOperand(0)); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4938 | ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4939 | EVT Ty = Arg.getValueType(); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4940 | setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4941 | sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4942 | return 0; |
| 4943 | } |
| 4944 | case Intrinsic::ctlz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4945 | SDValue Arg = getValue(I.getArgOperand(0)); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4946 | ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4947 | EVT Ty = Arg.getValueType(); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4948 | setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4949 | sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4950 | return 0; |
| 4951 | } |
| 4952 | case Intrinsic::ctpop: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4953 | SDValue Arg = getValue(I.getArgOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4954 | EVT Ty = Arg.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4955 | setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4956 | return 0; |
| 4957 | } |
| 4958 | case Intrinsic::stacksave: { |
| 4959 | SDValue Op = getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4960 | Res = DAG.getNode(ISD::STACKSAVE, sdl, |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4961 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
| 4962 | setValue(&I, Res); |
| 4963 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4964 | return 0; |
| 4965 | } |
| 4966 | case Intrinsic::stackrestore: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4967 | Res = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4968 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4969 | return 0; |
| 4970 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 4971 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4972 | // Emit code into the DAG to store the stack guard onto the stack. |
| 4973 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4974 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4975 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4976 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4977 | SDValue Src = getValue(I.getArgOperand(0)); // The guard's value. |
| 4978 | AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4979 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 4980 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4981 | MFI->setStackProtectorIndex(FI); |
| 4982 | |
| 4983 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 4984 | |
| 4985 | // Store the stack protector onto the stack. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4986 | Res = DAG.getStore(getRoot(), sdl, Src, FIN, |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 4987 | MachinePointerInfo::getFixedStack(FI), |
| 4988 | true, false, 0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4989 | setValue(&I, Res); |
| 4990 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 4991 | return 0; |
| 4992 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4993 | case Intrinsic::objectsize: { |
| 4994 | // 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] | 4995 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 4996 | |
| 4997 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 4998 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4999 | SDValue Arg = getValue(I.getCalledValue()); |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 5000 | EVT Ty = Arg.getValueType(); |
| 5001 | |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 5002 | if (CI->isZero()) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5003 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5004 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5005 | Res = DAG.getConstant(0, Ty); |
| 5006 | |
| 5007 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5008 | return 0; |
| 5009 | } |
Justin Holewinski | c2b7f5f | 2013-05-21 14:37:16 +0000 | [diff] [blame] | 5010 | case Intrinsic::annotation: |
| 5011 | case Intrinsic::ptr_annotation: |
| 5012 | // Drop the intrinsic, but forward the value |
| 5013 | setValue(&I, getValue(I.getOperand(0))); |
| 5014 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5015 | case Intrinsic::var_annotation: |
| 5016 | // Discard annotate attributes |
| 5017 | return 0; |
| 5018 | |
| 5019 | case Intrinsic::init_trampoline: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5020 | const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5021 | |
| 5022 | SDValue Ops[6]; |
| 5023 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5024 | Ops[1] = getValue(I.getArgOperand(0)); |
| 5025 | Ops[2] = getValue(I.getArgOperand(1)); |
| 5026 | Ops[3] = getValue(I.getArgOperand(2)); |
| 5027 | Ops[4] = DAG.getSrcValue(I.getArgOperand(0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5028 | Ops[5] = DAG.getSrcValue(F); |
| 5029 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5030 | Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5031 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5032 | DAG.setRoot(Res); |
| 5033 | return 0; |
| 5034 | } |
| 5035 | case Intrinsic::adjust_trampoline: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5036 | setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl, |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5037 | TLI.getPointerTy(), |
| 5038 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5039 | return 0; |
| 5040 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5041 | case Intrinsic::gcroot: |
| 5042 | if (GFI) { |
Bill Wendling | 95dd442 | 2012-05-01 22:50:45 +0000 | [diff] [blame] | 5043 | const Value *Alloca = I.getArgOperand(0)->stripPointerCasts(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5044 | const Constant *TypeMap = cast<Constant>(I.getArgOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5045 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5046 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 5047 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 5048 | } |
| 5049 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5050 | case Intrinsic::gcread: |
| 5051 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5052 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5053 | case Intrinsic::flt_rounds: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5054 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5055 | return 0; |
Jakub Staszak | 9da9934 | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 5056 | |
| 5057 | case Intrinsic::expect: { |
| 5058 | // Just replace __builtin_expect(exp, c) with EXP. |
| 5059 | setValue(&I, getValue(I.getArgOperand(0))); |
| 5060 | return 0; |
| 5061 | } |
| 5062 | |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5063 | case Intrinsic::debugtrap: |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5064 | case Intrinsic::trap: { |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5065 | StringRef TrapFuncName = TM.Options.getTrapFunctionName(); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5066 | if (TrapFuncName.empty()) { |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5067 | ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ? |
| 5068 | ISD::TRAP : ISD::DEBUGTRAP; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5069 | DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot())); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5070 | return 0; |
| 5071 | } |
| 5072 | TargetLowering::ArgListTy Args; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5073 | TargetLowering:: |
| 5074 | CallLoweringInfo CLI(getRoot(), I.getType(), |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5075 | false, false, false, false, 0, CallingConv::C, |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 5076 | /*isTailCall=*/false, |
| 5077 | /*doesNotRet=*/false, /*isReturnValueUsed=*/true, |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5078 | DAG.getExternalSymbol(TrapFuncName.data(), TLI.getPointerTy()), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5079 | Args, DAG, sdl); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5080 | std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5081 | DAG.setRoot(Result.second); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5082 | return 0; |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5083 | } |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5084 | |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 5085 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5086 | case Intrinsic::sadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5087 | case Intrinsic::usub_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5088 | case Intrinsic::ssub_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5089 | case Intrinsic::umul_with_overflow: |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5090 | case Intrinsic::smul_with_overflow: { |
| 5091 | ISD::NodeType Op; |
| 5092 | switch (Intrinsic) { |
| 5093 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 5094 | case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break; |
| 5095 | case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break; |
| 5096 | case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break; |
| 5097 | case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break; |
| 5098 | case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break; |
| 5099 | case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break; |
| 5100 | } |
| 5101 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 5102 | SDValue Op2 = getValue(I.getArgOperand(1)); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 5103 | |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5104 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5105 | setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2)); |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5106 | return 0; |
| 5107 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5108 | case Intrinsic::prefetch: { |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5109 | SDValue Ops[5]; |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5110 | unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5111 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5112 | Ops[1] = getValue(I.getArgOperand(0)); |
| 5113 | Ops[2] = getValue(I.getArgOperand(1)); |
| 5114 | Ops[3] = getValue(I.getArgOperand(2)); |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5115 | Ops[4] = getValue(I.getArgOperand(3)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5116 | DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl, |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5117 | DAG.getVTList(MVT::Other), |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5118 | &Ops[0], 5, |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5119 | EVT::getIntegerVT(*Context, 8), |
| 5120 | MachinePointerInfo(I.getArgOperand(0)), |
| 5121 | 0, /* align */ |
| 5122 | false, /* volatile */ |
| 5123 | rw==0, /* read */ |
| 5124 | rw==1)); /* write */ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5125 | return 0; |
| 5126 | } |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5127 | case Intrinsic::lifetime_start: |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5128 | case Intrinsic::lifetime_end: { |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5129 | bool IsStart = (Intrinsic == Intrinsic::lifetime_start); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5130 | // Stack coloring is not enabled in O0, discard region information. |
| 5131 | if (TM.getOptLevel() == CodeGenOpt::None) |
| 5132 | return 0; |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5133 | |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5134 | SmallVector<Value *, 4> Allocas; |
| 5135 | GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD); |
| 5136 | |
| 5137 | for (SmallVector<Value*, 4>::iterator Object = Allocas.begin(), |
| 5138 | E = Allocas.end(); Object != E; ++Object) { |
| 5139 | AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object); |
| 5140 | |
| 5141 | // Could not find an Alloca. |
| 5142 | if (!LifetimeObject) |
| 5143 | continue; |
| 5144 | |
| 5145 | int FI = FuncInfo.StaticAllocaMap[LifetimeObject]; |
| 5146 | |
| 5147 | SDValue Ops[2]; |
| 5148 | Ops[0] = getRoot(); |
| 5149 | Ops[1] = DAG.getFrameIndex(FI, TLI.getPointerTy(), true); |
| 5150 | unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END); |
| 5151 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5152 | Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5153 | DAG.setRoot(Res); |
| 5154 | } |
Nadav Rotem | 5882e56 | 2013-02-01 19:25:23 +0000 | [diff] [blame] | 5155 | return 0; |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5156 | } |
| 5157 | case Intrinsic::invariant_start: |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5158 | // Discard region information. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5159 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5160 | return 0; |
| 5161 | case Intrinsic::invariant_end: |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5162 | // Discard region information. |
| 5163 | return 0; |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 5164 | case Intrinsic::donothing: |
| 5165 | // ignore |
| 5166 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5167 | } |
| 5168 | } |
| 5169 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5170 | void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5171 | bool isTailCall, |
| 5172 | MachineBasicBlock *LandingPad) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5173 | PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 5174 | FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 5175 | Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5176 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 5177 | MCSymbol *BeginLabel = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5178 | |
| 5179 | TargetLowering::ArgListTy Args; |
| 5180 | TargetLowering::ArgListEntry Entry; |
| 5181 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5182 | |
| 5183 | // Check whether the function can return without sret-demotion. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 5184 | SmallVector<ISD::OutputArg, 4> Outs; |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 5185 | GetReturnInfo(RetTy, CS.getAttributes(), Outs, TLI); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5186 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5187 | bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(), |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 5188 | DAG.getMachineFunction(), |
| 5189 | FTy->isVarArg(), Outs, |
| 5190 | FTy->getContext()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5191 | |
| 5192 | SDValue DemoteStackSlot; |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5193 | int DemoteStackIdx = -100; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5194 | |
| 5195 | if (!CanLowerReturn) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5196 | uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize( |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5197 | FTy->getReturnType()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5198 | unsigned Align = TLI.getDataLayout()->getPrefTypeAlignment( |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5199 | FTy->getReturnType()); |
| 5200 | MachineFunction &MF = DAG.getMachineFunction(); |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5201 | DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5202 | Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5203 | |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5204 | DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI.getPointerTy()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5205 | Entry.Node = DemoteStackSlot; |
| 5206 | Entry.Ty = StackSlotPtrType; |
| 5207 | Entry.isSExt = false; |
| 5208 | Entry.isZExt = false; |
| 5209 | Entry.isInReg = false; |
| 5210 | Entry.isSRet = true; |
| 5211 | Entry.isNest = false; |
| 5212 | Entry.isByVal = false; |
Stephen Lin | 456ca04 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 5213 | Entry.isReturned = false; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5214 | Entry.Alignment = Align; |
| 5215 | Args.push_back(Entry); |
| 5216 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 5217 | } |
| 5218 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5219 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5220 | i != e; ++i) { |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 5221 | const Value *V = *i; |
| 5222 | |
| 5223 | // Skip empty types |
| 5224 | if (V->getType()->isEmptyTy()) |
| 5225 | continue; |
| 5226 | |
| 5227 | SDValue ArgNode = getValue(V); |
| 5228 | Entry.Node = ArgNode; Entry.Ty = V->getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5229 | |
| 5230 | unsigned attrInd = i - CS.arg_begin() + 1; |
Stephen Lin | 456ca04 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 5231 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 5232 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 5233 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 5234 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 5235 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 5236 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
| 5237 | Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned); |
| 5238 | Entry.Alignment = CS.getParamAlignment(attrInd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5239 | Args.push_back(Entry); |
| 5240 | } |
| 5241 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5242 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5243 | // Insert a label before the invoke call to mark the try range. This can be |
| 5244 | // used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5245 | BeginLabel = MMI.getContext().CreateTempSymbol(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 5246 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5247 | // For SjLj, keep track of which landing pads go with which invokes |
| 5248 | // so as to maintain the ordering of pads in the LSDA. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5249 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5250 | if (CallSiteIndex) { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5251 | MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
Bill Wendling | 30e6740 | 2011-10-05 22:24:35 +0000 | [diff] [blame] | 5252 | LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex); |
Bill Wendling | a8512ed | 2011-10-04 22:00:35 +0000 | [diff] [blame] | 5253 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5254 | // Now that the call site is handled, stop tracking it. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5255 | MMI.setCurrentCallSite(0); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5256 | } |
| 5257 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5258 | // Both PendingLoads and PendingExports must be flushed here; |
| 5259 | // this call might not return. |
| 5260 | (void)getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5261 | DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5262 | } |
| 5263 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5264 | // Check if target-independent constraints permit a tail call here. |
| 5265 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
Bill Wendling | 1a17bd2 | 2013-01-18 21:50:24 +0000 | [diff] [blame] | 5266 | if (isTailCall && !isInTailCallPosition(CS, TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5267 | isTailCall = false; |
| 5268 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5269 | TargetLowering:: |
| 5270 | CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5271 | getCurSDLoc(), CS); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5272 | std::pair<SDValue,SDValue> Result = TLI.LowerCallTo(CLI); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5273 | assert((isTailCall || Result.second.getNode()) && |
| 5274 | "Non-null chain expected with non-tail call!"); |
| 5275 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 5276 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5277 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5278 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5279 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5280 | // The instruction result is the result of loading from the |
| 5281 | // hidden sret parameter. |
| 5282 | SmallVector<EVT, 1> PVTs; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5283 | Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5284 | |
| 5285 | ComputeValueVTs(TLI, PtrRetTy, PVTs); |
| 5286 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 5287 | EVT PtrVT = PVTs[0]; |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5288 | |
| 5289 | SmallVector<EVT, 4> RetTys; |
| 5290 | SmallVector<uint64_t, 4> Offsets; |
| 5291 | RetTy = FTy->getReturnType(); |
| 5292 | ComputeValueVTs(TLI, RetTy, RetTys, &Offsets); |
| 5293 | |
| 5294 | unsigned NumValues = RetTys.size(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5295 | SmallVector<SDValue, 4> Values(NumValues); |
| 5296 | SmallVector<SDValue, 4> Chains(NumValues); |
| 5297 | |
| 5298 | for (unsigned i = 0; i < NumValues; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5299 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5300 | DemoteStackSlot, |
| 5301 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5302 | SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5303 | MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5304 | false, false, false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5305 | Values[i] = L; |
| 5306 | Chains[i] = L.getValue(1); |
| 5307 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5308 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5309 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5310 | MVT::Other, &Chains[0], NumValues); |
| 5311 | PendingLoads.push_back(Chain); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5312 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5313 | setValue(CS.getInstruction(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5314 | DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5315 | DAG.getVTList(&RetTys[0], RetTys.size()), |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5316 | &Values[0], Values.size())); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5317 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5318 | |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5319 | if (!Result.second.getNode()) { |
Evan Cheng | c249e48 | 2011-04-01 19:57:01 +0000 | [diff] [blame] | 5320 | // As a special case, a null chain means that a tail call has been emitted and |
| 5321 | // the DAG root is already updated. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5322 | HasTailCall = true; |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5323 | } else { |
| 5324 | DAG.setRoot(Result.second); |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5325 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5326 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5327 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5328 | // Insert a label at the end of the invoke call to mark the try range. This |
| 5329 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5330 | MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5331 | DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5332 | |
| 5333 | // Inform MachineModuleInfo of range. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5334 | MMI.addInvoke(LandingPad, BeginLabel, EndLabel); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5335 | } |
| 5336 | } |
| 5337 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5338 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 5339 | /// value is equal or not-equal to zero. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5340 | static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) { |
| 5341 | 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] | 5342 | UI != E; ++UI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5343 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5344 | if (IC->isEquality()) |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5345 | if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5346 | if (C->isNullValue()) |
| 5347 | continue; |
| 5348 | // Unknown instruction. |
| 5349 | return false; |
| 5350 | } |
| 5351 | return true; |
| 5352 | } |
| 5353 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5354 | static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5355 | Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5356 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5357 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5358 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 5359 | // input is from a string literal. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5360 | if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5361 | // Cast pointer to the type we really want to load. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5362 | LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput), |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5363 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5364 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5365 | if (const Constant *LoadCst = |
| 5366 | ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput), |
| 5367 | Builder.TD)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5368 | return Builder.getValue(LoadCst); |
| 5369 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5370 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5371 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 5372 | // still constant memory, the input chain can be the entry node. |
| 5373 | SDValue Root; |
| 5374 | bool ConstantMemory = false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5375 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5376 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 5377 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 5378 | Root = Builder.DAG.getEntryNode(); |
| 5379 | ConstantMemory = true; |
| 5380 | } else { |
| 5381 | // Do not serialize non-volatile loads against each other. |
| 5382 | Root = Builder.DAG.getRoot(); |
| 5383 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5384 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5385 | SDValue Ptr = Builder.getValue(PtrVal); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5386 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5387 | Ptr, MachinePointerInfo(PtrVal), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5388 | false /*volatile*/, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5389 | false /*nontemporal*/, |
| 5390 | false /*isinvariant*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5391 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5392 | if (!ConstantMemory) |
| 5393 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 5394 | return LoadVal; |
| 5395 | } |
| 5396 | |
| 5397 | |
| 5398 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 5399 | /// If so, return true and lower it, otherwise return false and it will be |
| 5400 | /// lowered like a normal call. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5401 | bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5402 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 5403 | if (I.getNumArgOperands() != 3) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5404 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5405 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5406 | const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 5407 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5408 | !I.getArgOperand(2)->getType()->isIntegerTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 5409 | !I.getType()->isIntegerTy()) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5410 | return false; |
| 5411 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5412 | const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5413 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5414 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 5415 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5416 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 5417 | bool ActuallyDoIt = true; |
| 5418 | MVT LoadVT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5419 | Type *LoadTy; |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5420 | switch (Size->getZExtValue()) { |
| 5421 | default: |
| 5422 | LoadVT = MVT::Other; |
| 5423 | LoadTy = 0; |
| 5424 | ActuallyDoIt = false; |
| 5425 | break; |
| 5426 | case 2: |
| 5427 | LoadVT = MVT::i16; |
| 5428 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 5429 | break; |
| 5430 | case 4: |
| 5431 | LoadVT = MVT::i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5432 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5433 | break; |
| 5434 | case 8: |
| 5435 | LoadVT = MVT::i64; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5436 | LoadTy = Type::getInt64Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5437 | break; |
| 5438 | /* |
| 5439 | case 16: |
| 5440 | LoadVT = MVT::v4i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5441 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5442 | LoadTy = VectorType::get(LoadTy, 4); |
| 5443 | break; |
| 5444 | */ |
| 5445 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5446 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5447 | // This turns into unaligned loads. We only do this if the target natively |
| 5448 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 5449 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5450 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5451 | // Require that we can find a legal MVT, and only do this if the target |
| 5452 | // supports unaligned loads of that type. Expanding into byte loads would |
| 5453 | // bloat the code. |
| 5454 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 5455 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 5456 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
| 5457 | if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT)) |
| 5458 | ActuallyDoIt = false; |
| 5459 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5460 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5461 | if (ActuallyDoIt) { |
| 5462 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 5463 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5464 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5465 | SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal, |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5466 | ISD::SETNE); |
| 5467 | EVT CallVT = TLI.getValueType(I.getType(), true); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5468 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurSDLoc(), CallVT)); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5469 | return true; |
| 5470 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5471 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5472 | |
| 5473 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5474 | return false; |
| 5475 | } |
| 5476 | |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5477 | /// visitUnaryFloatCall - If a call instruction is a unary floating-point |
| 5478 | /// operation (as expected), translate it to an SDNode with the specified opcode |
| 5479 | /// and return true. |
| 5480 | bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I, |
| 5481 | unsigned Opcode) { |
| 5482 | // Sanity check that it really is a unary floating-point call. |
| 5483 | if (I.getNumArgOperands() != 1 || |
| 5484 | !I.getArgOperand(0)->getType()->isFloatingPointTy() || |
| 5485 | I.getType() != I.getArgOperand(0)->getType() || |
| 5486 | !I.onlyReadsMemory()) |
| 5487 | return false; |
| 5488 | |
| 5489 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5490 | setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp)); |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5491 | return true; |
| 5492 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5493 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5494 | void SelectionDAGBuilder::visitCall(const CallInst &I) { |
Chris Lattner | 598751e | 2010-07-05 05:36:21 +0000 | [diff] [blame] | 5495 | // Handle inline assembly differently. |
| 5496 | if (isa<InlineAsm>(I.getCalledValue())) { |
| 5497 | visitInlineAsm(&I); |
| 5498 | return; |
| 5499 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5500 | |
Michael J. Spencer | 391b43b | 2010-10-21 20:49:23 +0000 | [diff] [blame] | 5501 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Michael J. Spencer | c9c137b | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 5502 | ComputeUsesVAFloatArgument(I, &MMI); |
Michael J. Spencer | 391b43b | 2010-10-21 20:49:23 +0000 | [diff] [blame] | 5503 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5504 | const char *RenameFn = 0; |
| 5505 | if (Function *F = I.getCalledFunction()) { |
| 5506 | if (F->isDeclaration()) { |
Chris Lattner | 598751e | 2010-07-05 05:36:21 +0000 | [diff] [blame] | 5507 | if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 5508 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 5509 | RenameFn = visitIntrinsicCall(I, IID); |
| 5510 | if (!RenameFn) |
| 5511 | return; |
| 5512 | } |
| 5513 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5514 | if (unsigned IID = F->getIntrinsicID()) { |
| 5515 | RenameFn = visitIntrinsicCall(I, IID); |
| 5516 | if (!RenameFn) |
| 5517 | return; |
| 5518 | } |
| 5519 | } |
| 5520 | |
| 5521 | // Check for well-known libc/libm calls. If the function is internal, it |
| 5522 | // can't be a library call. |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5523 | LibFunc::Func Func; |
| 5524 | if (!F->hasLocalLinkage() && F->hasName() && |
| 5525 | LibInfo->getLibFunc(F->getName(), Func) && |
| 5526 | LibInfo->hasOptimizedCodeGen(Func)) { |
| 5527 | switch (Func) { |
| 5528 | default: break; |
| 5529 | case LibFunc::copysign: |
| 5530 | case LibFunc::copysignf: |
| 5531 | case LibFunc::copysignl: |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 5532 | if (I.getNumArgOperands() == 2 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5533 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 5534 | I.getType() == I.getArgOperand(0)->getType() && |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5535 | I.getType() == I.getArgOperand(1)->getType() && |
| 5536 | I.onlyReadsMemory()) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5537 | SDValue LHS = getValue(I.getArgOperand(0)); |
| 5538 | SDValue RHS = getValue(I.getArgOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5539 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(), |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 5540 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5541 | return; |
| 5542 | } |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5543 | break; |
| 5544 | case LibFunc::fabs: |
| 5545 | case LibFunc::fabsf: |
| 5546 | case LibFunc::fabsl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5547 | if (visitUnaryFloatCall(I, ISD::FABS)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5548 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5549 | break; |
| 5550 | case LibFunc::sin: |
| 5551 | case LibFunc::sinf: |
| 5552 | case LibFunc::sinl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5553 | if (visitUnaryFloatCall(I, ISD::FSIN)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5554 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5555 | break; |
| 5556 | case LibFunc::cos: |
| 5557 | case LibFunc::cosf: |
| 5558 | case LibFunc::cosl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5559 | if (visitUnaryFloatCall(I, ISD::FCOS)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5560 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5561 | break; |
| 5562 | case LibFunc::sqrt: |
| 5563 | case LibFunc::sqrtf: |
| 5564 | case LibFunc::sqrtl: |
Preston Gurd | b704d23 | 2013-05-27 15:44:35 +0000 | [diff] [blame] | 5565 | case LibFunc::sqrt_finite: |
| 5566 | case LibFunc::sqrtf_finite: |
| 5567 | case LibFunc::sqrtl_finite: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5568 | if (visitUnaryFloatCall(I, ISD::FSQRT)) |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 5569 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5570 | break; |
| 5571 | case LibFunc::floor: |
| 5572 | case LibFunc::floorf: |
| 5573 | case LibFunc::floorl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5574 | if (visitUnaryFloatCall(I, ISD::FFLOOR)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5575 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5576 | break; |
| 5577 | case LibFunc::nearbyint: |
| 5578 | case LibFunc::nearbyintf: |
| 5579 | case LibFunc::nearbyintl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5580 | if (visitUnaryFloatCall(I, ISD::FNEARBYINT)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5581 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5582 | break; |
| 5583 | case LibFunc::ceil: |
| 5584 | case LibFunc::ceilf: |
| 5585 | case LibFunc::ceill: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5586 | if (visitUnaryFloatCall(I, ISD::FCEIL)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5587 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5588 | break; |
| 5589 | case LibFunc::rint: |
| 5590 | case LibFunc::rintf: |
| 5591 | case LibFunc::rintl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5592 | if (visitUnaryFloatCall(I, ISD::FRINT)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5593 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5594 | break; |
| 5595 | case LibFunc::trunc: |
| 5596 | case LibFunc::truncf: |
| 5597 | case LibFunc::truncl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5598 | if (visitUnaryFloatCall(I, ISD::FTRUNC)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5599 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5600 | break; |
| 5601 | case LibFunc::log2: |
| 5602 | case LibFunc::log2f: |
| 5603 | case LibFunc::log2l: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5604 | if (visitUnaryFloatCall(I, ISD::FLOG2)) |
Owen Anderson | 4e0adfa | 2011-12-15 00:54:12 +0000 | [diff] [blame] | 5605 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5606 | break; |
| 5607 | case LibFunc::exp2: |
| 5608 | case LibFunc::exp2f: |
| 5609 | case LibFunc::exp2l: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5610 | if (visitUnaryFloatCall(I, ISD::FEXP2)) |
Owen Anderson | 4e0adfa | 2011-12-15 00:54:12 +0000 | [diff] [blame] | 5611 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5612 | break; |
| 5613 | case LibFunc::memcmp: |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5614 | if (visitMemCmpCall(I)) |
| 5615 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5616 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5617 | } |
| 5618 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5619 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5620 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5621 | SDValue Callee; |
| 5622 | if (!RenameFn) |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5623 | Callee = getValue(I.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5624 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 5625 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5626 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 5627 | // Check if we can potentially perform a tail call. More detailed checking is |
| 5628 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 5629 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5632 | namespace { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5633 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5634 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 5635 | /// lowering. |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5636 | class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 5637 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5638 | /// CallOperand - If this is the result output operand or a clobber |
| 5639 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 5640 | /// This gets modified as the asm is processed. |
| 5641 | SDValue CallOperand; |
| 5642 | |
| 5643 | /// AssignedRegs - If this is a register or register class operand, this |
| 5644 | /// contains the set of register corresponding to the operand. |
| 5645 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5646 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5647 | explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5648 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 5649 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5650 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5651 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5652 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5653 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5654 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5655 | const TargetLowering &TLI, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5656 | const DataLayout *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5657 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5658 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5659 | if (isa<BasicBlock>(CallOperandVal)) |
| 5660 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5661 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5662 | llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5663 | |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5664 | // FIXME: code duplicated from TargetLowering::ParseConstraints(). |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5665 | // If this is an indirect operand, the operand is a pointer to the |
| 5666 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5667 | if (isIndirect) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5668 | llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5669 | if (!PtrTy) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5670 | report_fatal_error("Indirect operand for inline asm not a pointer!"); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5671 | OpTy = PtrTy->getElementType(); |
| 5672 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5673 | |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5674 | // Look for vector wrapped in a struct. e.g. { <16 x i8> }. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5675 | if (StructType *STy = dyn_cast<StructType>(OpTy)) |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5676 | if (STy->getNumElements() == 1) |
| 5677 | OpTy = STy->getElementType(0); |
| 5678 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5679 | // If OpTy is not a single value, it may be a struct/union that we |
| 5680 | // can tile with integers. |
| 5681 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 5682 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 5683 | switch (BitSize) { |
| 5684 | default: break; |
| 5685 | case 1: |
| 5686 | case 8: |
| 5687 | case 16: |
| 5688 | case 32: |
| 5689 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 5690 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5691 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5692 | break; |
| 5693 | } |
| 5694 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5695 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5696 | return TLI.getValueType(OpTy, true); |
| 5697 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5698 | }; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5699 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 5700 | typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector; |
| 5701 | |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5702 | } // end anonymous namespace |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5703 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5704 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 5705 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 5706 | /// register allocator to handle the assignment process. However, if the asm |
| 5707 | /// 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] | 5708 | /// allocation. This produces generally horrible, but correct, code. |
| 5709 | /// |
| 5710 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5711 | /// |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5712 | static void GetRegistersForValue(SelectionDAG &DAG, |
| 5713 | const TargetLowering &TLI, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5714 | SDLoc DL, |
Benjamin Kramer | 8b93ff2 | 2012-02-24 14:01:17 +0000 | [diff] [blame] | 5715 | SDISelAsmOperandInfo &OpInfo) { |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5716 | LLVMContext &Context = *DAG.getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5717 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5718 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5719 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5720 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5721 | // If this is a constraint for a single physreg, or a constraint for a |
| 5722 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5723 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5724 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5725 | OpInfo.ConstraintVT); |
| 5726 | |
| 5727 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5728 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5729 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5730 | // cast of the input value. More generally, handle any case where the input |
| 5731 | // value disagrees with the register class we plan to stick this in. |
| 5732 | if (OpInfo.Type == InlineAsm::isInput && |
| 5733 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5734 | // 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] | 5735 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5736 | // vector types). |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5737 | MVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5738 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5739 | OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5740 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5741 | OpInfo.ConstraintVT = RegVT; |
| 5742 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5743 | // If the input is a FP value and we want it in FP registers, do a |
| 5744 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5745 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5746 | // machine. |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5747 | RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits()); |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5748 | OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5749 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5750 | OpInfo.ConstraintVT = RegVT; |
| 5751 | } |
| 5752 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5753 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5754 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5755 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5756 | |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5757 | MVT RegVT; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5758 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5759 | |
| 5760 | // If this is a constraint for a specific physical register, like {r17}, |
| 5761 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5762 | if (unsigned AssignedReg = PhysReg.first) { |
| 5763 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5764 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5765 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5766 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5767 | // Get the actual register value type. This is important, because the user |
| 5768 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5769 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5770 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5771 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5772 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5773 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5774 | |
| 5775 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5776 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5777 | TargetRegisterClass::iterator I = RC->begin(); |
| 5778 | for (; *I != AssignedReg; ++I) |
| 5779 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5780 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5781 | // Already added the first reg. |
| 5782 | --NumRegs; ++I; |
| 5783 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5784 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5785 | Regs.push_back(*I); |
| 5786 | } |
| 5787 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5788 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5789 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5790 | return; |
| 5791 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5792 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5793 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5794 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5795 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5796 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5797 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5798 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5799 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5800 | // Create the appropriate number of virtual registers. |
| 5801 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5802 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5803 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5804 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5805 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5806 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5807 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5808 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5809 | // Otherwise, we couldn't allocate enough registers for this. |
| 5810 | } |
| 5811 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5812 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5813 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5814 | void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) { |
| 5815 | const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5816 | |
| 5817 | /// ConstraintOperands - Information about all of the constraints. |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 5818 | SDISelAsmOperandInfoVector ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5819 | |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5820 | TargetLowering::AsmOperandInfoVector |
| 5821 | TargetConstraints = TLI.ParseConstraints(CS); |
| 5822 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5823 | bool hasMemory = false; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5824 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5825 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5826 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5827 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 5828 | ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i])); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5829 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5830 | |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5831 | MVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5832 | |
| 5833 | // Compute the value type for each operand. |
| 5834 | switch (OpInfo.Type) { |
| 5835 | case InlineAsm::isOutput: |
| 5836 | // Indirect outputs just consume an argument. |
| 5837 | if (OpInfo.isIndirect) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5838 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5839 | break; |
| 5840 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5841 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5842 | // The return value of the call is this value. As such, there is no |
| 5843 | // corresponding argument. |
Nick Lewycky | 8de3400 | 2011-09-30 22:19:53 +0000 | [diff] [blame] | 5844 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5845 | if (StructType *STy = dyn_cast<StructType>(CS.getType())) { |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5846 | OpVT = TLI.getSimpleValueType(STy->getElementType(ResNo)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5847 | } else { |
| 5848 | assert(ResNo == 0 && "Asm only has one result!"); |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5849 | OpVT = TLI.getSimpleValueType(CS.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5850 | } |
| 5851 | ++ResNo; |
| 5852 | break; |
| 5853 | case InlineAsm::isInput: |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5854 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5855 | break; |
| 5856 | case InlineAsm::isClobber: |
| 5857 | // Nothing to do. |
| 5858 | break; |
| 5859 | } |
| 5860 | |
| 5861 | // If this is an input or an indirect output, process the call argument. |
| 5862 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5863 | if (OpInfo.CallOperandVal) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5864 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5865 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5866 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5867 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5868 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5869 | |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5870 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD). |
| 5871 | getSimpleVT(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5872 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5873 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5874 | OpInfo.ConstraintVT = OpVT; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5875 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5876 | // Indirect operand accesses access memory. |
| 5877 | if (OpInfo.isIndirect) |
| 5878 | hasMemory = true; |
| 5879 | else { |
| 5880 | for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) { |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5881 | TargetLowering::ConstraintType |
| 5882 | CType = TLI.getConstraintType(OpInfo.Codes[j]); |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5883 | if (CType == TargetLowering::C_Memory) { |
| 5884 | hasMemory = true; |
| 5885 | break; |
| 5886 | } |
| 5887 | } |
| 5888 | } |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5889 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5890 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5891 | SDValue Chain, Flag; |
| 5892 | |
| 5893 | // We won't need to flush pending loads if this asm doesn't touch |
| 5894 | // memory and is nonvolatile. |
| 5895 | if (hasMemory || IA->hasSideEffects()) |
| 5896 | Chain = getRoot(); |
| 5897 | else |
| 5898 | Chain = DAG.getRoot(); |
| 5899 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5900 | // Second pass over the constraints: compute which constraint option to use |
| 5901 | // and assign registers to constraints that want a specific physreg. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5902 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5903 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5904 | |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5905 | // If this is an output operand with a matching input operand, look up the |
| 5906 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5907 | // other is floating point, or their sizes are different, flag it as an |
| 5908 | // error. |
| 5909 | if (OpInfo.hasMatchingInput()) { |
| 5910 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5911 | |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5912 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 5913 | std::pair<unsigned, const TargetRegisterClass*> MatchRC = |
| 5914 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
Evan Cheng | 1dafa70 | 2011-08-23 19:17:21 +0000 | [diff] [blame] | 5915 | OpInfo.ConstraintVT); |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 5916 | std::pair<unsigned, const TargetRegisterClass*> InputRC = |
| 5917 | TLI.getRegForInlineAsmConstraint(Input.ConstraintCode, |
Evan Cheng | 1dafa70 | 2011-08-23 19:17:21 +0000 | [diff] [blame] | 5918 | Input.ConstraintVT); |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5919 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5920 | Input.ConstraintVT.isInteger()) || |
Eric Christopher | 5427ede | 2011-07-14 20:13:52 +0000 | [diff] [blame] | 5921 | (MatchRC.second != InputRC.second)) { |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5922 | report_fatal_error("Unsupported asm: input constraint" |
| 5923 | " with a matching output constraint of" |
| 5924 | " incompatible type!"); |
| 5925 | } |
| 5926 | Input.ConstraintVT = OpInfo.ConstraintVT; |
| 5927 | } |
| 5928 | } |
| 5929 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5930 | // Compute the constraint code and ConstraintType to use. |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 5931 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5932 | |
Eric Christopher | fffe363 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 5933 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5934 | OpInfo.Type == InlineAsm::isClobber) |
| 5935 | continue; |
| 5936 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5937 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5938 | // need to to provide an address for the memory input. |
| 5939 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5940 | !OpInfo.isIndirect) { |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5941 | assert((OpInfo.isMultipleAlternative || |
| 5942 | (OpInfo.Type == InlineAsm::isInput)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5943 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5944 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5945 | // Memory operands really want the address of the value. If we don't have |
| 5946 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5947 | // it to a stack slot. |
Eric Christopher | e0b42c0 | 2011-06-03 17:21:23 +0000 | [diff] [blame] | 5948 | // TODO: This isn't quite right. We need to handle these according to |
| 5949 | // the addressing mode that the constraint wants. Also, this may take |
| 5950 | // an additional register for the computation and we don't want that |
| 5951 | // either. |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 5952 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5953 | // If the operand is a float, integer, or vector constant, spill to a |
| 5954 | // constant pool entry to get its address. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5955 | const Value *OpVal = OpInfo.CallOperandVal; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5956 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 5957 | isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5958 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5959 | TLI.getPointerTy()); |
| 5960 | } else { |
| 5961 | // Otherwise, create a stack slot and emit a store to it before the |
| 5962 | // asm. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5963 | Type *Ty = OpVal->getType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5964 | uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty); |
| 5965 | unsigned Align = TLI.getDataLayout()->getPrefTypeAlignment(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5966 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 5967 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5968 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5969 | Chain = DAG.getStore(Chain, getCurSDLoc(), |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5970 | OpInfo.CallOperand, StackSlot, |
| 5971 | MachinePointerInfo::getFixedStack(SSFI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5972 | false, false, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5973 | OpInfo.CallOperand = StackSlot; |
| 5974 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5975 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5976 | // There is no longer a Value* corresponding to this operand. |
| 5977 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5978 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5979 | // It is now an indirect operand. |
| 5980 | OpInfo.isIndirect = true; |
| 5981 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5982 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5983 | // If this constraint is for a specific register, allocate it before |
| 5984 | // anything else. |
| 5985 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5986 | GetRegistersForValue(DAG, TLI, getCurSDLoc(), OpInfo); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5987 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5988 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5989 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 5990 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5991 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 5992 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5993 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5994 | // C_Register operands have already been allocated, Other/Memory don't need |
| 5995 | // to be. |
| 5996 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5997 | GetRegistersForValue(DAG, TLI, getCurSDLoc(), OpInfo); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5998 | } |
| 5999 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6000 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 6001 | std::vector<SDValue> AsmNodeOperands; |
| 6002 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 6003 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 6004 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 6005 | TLI.getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6006 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6007 | // If we have a !srcloc metadata node associated with it, we want to attach |
| 6008 | // this to the ultimately generated inline asm machineinstr. To do this, we |
| 6009 | // pass in the third operand as this (potentially null) inline asm MDNode. |
| 6010 | const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc"); |
| 6011 | AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6012 | |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6013 | // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore |
| 6014 | // bits as operand 3. |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 6015 | unsigned ExtraInfo = 0; |
| 6016 | if (IA->hasSideEffects()) |
| 6017 | ExtraInfo |= InlineAsm::Extra_HasSideEffects; |
| 6018 | if (IA->isAlignStack()) |
| 6019 | ExtraInfo |= InlineAsm::Extra_IsAlignStack; |
Chad Rosier | 77fffa6 | 2012-09-05 22:17:43 +0000 | [diff] [blame] | 6020 | // Set the asm dialect. |
Chad Rosier | 2f1d815 | 2012-09-05 22:40:13 +0000 | [diff] [blame] | 6021 | ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect; |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6022 | |
| 6023 | // Determine if this InlineAsm MayLoad or MayStore based on the constraints. |
| 6024 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 6025 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
| 6026 | |
| 6027 | // Compute the constraint code and ConstraintType to use. |
| 6028 | TLI.ComputeConstraintToUse(OpInfo, SDValue()); |
| 6029 | |
Chad Rosier | dfa4cec | 2012-10-30 20:01:12 +0000 | [diff] [blame] | 6030 | // Ideally, we would only check against memory constraints. However, the |
| 6031 | // meaning of an other constraint can be target-specific and we can't easily |
| 6032 | // reason about it. Therefore, be conservative and set MayLoad/MayStore |
| 6033 | // for other constriants as well. |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6034 | if (OpInfo.ConstraintType == TargetLowering::C_Memory || |
| 6035 | OpInfo.ConstraintType == TargetLowering::C_Other) { |
| 6036 | if (OpInfo.Type == InlineAsm::isInput) |
| 6037 | ExtraInfo |= InlineAsm::Extra_MayLoad; |
| 6038 | else if (OpInfo.Type == InlineAsm::isOutput) |
| 6039 | ExtraInfo |= InlineAsm::Extra_MayStore; |
Eric Christopher | fffe363 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 6040 | else if (OpInfo.Type == InlineAsm::isClobber) |
| 6041 | ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore); |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6042 | } |
| 6043 | } |
| 6044 | |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 6045 | AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo, |
| 6046 | TLI.getPointerTy())); |
Dale Johannesen | f1e309e | 2010-07-02 20:16:09 +0000 | [diff] [blame] | 6047 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6048 | // Loop over all of the inputs, copying the operand values into the |
| 6049 | // appropriate registers and processing the output regs. |
| 6050 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6051 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6052 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 6053 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6054 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6055 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 6056 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 6057 | |
| 6058 | switch (OpInfo.Type) { |
| 6059 | case InlineAsm::isOutput: { |
| 6060 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 6061 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 6062 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 6063 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 6064 | |
| 6065 | // Add information to the INLINEASM node to know about this output. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6066 | unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
| 6067 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6068 | TLI.getPointerTy())); |
| 6069 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 6070 | break; |
| 6071 | } |
| 6072 | |
| 6073 | // Otherwise, this is a register or register class output. |
| 6074 | |
| 6075 | // Copy the output from the appropriate register. Find a register that |
| 6076 | // we can use. |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6077 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 6078 | LLVMContext &Ctx = *DAG.getContext(); |
| 6079 | Ctx.emitError(CS.getInstruction(), |
| 6080 | "couldn't allocate output register for constraint '" + |
| 6081 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6082 | break; |
| 6083 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6084 | |
| 6085 | // If this is an indirect operand, store through the pointer after the |
| 6086 | // asm. |
| 6087 | if (OpInfo.isIndirect) { |
| 6088 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 6089 | OpInfo.CallOperandVal)); |
| 6090 | } else { |
| 6091 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 6092 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6093 | // Concatenate this output onto the outputs list. |
| 6094 | RetValRegs.append(OpInfo.AssignedRegs); |
| 6095 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6096 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6097 | // Add information to the INLINEASM node to know that this register is |
| 6098 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 6099 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6100 | InlineAsm::Kind_RegDefEarlyClobber : |
| 6101 | InlineAsm::Kind_RegDef, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6102 | false, |
| 6103 | 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6104 | DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6105 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6106 | break; |
| 6107 | } |
| 6108 | case InlineAsm::isInput: { |
| 6109 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6110 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 6111 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6112 | // If this is required to match an output register we have already set, |
| 6113 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 6114 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6115 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6116 | // Scan until we find the definition we already emitted of this operand. |
| 6117 | // When we find it, create a RegsForValue operand. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6118 | unsigned CurOp = InlineAsm::Op_FirstOperand; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6119 | for (; OperandNo; --OperandNo) { |
| 6120 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6121 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6122 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6123 | assert((InlineAsm::isRegDefKind(OpFlag) || |
| 6124 | InlineAsm::isRegDefEarlyClobberKind(OpFlag) || |
| 6125 | InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6126 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6129 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6130 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6131 | if (InlineAsm::isRegDefKind(OpFlag) || |
| 6132 | InlineAsm::isRegDefEarlyClobberKind(OpFlag)) { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6133 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6134 | if (OpInfo.isIndirect) { |
| 6135 | // This happens on gcc/testsuite/gcc.dg/pr8788-1.c |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 6136 | LLVMContext &Ctx = *DAG.getContext(); |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6137 | Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:" |
| 6138 | " don't know how to handle tied " |
| 6139 | "indirect register inputs"); |
Chad Rosier | 7590022 | 2013-03-01 19:12:05 +0000 | [diff] [blame] | 6140 | report_fatal_error("Cannot handle indirect register inputs!"); |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6141 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6142 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6143 | RegsForValue MatchedRegs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6144 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 6145 | MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6146 | MatchedRegs.RegVTs.push_back(RegVT); |
| 6147 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6148 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6149 | i != e; ++i) { |
| 6150 | if (const TargetRegisterClass *RC = TLI.getRegClassFor(RegVT)) |
| 6151 | MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC)); |
| 6152 | else { |
| 6153 | LLVMContext &Ctx = *DAG.getContext(); |
| 6154 | Ctx.emitError(CS.getInstruction(), "inline asm error: This value" |
| 6155 | " type register class is not natively supported!"); |
| 6156 | report_fatal_error("inline asm error: This value type register " |
| 6157 | "class is not natively supported!"); |
| 6158 | } |
| 6159 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6160 | // Use the produced MatchedRegs object to |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6161 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6162 | Chain, &Flag, CS.getInstruction()); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6163 | MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6164 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6165 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6166 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6167 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6168 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6169 | assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!"); |
| 6170 | assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 && |
| 6171 | "Unexpected number of operands"); |
| 6172 | // Add information to the INLINEASM node to know about this input. |
| 6173 | // See InlineAsm.h isUseOperandTiedToDef. |
| 6174 | OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag, |
| 6175 | OpInfo.getMatchedOperand()); |
| 6176 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
| 6177 | TLI.getPointerTy())); |
| 6178 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 6179 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6180 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6181 | |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6182 | // Treat indirect 'X' constraint as memory. |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6183 | if (OpInfo.ConstraintType == TargetLowering::C_Other && |
| 6184 | OpInfo.isIndirect) |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6185 | OpInfo.ConstraintType = TargetLowering::C_Memory; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6186 | |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6187 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6188 | std::vector<SDValue> Ops; |
Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 6189 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode, |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 6190 | Ops, DAG); |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6191 | if (Ops.empty()) { |
| 6192 | LLVMContext &Ctx = *DAG.getContext(); |
| 6193 | Ctx.emitError(CS.getInstruction(), |
| 6194 | "invalid operand for inline asm constraint '" + |
| 6195 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6196 | break; |
| 6197 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6198 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6199 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6200 | unsigned ResOpType = |
| 6201 | InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6202 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6203 | TLI.getPointerTy())); |
| 6204 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 6205 | break; |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6206 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6207 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6208 | if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6209 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 6210 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 6211 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6212 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6213 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6214 | unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 6215 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6216 | TLI.getPointerTy())); |
| 6217 | AsmNodeOperands.push_back(InOperandVal); |
| 6218 | break; |
| 6219 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6220 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6221 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 6222 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 6223 | "Unknown constraint type!"); |
Eric Christopher | 9eb4f8a | 2012-07-02 21:16:43 +0000 | [diff] [blame] | 6224 | |
| 6225 | // TODO: Support this. |
| 6226 | if (OpInfo.isIndirect) { |
| 6227 | LLVMContext &Ctx = *DAG.getContext(); |
| 6228 | Ctx.emitError(CS.getInstruction(), |
| 6229 | "Don't know how to handle indirect register inputs yet " |
| 6230 | "for constraint '" + Twine(OpInfo.ConstraintCode) + "'"); |
| 6231 | break; |
| 6232 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6233 | |
| 6234 | // Copy the input into the appropriate registers. |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6235 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 6236 | LLVMContext &Ctx = *DAG.getContext(); |
| 6237 | Ctx.emitError(CS.getInstruction(), |
| 6238 | "couldn't allocate input reg for constraint '" + |
| 6239 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6240 | break; |
| 6241 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6242 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6243 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6244 | Chain, &Flag, CS.getInstruction()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6245 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6246 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6247 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6248 | break; |
| 6249 | } |
| 6250 | case InlineAsm::isClobber: { |
| 6251 | // Add the clobbered value to the operand list, so that the register |
| 6252 | // allocator is aware that the physreg got clobbered. |
| 6253 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Jakob Stoklund Olesen | f792fa9 | 2011-06-27 04:08:33 +0000 | [diff] [blame] | 6254 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6255 | false, 0, DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6256 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6257 | break; |
| 6258 | } |
| 6259 | } |
| 6260 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6261 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6262 | // Finish up input operands. Set the input chain and add the flag last. |
Dale Johannesen | f1e309e | 2010-07-02 20:16:09 +0000 | [diff] [blame] | 6263 | AsmNodeOperands[InlineAsm::Op_InputChain] = Chain; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6264 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6265 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6266 | Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(), |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 6267 | DAG.getVTList(MVT::Other, MVT::Glue), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6268 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 6269 | Flag = Chain.getValue(1); |
| 6270 | |
| 6271 | // If this asm returns a register value, copy the result from that register |
| 6272 | // and set it as the value of the call. |
| 6273 | if (!RetValRegs.Regs.empty()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6274 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6275 | Chain, &Flag, CS.getInstruction()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6276 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6277 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 6278 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6279 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6280 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6281 | // If any of the results of the inline asm is a vector, it may have the |
| 6282 | // wrong width/num elts. This can happen for register classes that can |
| 6283 | // contain multiple different value types. The preg or vreg allocated may |
| 6284 | // not have the same VT as was expected. Convert it to the right type |
| 6285 | // with bit_convert. |
| 6286 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6287 | Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 6288 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6289 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6290 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6291 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 6292 | // If a result value was tied to an input value, the computed result may |
| 6293 | // have a wider width than the expected result. Extract the relevant |
| 6294 | // portion. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6295 | Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6296 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6297 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6298 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 6299 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6300 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6301 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 6302 | // Don't need to use this as a chain in this case. |
| 6303 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 6304 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6305 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6306 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6307 | std::vector<std::pair<SDValue, const Value *> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6308 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6309 | // Process indirect outputs, first output all of the flagged copies out of |
| 6310 | // physregs. |
| 6311 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 6312 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6313 | const Value *Ptr = IndirectStoresToEmit[i].second; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6314 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6315 | Chain, &Flag, IA); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6316 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
| 6317 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6318 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6319 | // Emit the non-flagged stores from the physregs. |
| 6320 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6321 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6322 | SDValue Val = DAG.getStore(Chain, getCurSDLoc(), |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6323 | StoresToEmit[i].first, |
| 6324 | getValue(StoresToEmit[i].second), |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 6325 | MachinePointerInfo(StoresToEmit[i].second), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6326 | false, false, 0); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6327 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6330 | if (!OutChains.empty()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6331 | Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6332 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6333 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6334 | DAG.setRoot(Chain); |
| 6335 | } |
| 6336 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6337 | void SelectionDAGBuilder::visitVAStart(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6338 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6339 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6340 | getValue(I.getArgOperand(0)), |
| 6341 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6344 | void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6345 | const DataLayout &TD = *TLI.getDataLayout(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6346 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 6347 | getRoot(), getValue(I.getOperand(0)), |
Rafael Espindola | cbeeae2 | 2010-07-11 04:01:49 +0000 | [diff] [blame] | 6348 | DAG.getSrcValue(I.getOperand(0)), |
Rafael Espindola | 9d544d0 | 2010-07-12 18:11:17 +0000 | [diff] [blame] | 6349 | TD.getABITypeAlignment(I.getType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6350 | setValue(&I, V); |
| 6351 | DAG.setRoot(V.getValue(1)); |
| 6352 | } |
| 6353 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6354 | void SelectionDAGBuilder::visitVAEnd(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6355 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6356 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6357 | getValue(I.getArgOperand(0)), |
| 6358 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6359 | } |
| 6360 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6361 | void SelectionDAGBuilder::visitVACopy(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6362 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6363 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6364 | getValue(I.getArgOperand(0)), |
| 6365 | getValue(I.getArgOperand(1)), |
| 6366 | DAG.getSrcValue(I.getArgOperand(0)), |
| 6367 | DAG.getSrcValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6368 | } |
| 6369 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6370 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6371 | /// implementation, which just calls LowerCall. |
| 6372 | /// FIXME: When all targets are |
| 6373 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6374 | std::pair<SDValue, SDValue> |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6375 | TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const { |
Stephen Lin | 3484da9 | 2013-04-30 22:49:28 +0000 | [diff] [blame] | 6376 | // Handle the incoming return values from the call. |
| 6377 | CLI.Ins.clear(); |
| 6378 | SmallVector<EVT, 4> RetTys; |
| 6379 | ComputeValueVTs(*this, CLI.RetTy, RetTys); |
| 6380 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 6381 | EVT VT = RetTys[I]; |
| 6382 | MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT); |
| 6383 | unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT); |
| 6384 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 6385 | ISD::InputArg MyFlags; |
| 6386 | MyFlags.VT = RegisterVT; |
| 6387 | MyFlags.Used = CLI.IsReturnValueUsed; |
| 6388 | if (CLI.RetSExt) |
| 6389 | MyFlags.Flags.setSExt(); |
| 6390 | if (CLI.RetZExt) |
| 6391 | MyFlags.Flags.setZExt(); |
| 6392 | if (CLI.IsInReg) |
| 6393 | MyFlags.Flags.setInReg(); |
| 6394 | CLI.Ins.push_back(MyFlags); |
| 6395 | } |
| 6396 | } |
| 6397 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6398 | // Handle all of the outgoing arguments. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6399 | CLI.Outs.clear(); |
| 6400 | CLI.OutVals.clear(); |
| 6401 | ArgListTy &Args = CLI.Args; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6402 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6403 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6404 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 6405 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 6406 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6407 | EVT VT = ValueVTs[Value]; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6408 | Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6409 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 6410 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6411 | ISD::ArgFlagsTy Flags; |
| 6412 | unsigned OriginalAlignment = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6413 | getDataLayout()->getABITypeAlignment(ArgTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6414 | |
| 6415 | if (Args[i].isZExt) |
| 6416 | Flags.setZExt(); |
| 6417 | if (Args[i].isSExt) |
| 6418 | Flags.setSExt(); |
| 6419 | if (Args[i].isInReg) |
| 6420 | Flags.setInReg(); |
| 6421 | if (Args[i].isSRet) |
| 6422 | Flags.setSRet(); |
| 6423 | if (Args[i].isByVal) { |
| 6424 | Flags.setByVal(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6425 | PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 6426 | Type *ElementTy = Ty->getElementType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6427 | Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6428 | // For ByVal, alignment should come from FE. BE will guess if this |
| 6429 | // info is not there but there are cases it cannot get right. |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6430 | unsigned FrameAlign; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6431 | if (Args[i].Alignment) |
| 6432 | FrameAlign = Args[i].Alignment; |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6433 | else |
| 6434 | FrameAlign = getByValTypeAlignment(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6435 | Flags.setByValAlign(FrameAlign); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6436 | } |
| 6437 | if (Args[i].isNest) |
| 6438 | Flags.setNest(); |
| 6439 | Flags.setOrigAlign(OriginalAlignment); |
| 6440 | |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6441 | MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6442 | unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6443 | SmallVector<SDValue, 4> Parts(NumParts); |
| 6444 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 6445 | |
| 6446 | if (Args[i].isSExt) |
| 6447 | ExtendKind = ISD::SIGN_EXTEND; |
| 6448 | else if (Args[i].isZExt) |
| 6449 | ExtendKind = ISD::ZERO_EXTEND; |
| 6450 | |
Stephen Lin | 3484da9 | 2013-04-30 22:49:28 +0000 | [diff] [blame] | 6451 | // Conservatively only handle 'returned' on non-vectors for now |
| 6452 | if (Args[i].isReturned && !Op.getValueType().isVector()) { |
| 6453 | assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues && |
| 6454 | "unexpected use of 'returned'"); |
| 6455 | // Before passing 'returned' to the target lowering code, ensure that |
| 6456 | // either the register MVT and the actual EVT are the same size or that |
| 6457 | // the return value and argument are extended in the same way; in these |
| 6458 | // cases it's safe to pass the argument register value unchanged as the |
| 6459 | // return register value (although it's at the target's option whether |
| 6460 | // to do so) |
| 6461 | // TODO: allow code generation to take advantage of partially preserved |
| 6462 | // registers rather than clobbering the entire register when the |
| 6463 | // parameter extension method is not compatible with the return |
| 6464 | // extension method |
| 6465 | if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) || |
| 6466 | (ExtendKind != ISD::ANY_EXTEND && |
| 6467 | CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt)) |
| 6468 | Flags.setReturned(); |
| 6469 | } |
| 6470 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6471 | getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6472 | PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6473 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6474 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6475 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 6476 | ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(), |
Manman Ren | 0a1544d | 2012-11-01 23:49:58 +0000 | [diff] [blame] | 6477 | i < CLI.NumFixedArgs, |
| 6478 | i, j*Parts[j].getValueType().getStoreSize()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6479 | if (NumParts > 1 && j == 0) |
| 6480 | MyFlags.Flags.setSplit(); |
| 6481 | else if (j != 0) |
| 6482 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6483 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6484 | CLI.Outs.push_back(MyFlags); |
| 6485 | CLI.OutVals.push_back(Parts[j]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6486 | } |
| 6487 | } |
| 6488 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6489 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6490 | SmallVector<SDValue, 4> InVals; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6491 | CLI.Chain = LowerCall(CLI, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6492 | |
| 6493 | // Verify that the target's LowerCall behaved as expected. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6494 | assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6495 | "LowerCall didn't return a valid chain!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6496 | assert((!CLI.IsTailCall || InVals.empty()) && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6497 | "LowerCall emitted a return value for a tail call!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6498 | assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6499 | "LowerCall didn't emit the correct number of values!"); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6500 | |
| 6501 | // For a tail call, the return value is merely live-out and there aren't |
| 6502 | // any nodes in the DAG representing it. Return a special value to |
| 6503 | // indicate that a tail call has been emitted and no more Instructions |
| 6504 | // should be processed in the current block. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6505 | if (CLI.IsTailCall) { |
| 6506 | CLI.DAG.setRoot(CLI.Chain); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6507 | return std::make_pair(SDValue(), SDValue()); |
| 6508 | } |
| 6509 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6510 | DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) { |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 6511 | assert(InVals[i].getNode() && |
| 6512 | "LowerCall emitted a null value!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6513 | assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() && |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 6514 | "LowerCall emitted a value with the wrong type!"); |
| 6515 | }); |
| 6516 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6517 | // Collect the legal value parts into potentially illegal values |
| 6518 | // that correspond to the original function's return values. |
| 6519 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6520 | if (CLI.RetSExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6521 | AssertOp = ISD::AssertSext; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6522 | else if (CLI.RetZExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6523 | AssertOp = ISD::AssertZext; |
| 6524 | SmallVector<SDValue, 4> ReturnValues; |
| 6525 | unsigned CurReg = 0; |
| 6526 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6527 | EVT VT = RetTys[I]; |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6528 | MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6529 | unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6530 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6531 | ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg], |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6532 | NumRegs, RegisterVT, VT, NULL, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 6533 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6534 | CurReg += NumRegs; |
| 6535 | } |
| 6536 | |
| 6537 | // For a function returning void, there is no return value. We can't create |
| 6538 | // such a node, so we just return a null return value in that case. In |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 6539 | // that case, nothing will actually look at the value. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6540 | if (ReturnValues.empty()) |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6541 | return std::make_pair(SDValue(), CLI.Chain); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6542 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6543 | SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL, |
| 6544 | CLI.DAG.getVTList(&RetTys[0], RetTys.size()), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6545 | &ReturnValues[0], ReturnValues.size()); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6546 | return std::make_pair(Res, CLI.Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6547 | } |
| 6548 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 6549 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 6550 | SmallVectorImpl<SDValue> &Results, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6551 | SelectionDAG &DAG) const { |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 6552 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 6553 | if (Res.getNode()) |
| 6554 | Results.push_back(Res); |
| 6555 | } |
| 6556 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6557 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6558 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6559 | } |
| 6560 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6561 | void |
| 6562 | SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) { |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 6563 | SDValue Op = getNonRegisterValue(V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6564 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 6565 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 6566 | "Copy from a reg to the same reg!"); |
| 6567 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 6568 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6569 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6570 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6571 | RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6572 | PendingExports.push_back(Chain); |
| 6573 | } |
| 6574 | |
| 6575 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 6576 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6577 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 6578 | /// entry block, return true. This includes arguments used by switches, since |
| 6579 | /// the switch may expand into multiple basic blocks. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6580 | static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6581 | // With FastISel active, we may be splitting blocks, so force creation |
| 6582 | // of virtual registers for all non-dead arguments. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6583 | if (FastISel) |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6584 | return A->use_empty(); |
| 6585 | |
| 6586 | const BasicBlock *Entry = A->getParent()->begin(); |
| 6587 | for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end(); |
| 6588 | UI != E; ++UI) { |
| 6589 | const User *U = *UI; |
| 6590 | if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U)) |
| 6591 | return false; // Use not in entry block. |
| 6592 | } |
| 6593 | return true; |
| 6594 | } |
| 6595 | |
Eli Bendersky | 6437d38 | 2013-02-28 23:09:18 +0000 | [diff] [blame] | 6596 | void SelectionDAGISel::LowerArguments(const Function &F) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6597 | SelectionDAG &DAG = SDB->DAG; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6598 | SDLoc dl = SDB->getCurSDLoc(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6599 | const DataLayout *TD = TLI.getDataLayout(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6600 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6601 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6602 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6603 | // Put in an sret pointer parameter before all the other parameters. |
| 6604 | SmallVector<EVT, 1> ValueVTs; |
| 6605 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 6606 | |
| 6607 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 6608 | // or one register. |
| 6609 | ISD::ArgFlagsTy Flags; |
| 6610 | Flags.setSRet(); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6611 | MVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]); |
Stepan Dyatkovskiy | 661afe7 | 2012-10-10 11:37:36 +0000 | [diff] [blame] | 6612 | ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6613 | Ins.push_back(RetArg); |
| 6614 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 6615 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6616 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6617 | unsigned Idx = 1; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6618 | 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] | 6619 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6620 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6621 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 6622 | bool isArgValueUsed = !I->use_empty(); |
| 6623 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 6624 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6625 | EVT VT = ValueVTs[Value]; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6626 | Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6627 | ISD::ArgFlagsTy Flags; |
| 6628 | unsigned OriginalAlignment = |
| 6629 | TD->getABITypeAlignment(ArgTy); |
| 6630 | |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6631 | if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6632 | Flags.setZExt(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6633 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6634 | Flags.setSExt(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6635 | if (F.getAttributes().hasAttribute(Idx, Attribute::InReg)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6636 | Flags.setInReg(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6637 | if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6638 | Flags.setSRet(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6639 | if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6640 | Flags.setByVal(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6641 | PointerType *Ty = cast<PointerType>(I->getType()); |
| 6642 | Type *ElementTy = Ty->getElementType(); |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6643 | Flags.setByValSize(TD->getTypeAllocSize(ElementTy)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6644 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 6645 | // this info is not there but there are cases it cannot get right. |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6646 | unsigned FrameAlign; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6647 | if (F.getParamAlignment(Idx)) |
| 6648 | FrameAlign = F.getParamAlignment(Idx); |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6649 | else |
| 6650 | FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6651 | Flags.setByValAlign(FrameAlign); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6652 | } |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6653 | if (F.getAttributes().hasAttribute(Idx, Attribute::Nest)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6654 | Flags.setNest(); |
| 6655 | Flags.setOrigAlign(OriginalAlignment); |
| 6656 | |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6657 | MVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6658 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6659 | for (unsigned i = 0; i != NumRegs; ++i) { |
Stepan Dyatkovskiy | 661afe7 | 2012-10-10 11:37:36 +0000 | [diff] [blame] | 6660 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed, |
| 6661 | Idx-1, i*RegisterVT.getStoreSize()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6662 | if (NumRegs > 1 && i == 0) |
| 6663 | MyFlags.Flags.setSplit(); |
| 6664 | // if it isn't first piece, alignment must be 1 |
| 6665 | else if (i > 0) |
| 6666 | MyFlags.Flags.setOrigAlign(1); |
| 6667 | Ins.push_back(MyFlags); |
| 6668 | } |
| 6669 | } |
| 6670 | } |
| 6671 | |
| 6672 | // Call the target to set up the argument values. |
| 6673 | SmallVector<SDValue, 8> InVals; |
| 6674 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 6675 | F.isVarArg(), Ins, |
| 6676 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6677 | |
| 6678 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6679 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6680 | "LowerFormalArguments didn't return a valid chain!"); |
| 6681 | assert(InVals.size() == Ins.size() && |
| 6682 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 6683 | DEBUG({ |
| 6684 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 6685 | assert(InVals[i].getNode() && |
| 6686 | "LowerFormalArguments emitted a null value!"); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 6687 | assert(EVT(Ins[i].VT) == InVals[i].getValueType() && |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 6688 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 6689 | } |
| 6690 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6691 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6692 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6693 | DAG.setRoot(NewRoot); |
| 6694 | |
| 6695 | // Set up the argument values. |
| 6696 | unsigned i = 0; |
| 6697 | Idx = 1; |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6698 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6699 | // Create a virtual register for the sret pointer, and put in a copy |
| 6700 | // from the sret argument into it. |
| 6701 | SmallVector<EVT, 1> ValueVTs; |
| 6702 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 6703 | MVT VT = ValueVTs[0].getSimpleVT(); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6704 | MVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6705 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6706 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6707 | RegVT, VT, NULL, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6708 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6709 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6710 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
| 6711 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)); |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6712 | FuncInfo->DemoteRegister = SRetReg; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6713 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6714 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6715 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6716 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6717 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 6718 | // Idx indexes LLVM arguments. Don't touch it. |
| 6719 | ++i; |
| 6720 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6721 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6722 | 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] | 6723 | ++I, ++Idx) { |
| 6724 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6725 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6726 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6727 | unsigned NumValues = ValueVTs.size(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 6728 | |
| 6729 | // If this argument is unused then remember its value. It is used to generate |
| 6730 | // debugging information. |
Adrian Prantl | df68803 | 2013-05-16 23:44:12 +0000 | [diff] [blame] | 6731 | if (I->use_empty() && NumValues) { |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 6732 | SDB->setUnusedArgValue(I, InVals[i]); |
| 6733 | |
Adrian Prantl | df68803 | 2013-05-16 23:44:12 +0000 | [diff] [blame] | 6734 | // Also remember any frame index for use in FastISel. |
| 6735 | if (FrameIndexSDNode *FI = |
| 6736 | dyn_cast<FrameIndexSDNode>(InVals[i].getNode())) |
| 6737 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
| 6738 | } |
| 6739 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6740 | for (unsigned Val = 0; Val != NumValues; ++Val) { |
| 6741 | EVT VT = ValueVTs[Val]; |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6742 | MVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6743 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6744 | |
| 6745 | if (!I->use_empty()) { |
| 6746 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6747 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6748 | AssertOp = ISD::AssertSext; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6749 | else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6750 | AssertOp = ISD::AssertZext; |
| 6751 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6752 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6753 | NumParts, PartVT, VT, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6754 | NULL, AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6755 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6756 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6757 | i += NumParts; |
| 6758 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6759 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6760 | // We don't need to do anything else for unused arguments. |
| 6761 | if (ArgValues.empty()) |
| 6762 | continue; |
| 6763 | |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6764 | // Note down frame index. |
| 6765 | if (FrameIndexSDNode *FI = |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 6766 | dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode())) |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6767 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 6768 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6769 | SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6770 | SDB->getCurSDLoc()); |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6771 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6772 | SDB->setValue(I, Res); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6773 | if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) { |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6774 | if (LoadSDNode *LNode = |
| 6775 | dyn_cast<LoadSDNode>(Res.getOperand(0).getNode())) |
| 6776 | if (FrameIndexSDNode *FI = |
| 6777 | dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) |
| 6778 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
| 6779 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6780 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6781 | // If this argument is live outside of the entry block, insert a copy from |
| 6782 | // wherever we got it to the vreg that other BB's will reference it as. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6783 | if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6784 | // If we can, though, try to skip creating an unnecessary vreg. |
| 6785 | // FIXME: This isn't very clean... it would be nice to make this more |
Eli Friedman | 7f33d67 | 2011-05-10 21:50:58 +0000 | [diff] [blame] | 6786 | // general. It's also subtly incompatible with the hacks FastISel |
| 6787 | // uses with vregs. |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6788 | unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg(); |
| 6789 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 6790 | FuncInfo->ValueMap[I] = Reg; |
| 6791 | continue; |
| 6792 | } |
| 6793 | } |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6794 | if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6795 | FuncInfo->InitializeRegForValue(I); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6796 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6797 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6798 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6799 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6800 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6801 | |
| 6802 | // Finally, if the target has anything special to do, allow it to do so. |
| 6803 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 6804 | EmitFunctionEntryCode(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6808 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6809 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6810 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6811 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6812 | /// the end. |
| 6813 | /// |
| 6814 | void |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6815 | SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6816 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6817 | |
| 6818 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6819 | |
| 6820 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6821 | // from this block. |
| 6822 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6823 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6824 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6825 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6826 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6827 | // If this terminator has multiple identical successors (common for |
| 6828 | // switches), only handle each succ once. |
| 6829 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6830 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6831 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6832 | |
| 6833 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6834 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6835 | // emitted yet. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6836 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 6837 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6838 | // Ignore dead phi's. |
| 6839 | if (PN->use_empty()) continue; |
| 6840 | |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 6841 | // Skip empty types |
| 6842 | if (PN->getType()->isEmptyTy()) |
| 6843 | continue; |
| 6844 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6845 | unsigned Reg; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6846 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6847 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6848 | if (const Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6849 | unsigned &RegOut = ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6850 | if (RegOut == 0) { |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 6851 | RegOut = FuncInfo.CreateRegs(C->getType()); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6852 | CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6853 | } |
| 6854 | Reg = RegOut; |
| 6855 | } else { |
Dan Gohman | c25ad63 | 2010-07-01 01:33:21 +0000 | [diff] [blame] | 6856 | DenseMap<const Value *, unsigned>::iterator I = |
| 6857 | FuncInfo.ValueMap.find(PHIOp); |
| 6858 | if (I != FuncInfo.ValueMap.end()) |
| 6859 | Reg = I->second; |
| 6860 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6861 | assert(isa<AllocaInst>(PHIOp) && |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6862 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6863 | "Didn't codegen value into a register!??"); |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 6864 | Reg = FuncInfo.CreateRegs(PHIOp->getType()); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6865 | CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6866 | } |
| 6867 | } |
| 6868 | |
| 6869 | // Remember that this register needs to added to the machine PHI node as |
| 6870 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6871 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6872 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6873 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6874 | EVT VT = ValueVTs[vti]; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6875 | unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6876 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6877 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6878 | Reg += NumRegisters; |
| 6879 | } |
| 6880 | } |
| 6881 | } |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6882 | ConstantsOut.clear(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6883 | } |