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" |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Optional.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Jakub Staszak | 81bfd71 | 2013-01-10 22:13:13 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ConstantFolding.h" |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Analysis.h" |
| 25 | #include "llvm/CodeGen/FastISel.h" |
| 26 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| 27 | #include "llvm/CodeGen/GCMetadata.h" |
| 28 | #include "llvm/CodeGen/GCStrategy.h" |
| 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 30 | #include "llvm/CodeGen/MachineFunction.h" |
| 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 32 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 33 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 35 | #include "llvm/CodeGen/SelectionDAG.h" |
Bill Wendling | 0bcbd1d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 37 | #include "llvm/IR/CallingConv.h" |
| 38 | #include "llvm/IR/Constants.h" |
| 39 | #include "llvm/IR/DataLayout.h" |
| 40 | #include "llvm/IR/DerivedTypes.h" |
| 41 | #include "llvm/IR/Function.h" |
| 42 | #include "llvm/IR/GlobalVariable.h" |
| 43 | #include "llvm/IR/InlineAsm.h" |
| 44 | #include "llvm/IR/Instructions.h" |
| 45 | #include "llvm/IR/IntrinsicInst.h" |
| 46 | #include "llvm/IR/Intrinsics.h" |
| 47 | #include "llvm/IR/LLVMContext.h" |
| 48 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 49 | #include "llvm/Support/CommandLine.h" |
| 50 | #include "llvm/Support/Debug.h" |
| 51 | #include "llvm/Support/ErrorHandling.h" |
| 52 | #include "llvm/Support/IntegersSubsetMapping.h" |
| 53 | #include "llvm/Support/MathExtras.h" |
| 54 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetFrameLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 57 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 58 | #include "llvm/Target/TargetLibraryInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 59 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 60 | #include "llvm/Target/TargetOptions.h" |
Richard Sandiford | ac168b8 | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 61 | #include "llvm/Target/TargetSelectionDAGInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 62 | #include <algorithm> |
| 63 | using namespace llvm; |
| 64 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 65 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 66 | /// some float libcalls (6, 8 or 12 bits). |
| 67 | static unsigned LimitFloatPrecision; |
| 68 | |
| 69 | static cl::opt<unsigned, true> |
| 70 | LimitFPPrecision("limit-float-precision", |
| 71 | cl::desc("Generate low-precision inline sequences " |
| 72 | "for some float libcalls"), |
| 73 | cl::location(LimitFloatPrecision), |
| 74 | cl::init(0)); |
| 75 | |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 76 | // Limit the width of DAG chains. This is important in general to prevent |
| 77 | // prevent DAG-based analysis from blowing up. For example, alias analysis and |
| 78 | // load clustering may not complete in reasonable time. It is difficult to |
| 79 | // recognize and avoid this situation within each individual analysis, and |
| 80 | // 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] | 81 | // the safe approach, and will be especially important with global DAGs. |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 82 | // |
| 83 | // MaxParallelChains default is arbitrarily high to avoid affecting |
| 84 | // 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] | 85 | // sequence over this should have been converted to llvm.memcpy by the |
| 86 | // frontend. It easy to induce this behavior with .ll code such as: |
| 87 | // %buffer = alloca [4096 x i8] |
| 88 | // %data = load [4096 x i8]* %argPtr |
| 89 | // store [4096 x i8] %data, [4096 x i8]* %buffer |
Andrew Trick | 778583a | 2011-03-11 17:46:59 +0000 | [diff] [blame] | 90 | static const unsigned MaxParallelChains = 64; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 91 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 92 | static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 93 | const SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 94 | MVT PartVT, EVT ValueVT, const Value *V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 95 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 96 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 97 | /// combined into the value they represent. If the parts combine to a type |
| 98 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 99 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 100 | /// (ISD::AssertSext). |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 101 | static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 102 | const SDValue *Parts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 103 | unsigned NumParts, MVT PartVT, EVT ValueVT, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 104 | const Value *V, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 105 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 106 | if (ValueVT.isVector()) |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 107 | return getCopyFromPartsVector(DAG, DL, Parts, NumParts, |
| 108 | PartVT, ValueVT, V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 109 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 110 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 111 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 112 | SDValue Val = Parts[0]; |
| 113 | |
| 114 | if (NumParts > 1) { |
| 115 | // Assemble the value from multiple parts. |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 116 | if (ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 117 | unsigned PartBits = PartVT.getSizeInBits(); |
| 118 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 119 | |
| 120 | // Assemble the power of 2 part. |
| 121 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 122 | 1 << Log2_32(NumParts) : NumParts; |
| 123 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 124 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 125 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 126 | SDValue Lo, Hi; |
| 127 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 128 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 129 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 130 | if (RoundParts > 2) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 131 | Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 132 | PartVT, HalfVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 133 | Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 134 | RoundParts / 2, PartVT, HalfVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 135 | } else { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 136 | Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]); |
| 137 | Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 138 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 139 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 140 | if (TLI.isBigEndian()) |
| 141 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 143 | Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 144 | |
| 145 | if (RoundParts < NumParts) { |
| 146 | // Assemble the trailing non-power-of-2 part. |
| 147 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 148 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 149 | Hi = getCopyFromParts(DAG, DL, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 150 | Parts + RoundParts, OddParts, PartVT, OddVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 151 | |
| 152 | // Combine the round and odd parts. |
| 153 | Lo = Val; |
| 154 | if (TLI.isBigEndian()) |
| 155 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 156 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 157 | Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi); |
| 158 | Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 159 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 160 | TLI.getPointerTy())); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 161 | Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo); |
| 162 | Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 163 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 164 | } else if (PartVT.isFloatingPoint()) { |
| 165 | // FP split into multiple FP parts (for ppcf128) |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 166 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 167 | "Unexpected split"); |
| 168 | SDValue Lo, Hi; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 169 | Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]); |
| 170 | Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 171 | if (TLI.isBigEndian()) |
| 172 | std::swap(Lo, Hi); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 173 | Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 174 | } else { |
| 175 | // FP split into integer parts (soft fp) |
| 176 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 177 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 178 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 179 | Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | // 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] | 184 | EVT PartEVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 185 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 186 | if (PartEVT == ValueVT) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 187 | return Val; |
| 188 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 189 | if (PartEVT.isInteger() && ValueVT.isInteger()) { |
| 190 | if (ValueVT.bitsLT(PartEVT)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 191 | // For a truncate, see if we have any information to |
| 192 | // indicate whether the truncated bits will always be |
| 193 | // zero or sign-extension. |
| 194 | if (AssertOp != ISD::DELETED_NODE) |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 195 | Val = DAG.getNode(AssertOp, DL, PartEVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 196 | DAG.getValueType(ValueVT)); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 197 | return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 198 | } |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 199 | return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 202 | if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 203 | // FP_ROUND's are always exact here. |
| 204 | if (ValueVT.bitsLT(Val.getValueType())) |
| 205 | return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, |
Pete Cooper | f57e1c2 | 2012-01-17 01:54:07 +0000 | [diff] [blame] | 206 | DAG.getTargetConstant(1, TLI.getPointerTy())); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 208 | return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 211 | if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits()) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 212 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 213 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 214 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 217 | /// getCopyFromPartsVector - Create a value that contains the specified legal |
| 218 | /// parts combined into the value they represent. If the parts combine to a |
| 219 | /// type larger then ValueVT then AssertOp can be used to specify whether the |
| 220 | /// extra bits are known to be zero (ISD::AssertZext) or sign extended from |
| 221 | /// ValueVT (ISD::AssertSext). |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 222 | static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 223 | const SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 224 | MVT PartVT, EVT ValueVT, const Value *V) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 225 | assert(ValueVT.isVector() && "Not a vector value"); |
| 226 | assert(NumParts > 0 && "No parts to assemble!"); |
| 227 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 228 | SDValue Val = Parts[0]; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 230 | // Handle a multi-element vector. |
| 231 | if (NumParts > 1) { |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 232 | EVT IntermediateVT; |
| 233 | MVT RegisterVT; |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 234 | unsigned NumIntermediates; |
| 235 | unsigned NumRegs = |
| 236 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
| 237 | NumIntermediates, RegisterVT); |
| 238 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 239 | NumParts = NumRegs; // Silence a compiler warning. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 240 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 241 | assert(RegisterVT == Parts[0].getSimpleValueType() && |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 242 | "Part type doesn't match part!"); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 243 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 244 | // Assemble the parts into intermediate operands. |
| 245 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 246 | if (NumIntermediates == NumParts) { |
| 247 | // If the register was not expanded, truncate or copy the value, |
| 248 | // as appropriate. |
| 249 | for (unsigned i = 0; i != NumParts; ++i) |
| 250 | Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 251 | PartVT, IntermediateVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 252 | } else if (NumParts > 0) { |
| 253 | // If the intermediate type was expanded, build the intermediate |
| 254 | // operands from the parts. |
| 255 | assert(NumParts % NumIntermediates == 0 && |
| 256 | "Must expand into a divisible number of parts!"); |
| 257 | unsigned Factor = NumParts / NumIntermediates; |
| 258 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 259 | Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 260 | PartVT, IntermediateVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 261 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 263 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 264 | // intermediate operands. |
| 265 | Val = DAG.getNode(IntermediateVT.isVector() ? |
| 266 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL, |
| 267 | ValueVT, &Ops[0], NumIntermediates); |
| 268 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 270 | // 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] | 271 | EVT PartEVT = Val.getValueType(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 272 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 273 | if (PartEVT == ValueVT) |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 274 | return Val; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 275 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 276 | if (PartEVT.isVector()) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 277 | // If the element type of the source/dest vectors are the same, but the |
| 278 | // parts vector has more elements than the value vector, then we have a |
| 279 | // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the |
| 280 | // elements we want. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 281 | if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) { |
| 282 | assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() && |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 283 | "Cannot narrow, it would be a lossy transformation"); |
| 284 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 285 | DAG.getConstant(0, TLI.getVectorIdxTy())); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 288 | // Vector/Vector bitcast. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 289 | if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 290 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
| 291 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 292 | assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() && |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 293 | "Cannot handle this kind of promotion"); |
| 294 | // Promoted vector extract |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 295 | bool Smaller = ValueVT.bitsLE(PartEVT); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 296 | return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 297 | DL, ValueVT, Val); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 298 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 299 | } |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 300 | |
Eric Christopher | 9aaa02a | 2011-06-01 19:55:10 +0000 | [diff] [blame] | 301 | // Trivial bitcast if the types are the same size and the destination |
| 302 | // vector type is legal. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 303 | if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() && |
Eric Christopher | 9aaa02a | 2011-06-01 19:55:10 +0000 | [diff] [blame] | 304 | TLI.isTypeLegal(ValueVT)) |
| 305 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 306 | |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 307 | // Handle cases such as i8 -> <1 x i1> |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 308 | if (ValueVT.getVectorNumElements() != 1) { |
| 309 | LLVMContext &Ctx = *DAG.getContext(); |
| 310 | Twine ErrMsg("non-trivial scalar-to-vector conversion"); |
| 311 | if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 312 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 313 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 314 | ErrMsg = ErrMsg + ", possible invalid constraint for vector type"; |
| 315 | Ctx.emitError(I, ErrMsg); |
| 316 | } else { |
| 317 | Ctx.emitError(ErrMsg); |
| 318 | } |
Chad Rosier | f0b0755 | 2013-05-01 19:49:26 +0000 | [diff] [blame] | 319 | return DAG.getUNDEF(ValueVT); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 320 | } |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 321 | |
| 322 | if (ValueVT.getVectorNumElements() == 1 && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 323 | ValueVT.getVectorElementType() != PartEVT) { |
| 324 | bool Smaller = ValueVT.bitsLE(PartEVT); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 325 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 326 | DL, ValueVT.getScalarType(), Val); |
| 327 | } |
| 328 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 329 | return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val); |
| 330 | } |
| 331 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 332 | static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 333 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 334 | MVT PartVT, const Value *V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 335 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 336 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 337 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 338 | /// 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] | 339 | static void getCopyToParts(SelectionDAG &DAG, SDLoc DL, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 340 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 341 | MVT PartVT, const Value *V, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 342 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 343 | EVT ValueVT = Val.getValueType(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 344 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 345 | // Handle the vector case separately. |
| 346 | if (ValueVT.isVector()) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 347 | return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 348 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 349 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 350 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 351 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 352 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 353 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 354 | if (NumParts == 0) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 355 | return; |
| 356 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 357 | assert(!ValueVT.isVector() && "Vector case handled elsewhere"); |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 358 | EVT PartEVT = PartVT; |
| 359 | if (PartEVT == ValueVT) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 360 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 361 | Parts[0] = Val; |
| 362 | return; |
| 363 | } |
| 364 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 365 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 366 | // If the parts cover more bits than the value has, promote the value. |
| 367 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 368 | assert(NumParts == 1 && "Do not know what to promote to!"); |
| 369 | Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val); |
| 370 | } else { |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 371 | assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && |
| 372 | ValueVT.isInteger() && |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 373 | "Unknown mismatch!"); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 374 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 375 | Val = DAG.getNode(ExtendKind, DL, ValueVT, Val); |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 376 | if (PartVT == MVT::x86mmx) |
| 377 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 378 | } |
| 379 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 380 | // Different types of the same size. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 381 | assert(NumParts == 1 && PartEVT != ValueVT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 382 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 383 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 384 | // If the parts cover less bits than value has, truncate the value. |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 385 | assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && |
| 386 | ValueVT.isInteger() && |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 387 | "Unknown mismatch!"); |
| 388 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 389 | Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 390 | if (PartVT == MVT::x86mmx) |
| 391 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // The value may have changed - recompute ValueVT. |
| 395 | ValueVT = Val.getValueType(); |
| 396 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 397 | "Failed to tile the value with PartVT!"); |
| 398 | |
| 399 | if (NumParts == 1) { |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 400 | if (PartEVT != ValueVT) { |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 401 | LLVMContext &Ctx = *DAG.getContext(); |
| 402 | Twine ErrMsg("scalar-to-vector conversion failed"); |
| 403 | if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 404 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 405 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 406 | ErrMsg = ErrMsg + ", possible invalid constraint for vector type"; |
| 407 | Ctx.emitError(I, ErrMsg); |
| 408 | } else { |
| 409 | Ctx.emitError(ErrMsg); |
| 410 | } |
| 411 | } |
| 412 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 413 | Parts[0] = Val; |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | // Expand the value into multiple parts. |
| 418 | if (NumParts & (NumParts - 1)) { |
| 419 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 420 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 421 | "Do not know what to expand to!"); |
| 422 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 423 | unsigned RoundBits = RoundParts * PartBits; |
| 424 | unsigned OddParts = NumParts - RoundParts; |
| 425 | SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val, |
| 426 | DAG.getIntPtrConstant(RoundBits)); |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 427 | getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 428 | |
| 429 | if (TLI.isBigEndian()) |
| 430 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 431 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 432 | |
| 433 | NumParts = RoundParts; |
| 434 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 435 | Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
| 436 | } |
| 437 | |
| 438 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 439 | // EXTRACT_ELEMENT. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 440 | Parts[0] = DAG.getNode(ISD::BITCAST, DL, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 441 | EVT::getIntegerVT(*DAG.getContext(), |
| 442 | ValueVT.getSizeInBits()), |
| 443 | Val); |
| 444 | |
| 445 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 446 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 447 | unsigned ThisBits = StepSize * PartBits / 2; |
| 448 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
| 449 | SDValue &Part0 = Parts[i]; |
| 450 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 451 | |
| 452 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, |
| 453 | ThisVT, Part0, DAG.getIntPtrConstant(1)); |
| 454 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, |
| 455 | ThisVT, Part0, DAG.getIntPtrConstant(0)); |
| 456 | |
| 457 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 458 | Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0); |
| 459 | Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (TLI.isBigEndian()) |
| 465 | std::reverse(Parts, Parts + OrigNumParts); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /// getCopyToPartsVector - Create a series of nodes that contain the specified |
| 470 | /// value split into legal parts. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 471 | static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 472 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 473 | MVT PartVT, const Value *V) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 474 | EVT ValueVT = Val.getValueType(); |
| 475 | assert(ValueVT.isVector() && "Not a vector"); |
| 476 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 477 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 478 | if (NumParts == 1) { |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 479 | EVT PartEVT = PartVT; |
| 480 | if (PartEVT == ValueVT) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 481 | // Nothing to do. |
| 482 | } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
| 483 | // Bitconvert vector->vector case. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 484 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 485 | } else if (PartVT.isVector() && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 486 | PartEVT.getVectorElementType() == ValueVT.getVectorElementType() && |
| 487 | PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 488 | EVT ElementVT = PartVT.getVectorElementType(); |
| 489 | // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in |
| 490 | // undef elements. |
| 491 | SmallVector<SDValue, 16> Ops; |
| 492 | for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i) |
| 493 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 494 | ElementVT, Val, DAG.getConstant(i, |
| 495 | TLI.getVectorIdxTy()))); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 496 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 497 | for (unsigned i = ValueVT.getVectorNumElements(), |
| 498 | e = PartVT.getVectorNumElements(); i != e; ++i) |
| 499 | Ops.push_back(DAG.getUNDEF(ElementVT)); |
| 500 | |
| 501 | Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size()); |
| 502 | |
| 503 | // FIXME: Use CONCAT for 2x -> 4x. |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 504 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 505 | //SDValue UndefElts = DAG.getUNDEF(VectorTy); |
| 506 | //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 507 | } else if (PartVT.isVector() && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 508 | PartEVT.getVectorElementType().bitsGE( |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 509 | ValueVT.getVectorElementType()) && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 510 | PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) { |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 511 | |
| 512 | // Promoted vector extract |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 513 | bool Smaller = PartEVT.bitsLE(ValueVT); |
Nadav Rotem | c6341e6 | 2011-06-19 08:49:38 +0000 | [diff] [blame] | 514 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 515 | DL, PartVT, Val); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 516 | } else{ |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 517 | // Vector -> scalar conversion. |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 518 | assert(ValueVT.getVectorNumElements() == 1 && |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 519 | "Only trivial vector-to-scalar conversions should get here!"); |
| 520 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 521 | PartVT, Val, DAG.getConstant(0, TLI.getVectorIdxTy())); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 522 | |
| 523 | bool Smaller = ValueVT.bitsLE(PartVT); |
| 524 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 525 | DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 526 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 527 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 528 | Parts[0] = Val; |
| 529 | return; |
| 530 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 531 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 532 | // Handle a multi-element vector. |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 533 | EVT IntermediateVT; |
| 534 | MVT RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 535 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 536 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
Devang Patel | 8f09bea | 2010-08-26 20:32:32 +0000 | [diff] [blame] | 537 | IntermediateVT, |
| 538 | NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 539 | unsigned NumElements = ValueVT.getVectorNumElements(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 540 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 541 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 542 | NumParts = NumRegs; // Silence a compiler warning. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 543 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 544 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 545 | // Split the vector into intermediate operands. |
| 546 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 547 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 548 | if (IntermediateVT.isVector()) |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 549 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 550 | IntermediateVT, Val, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 551 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 552 | TLI.getVectorIdxTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 553 | else |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 554 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 555 | IntermediateVT, Val, |
| 556 | DAG.getConstant(i, TLI.getVectorIdxTy())); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 557 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 558 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 559 | // Split the intermediate operands into legal parts. |
| 560 | if (NumParts == NumIntermediates) { |
| 561 | // If the register was not expanded, promote or copy the value, |
| 562 | // as appropriate. |
| 563 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 564 | getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 565 | } else if (NumParts > 0) { |
| 566 | // If the intermediate type was expanded, split each the value into |
| 567 | // legal parts. |
| 568 | assert(NumParts % NumIntermediates == 0 && |
| 569 | "Must expand into a divisible number of parts!"); |
| 570 | unsigned Factor = NumParts / NumIntermediates; |
| 571 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 572 | getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 576 | namespace { |
| 577 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 578 | /// that a particular set of values is assigned, and the type information |
| 579 | /// about the value. The most common situation is to represent one value at a |
| 580 | /// time, but struct or array values are handled element-wise as multiple |
| 581 | /// values. The splitting of aggregates is performed recursively, so that we |
| 582 | /// never have aggregate-typed registers. The values at this point do not |
| 583 | /// necessarily have legal types, so each value may require one or more |
| 584 | /// registers of some legal type. |
| 585 | /// |
| 586 | struct RegsForValue { |
| 587 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 588 | /// may need be promoted or synthesized from one or more registers. |
| 589 | /// |
| 590 | SmallVector<EVT, 4> ValueVTs; |
| 591 | |
| 592 | /// RegVTs - The value types of the registers. This is the same size as |
| 593 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 594 | /// register or registers are. (Individual values are never synthesized |
| 595 | /// from more than one type of register.) |
| 596 | /// |
| 597 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 598 | /// getRegisterType member function, however when with physical registers |
| 599 | /// it is necessary to have a separate record of the types. |
| 600 | /// |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 601 | SmallVector<MVT, 4> RegVTs; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 602 | |
| 603 | /// Regs - This list holds the registers assigned to the values. |
| 604 | /// Each legal or promoted value requires one register, and each |
| 605 | /// expanded value requires multiple registers. |
| 606 | /// |
| 607 | SmallVector<unsigned, 4> Regs; |
| 608 | |
| 609 | RegsForValue() {} |
| 610 | |
| 611 | RegsForValue(const SmallVector<unsigned, 4> ®s, |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 612 | MVT regvt, EVT valuevt) |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 613 | : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 614 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 615 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 616 | unsigned Reg, Type *Ty) { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 617 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 618 | |
| 619 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 620 | EVT ValueVT = ValueVTs[Value]; |
| 621 | unsigned NumRegs = tli.getNumRegisters(Context, ValueVT); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 622 | MVT RegisterVT = tli.getRegisterType(Context, ValueVT); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 623 | for (unsigned i = 0; i != NumRegs; ++i) |
| 624 | Regs.push_back(Reg + i); |
| 625 | RegVTs.push_back(RegisterVT); |
| 626 | Reg += NumRegs; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 631 | bool areValueTypesLegal(const TargetLowering &TLI) { |
| 632 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 633 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 634 | if (!TLI.isTypeLegal(RegisterVT)) |
| 635 | return false; |
| 636 | } |
| 637 | return true; |
| 638 | } |
| 639 | |
| 640 | /// append - Add the specified values to this one. |
| 641 | void append(const RegsForValue &RHS) { |
| 642 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 643 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 644 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 645 | } |
| 646 | |
| 647 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 648 | /// this value and returns the result as a ValueVTs value. This uses |
| 649 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 650 | /// If the Flag pointer is NULL, no flag is used. |
| 651 | SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 652 | SDLoc dl, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 653 | SDValue &Chain, SDValue *Flag, |
| 654 | const Value *V = 0) const; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 655 | |
| 656 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 657 | /// specified value into the registers specified by this object. This uses |
| 658 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 659 | /// If the Flag pointer is NULL, no flag is used. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 660 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 661 | SDValue &Chain, SDValue *Flag, const Value *V) const; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 662 | |
| 663 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 664 | /// operand list. This adds the code marker, matching input operand index |
| 665 | /// (if applicable), and includes the number of values added into it. |
| 666 | void AddInlineAsmOperands(unsigned Kind, |
| 667 | bool HasMatching, unsigned MatchingIdx, |
| 668 | SelectionDAG &DAG, |
| 669 | std::vector<SDValue> &Ops) const; |
| 670 | }; |
| 671 | } |
| 672 | |
| 673 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 674 | /// this value and returns the result as a ValueVT value. This uses |
| 675 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 676 | /// If the Flag pointer is NULL, no flag is used. |
| 677 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
| 678 | FunctionLoweringInfo &FuncInfo, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 679 | SDLoc dl, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 680 | SDValue &Chain, SDValue *Flag, |
| 681 | const Value *V) const { |
Dan Gohman | 7da5d3f | 2010-07-26 18:15:41 +0000 | [diff] [blame] | 682 | // A Value with type {} or [0 x %t] needs no registers. |
| 683 | if (ValueVTs.empty()) |
| 684 | return SDValue(); |
| 685 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 686 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 687 | |
| 688 | // Assemble the legal parts into the final values. |
| 689 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 690 | SmallVector<SDValue, 8> Parts; |
| 691 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 692 | // Copy the legal parts from the registers. |
| 693 | EVT ValueVT = ValueVTs[Value]; |
| 694 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 695 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 696 | |
| 697 | Parts.resize(NumRegs); |
| 698 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 699 | SDValue P; |
| 700 | if (Flag == 0) { |
| 701 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
| 702 | } else { |
| 703 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
| 704 | *Flag = P.getValue(2); |
| 705 | } |
| 706 | |
| 707 | Chain = P.getValue(1); |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 708 | Parts[i] = P; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 709 | |
| 710 | // If the source register was virtual and if we know something about it, |
| 711 | // add an assert node. |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 712 | if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) || |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 713 | !RegisterVT.isInteger() || RegisterVT.isVector()) |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 714 | continue; |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 715 | |
| 716 | const FunctionLoweringInfo::LiveOutInfo *LOI = |
| 717 | FuncInfo.GetLiveOutRegInfo(Regs[Part+i]); |
| 718 | if (!LOI) |
| 719 | continue; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 720 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 721 | unsigned RegSize = RegisterVT.getSizeInBits(); |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 722 | unsigned NumSignBits = LOI->NumSignBits; |
| 723 | unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes(); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 724 | |
Quentin Colombet | a3fb49c | 2013-06-18 20:14:39 +0000 | [diff] [blame] | 725 | if (NumZeroBits == RegSize) { |
| 726 | // The current value is a zero. |
| 727 | // Explicitly express that as it would be easier for |
| 728 | // optimizations to kick in. |
| 729 | Parts[i] = DAG.getConstant(0, RegisterVT); |
| 730 | continue; |
| 731 | } |
| 732 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 733 | // FIXME: We capture more information than the dag can represent. For |
| 734 | // now, just use the tightest assertzext/assertsext possible. |
| 735 | bool isSExt = true; |
| 736 | EVT FromVT(MVT::Other); |
| 737 | if (NumSignBits == RegSize) |
| 738 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 739 | else if (NumZeroBits >= RegSize-1) |
| 740 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 741 | else if (NumSignBits > RegSize-8) |
| 742 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
| 743 | else if (NumZeroBits >= RegSize-8) |
| 744 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 745 | else if (NumSignBits > RegSize-16) |
| 746 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
| 747 | else if (NumZeroBits >= RegSize-16) |
| 748 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
| 749 | else if (NumSignBits > RegSize-32) |
| 750 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
| 751 | else if (NumZeroBits >= RegSize-32) |
| 752 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
| 753 | else |
| 754 | continue; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 755 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 756 | // Add an assertion node. |
| 757 | assert(FromVT != MVT::Other); |
| 758 | Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
| 759 | RegisterVT, P, DAG.getValueType(FromVT)); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 763 | NumRegs, RegisterVT, ValueVT, V); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 764 | Part += NumRegs; |
| 765 | Parts.clear(); |
| 766 | } |
| 767 | |
| 768 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 769 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 770 | &Values[0], ValueVTs.size()); |
| 771 | } |
| 772 | |
| 773 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 774 | /// specified value into the registers specified by this object. This uses |
| 775 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 776 | /// If the Flag pointer is NULL, no flag is used. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 777 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 778 | SDValue &Chain, SDValue *Flag, |
| 779 | const Value *V) const { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 780 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 781 | |
| 782 | // Get the list of the values's legal parts. |
| 783 | unsigned NumRegs = Regs.size(); |
| 784 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 785 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 786 | EVT ValueVT = ValueVTs[Value]; |
| 787 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 788 | MVT RegisterVT = RegVTs[Value]; |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 789 | ISD::NodeType ExtendKind = |
| 790 | TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 791 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 792 | getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 793 | &Parts[Part], NumParts, RegisterVT, V, ExtendKind); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 794 | Part += NumParts; |
| 795 | } |
| 796 | |
| 797 | // Copy the parts into the registers. |
| 798 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 799 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 800 | SDValue Part; |
| 801 | if (Flag == 0) { |
| 802 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
| 803 | } else { |
| 804 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
| 805 | *Flag = Part.getValue(1); |
| 806 | } |
| 807 | |
| 808 | Chains[i] = Part.getValue(0); |
| 809 | } |
| 810 | |
| 811 | if (NumRegs == 1 || Flag) |
| 812 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
| 813 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 814 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 815 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 816 | // user as well as a successor (the TF operands are flagged to the user). |
| 817 | // c1, f1 = CopyToReg |
| 818 | // c2, f2 = CopyToReg |
| 819 | // c3 = TokenFactor c1, c2 |
| 820 | // ... |
| 821 | // = op c3, ..., f2 |
| 822 | Chain = Chains[NumRegs-1]; |
| 823 | else |
| 824 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
| 825 | } |
| 826 | |
| 827 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 828 | /// operand list. This adds the code marker and includes the number of |
| 829 | /// values added into it. |
| 830 | void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching, |
| 831 | unsigned MatchingIdx, |
| 832 | SelectionDAG &DAG, |
| 833 | std::vector<SDValue> &Ops) const { |
| 834 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 835 | |
| 836 | unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size()); |
| 837 | if (HasMatching) |
| 838 | Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx); |
Jakob Stoklund Olesen | 459b74b | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 839 | else if (!Regs.empty() && |
| 840 | TargetRegisterInfo::isVirtualRegister(Regs.front())) { |
| 841 | // Put the register class of the virtual registers in the flag word. That |
| 842 | // way, later passes can recompute register class constraints for inline |
| 843 | // assembly as well as normal instructions. |
| 844 | // Don't do this for tied operands that can use the regclass information |
| 845 | // from the def. |
| 846 | const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 847 | const TargetRegisterClass *RC = MRI.getRegClass(Regs.front()); |
| 848 | Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID()); |
| 849 | } |
| 850 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 851 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
| 852 | Ops.push_back(Res); |
| 853 | |
| 854 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 855 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 856 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 857 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 858 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
| 859 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
| 860 | } |
| 861 | } |
| 862 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 863 | |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 864 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa, |
| 865 | const TargetLibraryInfo *li) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 866 | AA = &aa; |
| 867 | GFI = gfi; |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 868 | LibInfo = li; |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 869 | TD = DAG.getTarget().getDataLayout(); |
Richard Smith | cb1f68d | 2012-08-22 00:42:39 +0000 | [diff] [blame] | 870 | Context = DAG.getContext(); |
Bill Wendling | 4ed1fb0 | 2011-10-15 01:00:26 +0000 | [diff] [blame] | 871 | LPadToCallSiteMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 874 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 875 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 876 | /// for a new block. This doesn't clear out information about |
| 877 | /// additional blocks that are needed to complete switch lowering |
| 878 | /// or PHI node updating; that information is cleared out as it is |
| 879 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 880 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 881 | NodeMap.clear(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 882 | UnusedArgNodeMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 883 | PendingLoads.clear(); |
| 884 | PendingExports.clear(); |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 885 | CurInst = NULL; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 886 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 889 | /// clearDanglingDebugInfo - Clear the dangling debug information |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 890 | /// map. This function is separated from the clear so that debug |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 891 | /// information that is dangling in a basic block can be properly |
| 892 | /// resolved in a different basic block. This allows the |
| 893 | /// SelectionDAG to resolve dangling debug information attached |
| 894 | /// to PHI nodes. |
| 895 | void SelectionDAGBuilder::clearDanglingDebugInfo() { |
| 896 | DanglingDebugInfoMap.clear(); |
| 897 | } |
| 898 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 899 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 900 | /// flushing any PendingLoad items. This must be done before emitting |
| 901 | /// a store or any other node that may need to be ordered after any |
| 902 | /// prior load instructions. |
| 903 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 904 | SDValue SelectionDAGBuilder::getRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 905 | if (PendingLoads.empty()) |
| 906 | return DAG.getRoot(); |
| 907 | |
| 908 | if (PendingLoads.size() == 1) { |
| 909 | SDValue Root = PendingLoads[0]; |
| 910 | DAG.setRoot(Root); |
| 911 | PendingLoads.clear(); |
| 912 | return Root; |
| 913 | } |
| 914 | |
| 915 | // Otherwise, we have to make a token factor node. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 916 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 917 | &PendingLoads[0], PendingLoads.size()); |
| 918 | PendingLoads.clear(); |
| 919 | DAG.setRoot(Root); |
| 920 | return Root; |
| 921 | } |
| 922 | |
| 923 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 924 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 925 | /// to do this before emitting a terminator instruction. |
| 926 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 927 | SDValue SelectionDAGBuilder::getControlRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 928 | SDValue Root = DAG.getRoot(); |
| 929 | |
| 930 | if (PendingExports.empty()) |
| 931 | return Root; |
| 932 | |
| 933 | // Turn all of the CopyToReg chains into one factored node. |
| 934 | if (Root.getOpcode() != ISD::EntryToken) { |
| 935 | unsigned i = 0, e = PendingExports.size(); |
| 936 | for (; i != e; ++i) { |
| 937 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 938 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 939 | break; // Don't add the root if we already indirectly depend on it. |
| 940 | } |
| 941 | |
| 942 | if (i == e) |
| 943 | PendingExports.push_back(Root); |
| 944 | } |
| 945 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 946 | Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 947 | &PendingExports[0], |
| 948 | PendingExports.size()); |
| 949 | PendingExports.clear(); |
| 950 | DAG.setRoot(Root); |
| 951 | return Root; |
| 952 | } |
| 953 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 954 | void SelectionDAGBuilder::visit(const Instruction &I) { |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 955 | // Set up outgoing PHI node register values before emitting the terminator. |
| 956 | if (isa<TerminatorInst>(&I)) |
| 957 | HandlePHINodesInSuccessorBlocks(I.getParent()); |
| 958 | |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 959 | ++SDNodeOrder; |
| 960 | |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 961 | CurInst = &I; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 962 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 963 | visit(I.getOpcode(), I); |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 964 | |
Dan Gohman | 92884f7 | 2010-04-20 15:03:56 +0000 | [diff] [blame] | 965 | if (!isa<TerminatorInst>(&I) && !HasTailCall) |
| 966 | CopyToExportRegsIfNeeded(&I); |
| 967 | |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 968 | CurInst = NULL; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 971 | void SelectionDAGBuilder::visitPHI(const PHINode &) { |
| 972 | llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!"); |
| 973 | } |
| 974 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 975 | void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 976 | // Note: this doesn't use InstVisitor, because it has to work with |
| 977 | // ConstantExpr's in addition to instructions. |
| 978 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 979 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 980 | // Build the switch statement using the Instruction.def file. |
| 981 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Galina Kistanova | 72ea0c9 | 2012-07-19 04:50:12 +0000 | [diff] [blame] | 982 | case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break; |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 983 | #include "llvm/IR/Instruction.def" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 984 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 985 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 986 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 987 | // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, |
| 988 | // generate the debug data structures now that we've seen its definition. |
| 989 | void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V, |
| 990 | SDValue Val) { |
| 991 | DanglingDebugInfo &DDI = DanglingDebugInfoMap[V]; |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 992 | if (DDI.getDI()) { |
| 993 | const DbgValueInst *DI = DDI.getDI(); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 994 | DebugLoc dl = DDI.getdl(); |
| 995 | unsigned DbgSDNodeOrder = DDI.getSDNodeOrder(); |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 996 | MDNode *Variable = DI->getVariable(); |
| 997 | uint64_t Offset = DI->getOffset(); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 998 | SDDbgValue *SDV; |
| 999 | if (Val.getNode()) { |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 1000 | if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1001 | SDV = DAG.getDbgValue(Variable, Val.getNode(), |
| 1002 | Val.getResNo(), Offset, dl, DbgSDNodeOrder); |
| 1003 | DAG.AddDbgValue(SDV, Val.getNode(), false); |
| 1004 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1005 | } else |
Adrian Prantl | 5da4e4f | 2013-05-22 18:02:19 +0000 | [diff] [blame] | 1006 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1007 | DanglingDebugInfoMap[V] = DanglingDebugInfo(); |
| 1008 | } |
| 1009 | } |
| 1010 | |
Nick Lewycky | 8de3400 | 2011-09-30 22:19:53 +0000 | [diff] [blame] | 1011 | /// getValue - Return an SDValue for the given Value. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1012 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1013 | // If we already have an SDValue for this value, use it. It's important |
| 1014 | // to do this first, so that we don't create a CopyFromReg if we already |
| 1015 | // have a regular SDValue. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1016 | SDValue &N = NodeMap[V]; |
| 1017 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1018 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1019 | // If there's a virtual register allocated and initialized for this |
| 1020 | // value, use it. |
| 1021 | DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V); |
| 1022 | if (It != FuncInfo.ValueMap.end()) { |
| 1023 | unsigned InReg = It->second; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1024 | RegsForValue RFV(*DAG.getContext(), *TM.getTargetLowering(), |
| 1025 | InReg, V->getType()); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1026 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1027 | N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V); |
Devang Patel | 8f31428 | 2011-01-25 18:09:58 +0000 | [diff] [blame] | 1028 | resolveDanglingDebugInfo(V, N); |
| 1029 | return N; |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1030 | } |
| 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 | |
| 1039 | /// getNonRegisterValue - Return an SDValue for the given Value, but |
| 1040 | /// don't look in FuncInfo.ValueMap for a virtual register. |
| 1041 | SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) { |
| 1042 | // If we already have an SDValue for this value, use it. |
| 1043 | SDValue &N = NodeMap[V]; |
| 1044 | if (N.getNode()) return N; |
| 1045 | |
| 1046 | // Otherwise create a new SDValue and remember it. |
| 1047 | SDValue Val = getValueImpl(V); |
| 1048 | NodeMap[V] = Val; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1049 | resolveDanglingDebugInfo(V, Val); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1050 | return Val; |
| 1051 | } |
| 1052 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1053 | /// getValueImpl - Helper function for getValue and getNonRegisterValue. |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1054 | /// Create an SDValue for the given value. |
| 1055 | SDValue SelectionDAGBuilder::getValueImpl(const Value *V) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1056 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1057 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1058 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1059 | EVT VT = TLI->getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1060 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1061 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1062 | return DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1063 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1064 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1065 | return DAG.getGlobalAddress(GV, getCurSDLoc(), VT); |
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<ConstantPointerNull>(C)) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1068 | return DAG.getConstant(0, TLI->getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1069 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1070 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1071 | return DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1072 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1073 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1074 | return DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1075 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1076 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1077 | visit(CE->getOpcode(), *CE); |
| 1078 | SDValue N1 = NodeMap[V]; |
Dan Gohman | ac7d05c | 2010-04-16 16:55:18 +0000 | [diff] [blame] | 1079 | assert(N1.getNode() && "visit didn't populate the NodeMap!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1080 | return N1; |
| 1081 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1082 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1083 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 1084 | SmallVector<SDValue, 4> Constants; |
| 1085 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 1086 | OI != OE; ++OI) { |
| 1087 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 1088 | // If the operand is an empty aggregate, there are no values. |
| 1089 | if (!Val) continue; |
| 1090 | // Add each leaf value from the operand to the Constants list |
| 1091 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1092 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 1093 | Constants.push_back(SDValue(Val, i)); |
| 1094 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1095 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1096 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1097 | getCurSDLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1098 | } |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1100 | if (const ConstantDataSequential *CDS = |
| 1101 | dyn_cast<ConstantDataSequential>(C)) { |
| 1102 | SmallVector<SDValue, 4> Ops; |
Chris Lattner | 0f193b8 | 2012-01-25 01:27:20 +0000 | [diff] [blame] | 1103 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1104 | SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode(); |
| 1105 | // Add each leaf value from the operand to the Constants list |
| 1106 | // to form a flattened list of all the values. |
| 1107 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 1108 | Ops.push_back(SDValue(Val, i)); |
| 1109 | } |
| 1110 | |
| 1111 | if (isa<ArrayType>(CDS->getType())) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1112 | return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc()); |
| 1113 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1114 | VT, &Ops[0], Ops.size()); |
| 1115 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1116 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1117 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1118 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 1119 | "Unknown struct or array constant!"); |
| 1120 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1121 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1122 | ComputeValueVTs(*TLI, C->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1123 | unsigned NumElts = ValueVTs.size(); |
| 1124 | if (NumElts == 0) |
| 1125 | return SDValue(); // empty struct |
| 1126 | SmallVector<SDValue, 4> Constants(NumElts); |
| 1127 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1128 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1129 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1130 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1131 | else if (EltVT.isFloatingPoint()) |
| 1132 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 1133 | else |
| 1134 | Constants[i] = DAG.getConstant(0, EltVT); |
| 1135 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1136 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1137 | return DAG.getMergeValues(&Constants[0], NumElts, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1138 | getCurSDLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1141 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1142 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1144 | VectorType *VecTy = cast<VectorType>(V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1145 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1146 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1147 | // Now that we know the number and type of the elements, get that number of |
| 1148 | // elements into the Ops array based on what kind of constant it is. |
| 1149 | SmallVector<SDValue, 16> Ops; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1150 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1151 | for (unsigned i = 0; i != NumElements; ++i) |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1152 | Ops.push_back(getValue(CV->getOperand(i))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1153 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1154 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1155 | EVT EltVT = TLI->getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1156 | |
| 1157 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1158 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1159 | Op = DAG.getConstantFP(0, EltVT); |
| 1160 | else |
| 1161 | Op = DAG.getConstant(0, EltVT); |
| 1162 | Ops.assign(NumElements, Op); |
| 1163 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1164 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1165 | // Create a BUILD_VECTOR node. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1166 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1167 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1168 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1169 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1170 | // If this is a static alloca, generate it as the frameindex instead of |
| 1171 | // computation. |
| 1172 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 1173 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 1174 | FuncInfo.StaticAllocaMap.find(AI); |
| 1175 | if (SI != FuncInfo.StaticAllocaMap.end()) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1176 | return DAG.getFrameIndex(SI->second, TLI->getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1177 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1178 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1179 | // If this is an instruction which fast-isel has deferred, select it now. |
| 1180 | if (const Instruction *Inst = dyn_cast<Instruction>(V)) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1181 | unsigned InReg = FuncInfo.InitializeRegForValue(Inst); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1182 | RegsForValue RFV(*DAG.getContext(), *TLI, InReg, Inst->getType()); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1183 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1184 | return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1185 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1186 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1187 | llvm_unreachable("Can't get register for value!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1190 | void SelectionDAGBuilder::visitRet(const ReturnInst &I) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1191 | const TargetLowering *TLI = TM.getTargetLowering(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1192 | SDValue Chain = getControlRoot(); |
| 1193 | SmallVector<ISD::OutputArg, 8> Outs; |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1194 | SmallVector<SDValue, 8> OutVals; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1195 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 1196 | if (!FuncInfo.CanLowerReturn) { |
| 1197 | unsigned DemoteReg = FuncInfo.DemoteRegister; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1198 | const Function *F = I.getParent()->getParent(); |
| 1199 | |
| 1200 | // Emit a store of the return value through the virtual register. |
| 1201 | // Leave Outs empty so that LowerReturn won't try to load return |
| 1202 | // registers the usual way. |
| 1203 | SmallVector<EVT, 1> PtrValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1204 | ComputeValueVTs(*TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1205 | PtrValueVTs); |
| 1206 | |
| 1207 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 1208 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1209 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1210 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1211 | SmallVector<uint64_t, 4> Offsets; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1212 | ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1213 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1214 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1215 | SmallVector<SDValue, 4> Chains(NumValues); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1216 | for (unsigned i = 0; i != NumValues; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1217 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 1218 | RetPtr.getValueType(), RetPtr, |
| 1219 | DAG.getIntPtrConstant(Offsets[i])); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1220 | Chains[i] = |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1221 | DAG.getStore(Chain, getCurSDLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1222 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 1223 | // FIXME: better loc info would be nice. |
| 1224 | Add, MachinePointerInfo(), false, false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1227 | Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1228 | MVT::Other, &Chains[0], NumValues); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 1229 | } else if (I.getNumOperands() != 0) { |
| 1230 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1231 | ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 1232 | unsigned NumValues = ValueVTs.size(); |
| 1233 | if (NumValues) { |
| 1234 | SDValue RetOp = getValue(I.getOperand(0)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1235 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 1236 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1237 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1238 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1239 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1240 | const Function *F = I.getParent()->getParent(); |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1241 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1242 | Attribute::SExt)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1243 | ExtendKind = ISD::SIGN_EXTEND; |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1244 | else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1245 | Attribute::ZExt)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1246 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1247 | |
Cameron Zwarich | 7bbf0ee | 2011-03-17 14:53:37 +0000 | [diff] [blame] | 1248 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1249 | VT = TLI->getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1250 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1251 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), VT); |
| 1252 | MVT PartVT = TLI->getRegisterType(*DAG.getContext(), VT); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1253 | SmallVector<SDValue, 4> Parts(NumParts); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1254 | getCopyToParts(DAG, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1255 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 1256 | &Parts[0], NumParts, PartVT, &I, ExtendKind); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1257 | |
| 1258 | // 'inreg' on function refers to return value |
| 1259 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1260 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1261 | Attribute::InReg)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1262 | Flags.setInReg(); |
| 1263 | |
| 1264 | // Propagate extension type if any |
Cameron Zwarich | 8df6bf5 | 2011-03-16 22:20:07 +0000 | [diff] [blame] | 1265 | if (ExtendKind == ISD::SIGN_EXTEND) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1266 | Flags.setSExt(); |
Cameron Zwarich | 8df6bf5 | 2011-03-16 22:20:07 +0000 | [diff] [blame] | 1267 | else if (ExtendKind == ISD::ZERO_EXTEND) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1268 | Flags.setZExt(); |
| 1269 | |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1270 | for (unsigned i = 0; i < NumParts; ++i) { |
| 1271 | Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(), |
Manman Ren | 0a1544d | 2012-11-01 23:49:58 +0000 | [diff] [blame] | 1272 | /*isfixed=*/true, 0, 0)); |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1273 | OutVals.push_back(Parts[i]); |
| 1274 | } |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1275 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1276 | } |
| 1277 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1278 | |
| 1279 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1280 | CallingConv::ID CallConv = |
| 1281 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1282 | Chain = TM.getTargetLowering()->LowerReturn(Chain, CallConv, isVarArg, |
| 1283 | Outs, OutVals, getCurSDLoc(), |
| 1284 | DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1287 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1288 | "LowerReturn didn't return a valid chain!"); |
| 1289 | |
| 1290 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1291 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1294 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1295 | /// created for it, emit nodes to copy the value into the virtual |
| 1296 | /// registers. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1297 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) { |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 1298 | // Skip empty types |
| 1299 | if (V->getType()->isEmptyTy()) |
| 1300 | return; |
| 1301 | |
Dan Gohman | 33b7a29 | 2010-04-16 17:15:02 +0000 | [diff] [blame] | 1302 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1303 | if (VMI != FuncInfo.ValueMap.end()) { |
| 1304 | assert(!V->use_empty() && "Unused value assigned virtual registers!"); |
| 1305 | CopyValueToVirtualRegister(V, VMI->second); |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1309 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1310 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1311 | /// CopyTo/FromReg. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1312 | void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1313 | // No need to export constants. |
| 1314 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1315 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1316 | // Already exported? |
| 1317 | if (FuncInfo.isExportedInst(V)) return; |
| 1318 | |
| 1319 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1320 | CopyValueToVirtualRegister(V, Reg); |
| 1321 | } |
| 1322 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1323 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1324 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1325 | // The operands of the setcc have to be in this block. We don't know |
| 1326 | // how to export them from some other block. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1327 | if (const Instruction *VI = dyn_cast<Instruction>(V)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1328 | // Can export from current BB. |
| 1329 | if (VI->getParent() == FromBB) |
| 1330 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1331 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1332 | // Is already exported, noop. |
| 1333 | return FuncInfo.isExportedInst(V); |
| 1334 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1335 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1336 | // If this is an argument, we can export it if the BB is the entry block or |
| 1337 | // if it is already exported. |
| 1338 | if (isa<Argument>(V)) { |
| 1339 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1340 | return true; |
| 1341 | |
| 1342 | // Otherwise, can only export this if it is already exported. |
| 1343 | return FuncInfo.isExportedInst(V); |
| 1344 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1345 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1346 | // Otherwise, constants can always be exported. |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1350 | /// Return branch probability calculated by BranchProbabilityInfo for IR blocks. |
Jakub Staszak | 25101bb | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 1351 | uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src, |
| 1352 | const MachineBasicBlock *Dst) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1353 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
| 1354 | if (!BPI) |
| 1355 | return 0; |
Jakub Staszak | 95ece8e | 2011-07-29 20:05:36 +0000 | [diff] [blame] | 1356 | const BasicBlock *SrcBB = Src->getBasicBlock(); |
| 1357 | const BasicBlock *DstBB = Dst->getBasicBlock(); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1358 | return BPI->getEdgeWeight(SrcBB, DstBB); |
| 1359 | } |
| 1360 | |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 1361 | void SelectionDAGBuilder:: |
| 1362 | addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst, |
| 1363 | uint32_t Weight /* = 0 */) { |
| 1364 | if (!Weight) |
| 1365 | Weight = getEdgeWeight(Src, Dst); |
| 1366 | Src->addSuccessor(Dst, Weight); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1370 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1371 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1372 | return I->getParent() == BB; |
| 1373 | return true; |
| 1374 | } |
| 1375 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1376 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1377 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1378 | /// AND operator tree. |
| 1379 | /// |
| 1380 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1381 | SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1382 | MachineBasicBlock *TBB, |
| 1383 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1384 | MachineBasicBlock *CurBB, |
| 1385 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1386 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1387 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1388 | // If the leaf of the tree is a comparison, merge the condition into |
| 1389 | // the caseblock. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1390 | if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1391 | // The operands of the cmp have to be in this block. We don't know |
| 1392 | // how to export them from some other block. If this is the first block |
| 1393 | // of the sequence, no exporting is needed. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1394 | if (CurBB == SwitchBB || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1395 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1396 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1397 | ISD::CondCode Condition; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1398 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1399 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1400 | } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1401 | Condition = getFCmpCondCode(FC->getPredicate()); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1402 | if (TM.Options.NoNaNsFPMath) |
| 1403 | Condition = getFCmpCodeWithoutNaN(Condition); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1404 | } else { |
| 1405 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1406 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1407 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1408 | |
| 1409 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1410 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1411 | SwitchCases.push_back(CB); |
| 1412 | return; |
| 1413 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1417 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1418 | NULL, TBB, FBB, CurBB); |
| 1419 | SwitchCases.push_back(CB); |
| 1420 | } |
| 1421 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1422 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1423 | void SelectionDAGBuilder::FindMergedConditions(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1424 | MachineBasicBlock *TBB, |
| 1425 | MachineBasicBlock *FBB, |
| 1426 | MachineBasicBlock *CurBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1427 | MachineBasicBlock *SwitchBB, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1428 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1429 | // 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] | 1430 | const Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1431 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1432 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1433 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1434 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1435 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1436 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1437 | return; |
| 1438 | } |
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 | // Create TmpBB after CurBB. |
| 1441 | MachineFunction::iterator BBI = CurBB; |
| 1442 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1443 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1444 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1445 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1446 | if (Opc == Instruction::Or) { |
| 1447 | // Codegen X | Y as: |
| 1448 | // jmp_if_X TBB |
| 1449 | // jmp TmpBB |
| 1450 | // TmpBB: |
| 1451 | // jmp_if_Y TBB |
| 1452 | // jmp FBB |
| 1453 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1454 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1455 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1456 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1457 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1458 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1459 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1460 | } else { |
| 1461 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1462 | // Codegen X & Y as: |
| 1463 | // jmp_if_X TmpBB |
| 1464 | // jmp FBB |
| 1465 | // TmpBB: |
| 1466 | // jmp_if_Y TBB |
| 1467 | // jmp FBB |
| 1468 | // |
| 1469 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1470 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1471 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1472 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1473 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1474 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1475 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1480 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1481 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1482 | bool |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 1483 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1484 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1485 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1486 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1487 | // will get folded into a single comparison, so don't emit two blocks. |
| 1488 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1489 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1490 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1491 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1492 | return false; |
| 1493 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1495 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1496 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1497 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1498 | Cases[0].CC == Cases[1].CC && |
| 1499 | isa<Constant>(Cases[0].CmpRHS) && |
| 1500 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1501 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1502 | return false; |
| 1503 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1504 | return false; |
| 1505 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 1506 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1507 | return true; |
| 1508 | } |
| 1509 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1510 | void SelectionDAGBuilder::visitBr(const BranchInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1511 | MachineBasicBlock *BrMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1512 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1513 | // Update machine-CFG edges. |
| 1514 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1515 | |
| 1516 | // Figure out which block is immediately after the current one. |
| 1517 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1518 | MachineFunction::iterator BBI = BrMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1519 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1520 | NextBlock = BBI; |
| 1521 | |
| 1522 | if (I.isUnconditional()) { |
| 1523 | // Update machine-CFG edges. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1524 | BrMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1525 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1526 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1527 | if (Succ0MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1528 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1529 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1530 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1531 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1532 | return; |
| 1533 | } |
| 1534 | |
| 1535 | // If this condition is one of the special cases we handle, do special stuff |
| 1536 | // now. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1537 | const Value *CondVal = I.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1538 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1539 | |
| 1540 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1541 | // 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] | 1542 | // As long as jumps are not expensive, this should improve performance. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1543 | // For example, instead of something like: |
| 1544 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1545 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1546 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1547 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1548 | // or C, F |
| 1549 | // jnz foo |
| 1550 | // Emit: |
| 1551 | // cmp A, B |
| 1552 | // je foo |
| 1553 | // cmp D, E |
| 1554 | // jle foo |
| 1555 | // |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1556 | if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1557 | if (!TM.getTargetLowering()->isJumpExpensive() && |
Chris Lattner | de189be | 2010-11-30 18:12:52 +0000 | [diff] [blame] | 1558 | BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1559 | (BOp->getOpcode() == Instruction::And || |
| 1560 | BOp->getOpcode() == Instruction::Or)) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1561 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, |
| 1562 | BOp->getOpcode()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1563 | // If the compares in later blocks need to use values not currently |
| 1564 | // exported from this block, export them now. This block should always |
| 1565 | // be the first entry. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1566 | assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!"); |
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 | // Allow some cases to be rejected. |
| 1569 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1570 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1571 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1572 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1573 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1574 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1575 | // Emit the branch for this block. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1576 | visitSwitchCase(SwitchCases[0], BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1577 | SwitchCases.erase(SwitchCases.begin()); |
| 1578 | return; |
| 1579 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1580 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1581 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1582 | // SwitchCases. |
| 1583 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1584 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1585 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1586 | SwitchCases.clear(); |
| 1587 | } |
| 1588 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1589 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1590 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1591 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1592 | NULL, Succ0MBB, Succ1MBB, BrMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1593 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1594 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1595 | // cond branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1596 | visitSwitchCase(CB, BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1600 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1601 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB, |
| 1602 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1603 | SDValue Cond; |
| 1604 | SDValue CondLHS = getValue(CB.CmpLHS); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1605 | SDLoc dl = getCurSDLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1606 | |
| 1607 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1608 | if (CB.CmpMHS == NULL) { |
| 1609 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1610 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1611 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1612 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1613 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1614 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1615 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1616 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1617 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1618 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1619 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1620 | } else { |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1621 | assert(CB.CC == ISD::SETCC_INVALID && |
| 1622 | "Condition is undefined for to-the-range belonging check."); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1623 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1624 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1625 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1626 | |
| 1627 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1628 | EVT VT = CmpOp.getValueType(); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 1629 | |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1630 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(false)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1631 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1632 | ISD::SETULE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1633 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1634 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1635 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1636 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1637 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1638 | } |
| 1639 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1640 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1641 | // Update successor info |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 1642 | addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight); |
Jakob Stoklund Olesen | e7fdef4 | 2012-08-20 21:39:52 +0000 | [diff] [blame] | 1643 | // TrueBB and FalseBB are always different unless the incoming IR is |
| 1644 | // degenerate. This only happens when running llc on weird IR. |
| 1645 | if (CB.TrueBB != CB.FalseBB) |
| 1646 | addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1647 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1648 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1649 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1650 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1651 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1652 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1653 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1654 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1655 | // If the lhs block is the next block, invert the condition so that we can |
| 1656 | // fall through to the lhs instead of the rhs block. |
| 1657 | if (CB.TrueBB == NextBlock) { |
| 1658 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1659 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1660 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1661 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1662 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1663 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1664 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1665 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1666 | |
Evan Cheng | 266a99d | 2010-09-23 06:51:55 +0000 | [diff] [blame] | 1667 | // Insert the false branch. Do this even if it's a fall through branch, |
| 1668 | // this makes it easier to do DAG optimizations which require inverting |
| 1669 | // the branch condition. |
| 1670 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1671 | DAG.getBasicBlock(CB.FalseBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1672 | |
| 1673 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1677 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1678 | // Emit the code for the jump table |
| 1679 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1680 | EVT PTy = TM.getTargetLowering()->getPointerTy(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1681 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1682 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1683 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1684 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(), |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1685 | MVT::Other, Index.getValue(1), |
| 1686 | Table, Index); |
| 1687 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1691 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1692 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1693 | JumpTableHeader &JTH, |
| 1694 | MachineBasicBlock *SwitchBB) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1695 | // Subtract the lowest switch case value from the value being switched on and |
| 1696 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1697 | // difference between smallest and largest cases. |
| 1698 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1699 | EVT VT = SwitchOp.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1700 | SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1701 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1702 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1703 | // 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] | 1704 | // 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] | 1705 | // can be used as an index into the jump table in a subsequent basic block. |
| 1706 | // This value may be smaller or larger than the target's pointer type, and |
| 1707 | // therefore require extension or truncating. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1708 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1709 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI->getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1710 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1711 | unsigned JumpTableReg = FuncInfo.CreateReg(TLI->getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1712 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1713 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1714 | JT.Reg = JumpTableReg; |
| 1715 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1716 | // Emit the range check for the jump table, and branch to the default block |
| 1717 | // for the switch statement if the value being switched on exceeds the largest |
| 1718 | // case in the switch. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1719 | SDValue CMP = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1720 | TLI->getSetCCResultType(*DAG.getContext(), |
| 1721 | Sub.getValueType()), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1722 | Sub, |
| 1723 | DAG.getConstant(JTH.Last - JTH.First,VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1724 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1725 | |
| 1726 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1727 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1728 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1729 | MachineFunction::iterator BBI = SwitchBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1730 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1731 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1732 | NextBlock = BBI; |
| 1733 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1734 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1735 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1736 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1737 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1738 | if (JT.MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1739 | BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond, |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1740 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1741 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1742 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1746 | /// suitable for "bit tests" |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1747 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B, |
| 1748 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1749 | // Subtract the minimum value |
| 1750 | SDValue SwitchOp = getValue(B.SValue); |
Patrik Hagglund | 34525f9 | 2012-12-11 11:14:33 +0000 | [diff] [blame] | 1751 | EVT VT = SwitchOp.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1752 | SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1753 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1754 | |
| 1755 | // Check range |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1756 | const TargetLowering *TLI = TM.getTargetLowering(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1757 | SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1758 | TLI->getSetCCResultType(*DAG.getContext(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1759 | Sub.getValueType()), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1760 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1761 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1762 | |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1763 | // Determine the type of the test operands. |
| 1764 | bool UsePtrType = false; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1765 | if (!TLI->isTypeLegal(VT)) |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1766 | UsePtrType = true; |
| 1767 | else { |
| 1768 | for (unsigned i = 0, e = B.Cases.size(); i != e; ++i) |
Eli Friedman | 5c75af6 | 2011-10-12 22:46:45 +0000 | [diff] [blame] | 1769 | if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) { |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1770 | // Switch table case range are encoded into series of masks. |
| 1771 | // Just use pointer type, it's guaranteed to fit. |
| 1772 | UsePtrType = true; |
| 1773 | break; |
| 1774 | } |
| 1775 | } |
| 1776 | if (UsePtrType) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1777 | VT = TLI->getPointerTy(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1778 | Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT); |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1779 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1780 | |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1781 | B.RegVT = VT.getSimpleVT(); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 1782 | B.Reg = FuncInfo.CreateReg(B.RegVT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1783 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1784 | B.Reg, Sub); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1785 | |
| 1786 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1787 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1788 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1789 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1790 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1791 | NextBlock = BBI; |
| 1792 | |
| 1793 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1794 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1795 | addSuccessorWithWeight(SwitchBB, B.Default); |
| 1796 | addSuccessorWithWeight(SwitchBB, MBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1797 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1798 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1799 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1800 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1801 | |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1802 | if (MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1803 | BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo, |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1804 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1805 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1806 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | /// visitBitTestCase - this function produces one "bit test" |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1810 | void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB, |
| 1811 | MachineBasicBlock* NextMBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1812 | uint32_t BranchWeightToNext, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1813 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1814 | BitTestCase &B, |
| 1815 | MachineBasicBlock *SwitchBB) { |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 1816 | MVT VT = BB.RegVT; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1817 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1818 | Reg, VT); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1819 | SDValue Cmp; |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1820 | unsigned PopCount = CountPopulation_64(B.Mask); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1821 | const TargetLowering *TLI = TM.getTargetLowering(); |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1822 | if (PopCount == 1) { |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1823 | // Testing for a single bit; just compare the shift count with what it |
| 1824 | // would need to be to shift a 1 bit in that position. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1825 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1826 | TLI->getSetCCResultType(*DAG.getContext(), VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1827 | ShiftOp, |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1828 | DAG.getConstant(countTrailingZeros(B.Mask), VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1829 | ISD::SETEQ); |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1830 | } else if (PopCount == BB.Range) { |
| 1831 | // 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] | 1832 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1833 | TLI->getSetCCResultType(*DAG.getContext(), VT), |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1834 | ShiftOp, |
| 1835 | DAG.getConstant(CountTrailingOnes_64(B.Mask), VT), |
| 1836 | ISD::SETNE); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1837 | } else { |
| 1838 | // Make desired shift |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1839 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT, |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1840 | DAG.getConstant(1, VT), ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1841 | |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1842 | // Emit bit tests and jumps |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1843 | SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1844 | VT, SwitchVal, DAG.getConstant(B.Mask, VT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1845 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1846 | TLI->getSetCCResultType(*DAG.getContext(), VT), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1847 | AndOp, DAG.getConstant(0, VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1848 | ISD::SETNE); |
| 1849 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1850 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1851 | // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight. |
| 1852 | addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight); |
| 1853 | // The branch weight from SwitchBB to NextMBB is BranchWeightToNext. |
| 1854 | addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1855 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1856 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1857 | MVT::Other, getControlRoot(), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1858 | Cmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1859 | |
| 1860 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1861 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1862 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1863 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1864 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1865 | NextBlock = BBI; |
| 1866 | |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1867 | if (NextMBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1868 | BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd, |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1869 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1870 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1871 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1874 | void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1875 | MachineBasicBlock *InvokeMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1876 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1877 | // Retrieve successors. |
| 1878 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1879 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1880 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1881 | const Value *Callee(I.getCalledValue()); |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1882 | const Function *Fn = dyn_cast<Function>(Callee); |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1883 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1884 | visitInlineAsm(&I); |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1885 | else if (Fn && Fn->isIntrinsic()) { |
| 1886 | assert(Fn->getIntrinsicID() == Intrinsic::donothing); |
Nuno Lopes | 4532bf6 | 2012-07-18 00:07:17 +0000 | [diff] [blame] | 1887 | // Ignore invokes to @llvm.donothing: jump directly to the next BB. |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1888 | } else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1889 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1890 | |
| 1891 | // If the value of the invoke is used outside of its defining block, make it |
| 1892 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1893 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1894 | |
| 1895 | // Update successor info |
Chandler Carruth | f264568 | 2011-11-22 11:37:46 +0000 | [diff] [blame] | 1896 | addSuccessorWithWeight(InvokeMBB, Return); |
| 1897 | addSuccessorWithWeight(InvokeMBB, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1898 | |
| 1899 | // Drop into normal successor. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1900 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1901 | MVT::Other, getControlRoot(), |
| 1902 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 1905 | void SelectionDAGBuilder::visitResume(const ResumeInst &RI) { |
| 1906 | llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!"); |
| 1907 | } |
| 1908 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1909 | void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) { |
| 1910 | assert(FuncInfo.MBB->isLandingPad() && |
| 1911 | "Call to landingpad not in landing pad!"); |
| 1912 | |
| 1913 | MachineBasicBlock *MBB = FuncInfo.MBB; |
| 1914 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 1915 | AddLandingPadInfo(LP, MMI, MBB); |
| 1916 | |
Bill Wendling | bdf9db6 | 2012-02-13 23:47:16 +0000 | [diff] [blame] | 1917 | // If there aren't registers to copy the values into (e.g., during SjLj |
| 1918 | // exceptions), then don't bother to create these DAG nodes. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1919 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1920 | if (TLI->getExceptionPointerRegister() == 0 && |
| 1921 | TLI->getExceptionSelectorRegister() == 0) |
Bill Wendling | bdf9db6 | 2012-02-13 23:47:16 +0000 | [diff] [blame] | 1922 | return; |
| 1923 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1924 | SmallVector<EVT, 2> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1925 | ComputeValueVTs(*TLI, LP.getType(), ValueVTs); |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 1926 | assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported"); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1927 | |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 1928 | // Get the two live-in registers as SDValues. The physregs have already been |
| 1929 | // copied into virtual registers. |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1930 | SDValue Ops[2]; |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 1931 | Ops[0] = DAG.getZExtOrTrunc( |
| 1932 | DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(), |
| 1933 | FuncInfo.ExceptionPointerVirtReg, TLI->getPointerTy()), |
| 1934 | getCurSDLoc(), ValueVTs[0]); |
| 1935 | Ops[1] = DAG.getZExtOrTrunc( |
| 1936 | DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(), |
| 1937 | FuncInfo.ExceptionSelectorVirtReg, TLI->getPointerTy()), |
| 1938 | getCurSDLoc(), ValueVTs[1]); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1939 | |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 1940 | // Merge into one. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1941 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1942 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 1943 | &Ops[0], 2); |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 1944 | setValue(&LP, Res); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1947 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1948 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1949 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1950 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1951 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1952 | MachineBasicBlock *Default, |
| 1953 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1954 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1955 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1956 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1957 | return false; |
| 1958 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1959 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1960 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1961 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Figure out which block is immediately after the current one. |
| 1964 | MachineBasicBlock *NextBlock = 0; |
| 1965 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1966 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1967 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1968 | NextBlock = BBI; |
| 1969 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1970 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1971 | // 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] | 1972 | // is the same as the other, but has one bit unset that the other has set, |
| 1973 | // use bit manipulation to do two compares at once. For example: |
| 1974 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1975 | // TODO: This could be extended to merge any 2 cases in switches with 3 cases. |
| 1976 | // TODO: Handle cases where CR.CaseBB != SwitchBB. |
| 1977 | if (Size == 2 && CR.CaseBB == SwitchBB) { |
| 1978 | Case &Small = *CR.Range.first; |
| 1979 | Case &Big = *(CR.Range.second-1); |
| 1980 | |
| 1981 | if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) { |
| 1982 | const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue(); |
| 1983 | const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue(); |
| 1984 | |
| 1985 | // Check that there is only one bit different. |
| 1986 | if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 && |
| 1987 | (SmallValue | BigValue) == BigValue) { |
| 1988 | // Isolate the common bit. |
| 1989 | APInt CommonBit = BigValue & ~SmallValue; |
| 1990 | assert((SmallValue | CommonBit) == BigValue && |
| 1991 | CommonBit.countPopulation() == 1 && "Not a common bit?"); |
| 1992 | |
| 1993 | SDValue CondLHS = getValue(SV); |
| 1994 | EVT VT = CondLHS.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1995 | SDLoc DL = getCurSDLoc(); |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1996 | |
| 1997 | SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS, |
| 1998 | DAG.getConstant(CommonBit, VT)); |
| 1999 | SDValue Cond = DAG.getSetCC(DL, MVT::i1, |
| 2000 | Or, DAG.getConstant(BigValue, VT), |
| 2001 | ISD::SETEQ); |
| 2002 | |
| 2003 | // Update successor info. |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2004 | // Both Small and Big will jump to Small.BB, so we sum up the weights. |
| 2005 | addSuccessorWithWeight(SwitchBB, Small.BB, |
| 2006 | Small.ExtraWeight + Big.ExtraWeight); |
| 2007 | addSuccessorWithWeight(SwitchBB, Default, |
| 2008 | // The default destination is the first successor in IR. |
| 2009 | BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0); |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 2010 | |
| 2011 | // Insert the true branch. |
| 2012 | SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other, |
| 2013 | getControlRoot(), Cond, |
| 2014 | DAG.getBasicBlock(Small.BB)); |
| 2015 | |
| 2016 | // Insert the false branch. |
| 2017 | BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond, |
| 2018 | DAG.getBasicBlock(Default)); |
| 2019 | |
| 2020 | DAG.setRoot(BrCond); |
| 2021 | return true; |
| 2022 | } |
| 2023 | } |
| 2024 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2025 | |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2026 | // 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] | 2027 | uint32_t UnhandledWeights = 0; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2028 | if (BPI) { |
| 2029 | 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] | 2030 | uint32_t IWeight = I->ExtraWeight; |
| 2031 | UnhandledWeights += IWeight; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2032 | for (CaseItr J = CR.Range.first; J < I; ++J) { |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2033 | uint32_t JWeight = J->ExtraWeight; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2034 | if (IWeight > JWeight) |
| 2035 | std::swap(*I, *J); |
| 2036 | } |
| 2037 | } |
| 2038 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2039 | // 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] | 2040 | Case &BackCase = *(CR.Range.second-1); |
Benjamin Kramer | 5db954d | 2012-05-26 21:19:12 +0000 | [diff] [blame] | 2041 | if (Size > 1 && |
| 2042 | NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2043 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 2044 | // branches in this order. See if rearranging a case value would help. |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2045 | // We start at the bottom as it's the case with the least weight. |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2046 | 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] | 2047 | if (I->BB == NextBlock) { |
| 2048 | std::swap(*I, BackCase); |
| 2049 | break; |
| 2050 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2051 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2052 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2053 | // Create a CaseBlock record representing a conditional branch to |
| 2054 | // the Case's target mbb if the value being switched on SV is equal |
| 2055 | // to C. |
| 2056 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 2057 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 2058 | MachineBasicBlock *FallThrough; |
| 2059 | if (I != E-1) { |
| 2060 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 2061 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2062 | |
| 2063 | // Put SV in a virtual register to make it available from the new blocks. |
| 2064 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2065 | } else { |
| 2066 | // If the last case doesn't match, go to the default block. |
| 2067 | FallThrough = Default; |
| 2068 | } |
| 2069 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2070 | const Value *RHS, *LHS, *MHS; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2071 | ISD::CondCode CC; |
| 2072 | if (I->High == I->Low) { |
| 2073 | // This is just small small case range :) containing exactly 1 case |
| 2074 | CC = ISD::SETEQ; |
| 2075 | LHS = SV; RHS = I->High; MHS = NULL; |
| 2076 | } else { |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2077 | CC = ISD::SETCC_INVALID; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2078 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 2079 | } |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2080 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2081 | // The false weight should be sum of all un-handled cases. |
| 2082 | UnhandledWeights -= I->ExtraWeight; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2083 | CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough, |
| 2084 | /* me */ CurBlock, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2085 | /* trueweight */ I->ExtraWeight, |
| 2086 | /* falseweight */ UnhandledWeights); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2087 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2088 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 2089 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 2090 | // vector to be later processed by SDISel, and insert the node's MBB |
| 2091 | // before the next MBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2092 | if (CurBlock == SwitchBB) |
| 2093 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2094 | else |
| 2095 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2096 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2097 | CurBlock = FallThrough; |
| 2098 | } |
| 2099 | |
| 2100 | return true; |
| 2101 | } |
| 2102 | |
| 2103 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 2104 | return TLI.supportJumpTables() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2105 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 2106 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2107 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2108 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2109 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2110 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2111 | APInt LastExt = Last.zext(BitWidth), FirstExt = First.zext(BitWidth); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2112 | return (LastExt - FirstExt + 1ULL); |
| 2113 | } |
| 2114 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2115 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Chris Lattner | c3ab388 | 2011-09-09 22:06:59 +0000 | [diff] [blame] | 2116 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR, |
| 2117 | CaseRecVector &WorkList, |
| 2118 | const Value *SV, |
| 2119 | MachineBasicBlock *Default, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2120 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2121 | Case& FrontCase = *CR.Range.first; |
| 2122 | Case& BackCase = *(CR.Range.second-1); |
| 2123 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2124 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2125 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2126 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2127 | APInt TSize(First.getBitWidth(), 0); |
Chris Lattner | c3ab388 | 2011-09-09 22:06:59 +0000 | [diff] [blame] | 2128 | 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] | 2129 | TSize += I->size(); |
| 2130 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2131 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2132 | if (!areJTsAllowed(*TLI) || TSize.ult(TLI->getMinimumJumpTableEntries())) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2133 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2134 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2135 | APInt Range = ComputeRange(First, Last); |
Jakob Stoklund Olesen | 7944391 | 2011-10-26 01:47:48 +0000 | [diff] [blame] | 2136 | // The density is TSize / Range. Require at least 40%. |
| 2137 | // It should not be possible for IntTSize to saturate for sane code, but make |
| 2138 | // sure we handle Range saturation correctly. |
| 2139 | uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10); |
| 2140 | uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10); |
| 2141 | if (IntTSize * 10 < IntRange * 4) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2142 | return false; |
| 2143 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2144 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2145 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
Jakob Stoklund Olesen | 7944391 | 2011-10-26 01:47:48 +0000 | [diff] [blame] | 2146 | << "Range: " << Range << ". Size: " << TSize << ".\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2147 | |
| 2148 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2149 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2150 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2151 | |
| 2152 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2153 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 2154 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2155 | |
| 2156 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2157 | |
| 2158 | // Create a new basic block to hold the code for loading the address |
| 2159 | // of the jump table, and jumping to it. Update successor information; |
| 2160 | // we will either branch to the default case for the switch, or the jump |
| 2161 | // table. |
| 2162 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2163 | CurMF->insert(BBI, JumpTableBB); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 2164 | |
| 2165 | addSuccessorWithWeight(CR.CaseBB, Default); |
| 2166 | addSuccessorWithWeight(CR.CaseBB, JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2167 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2168 | // Build a vector of destination BBs, corresponding to each target |
| 2169 | // of the jump table. If the value of the jump table slot corresponds to |
| 2170 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 2171 | // the default BB. |
| 2172 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2173 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2174 | 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] | 2175 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 2176 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2177 | |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2178 | if (Low.ule(TEI) && TEI.ule(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2179 | DestBBs.push_back(I->BB); |
| 2180 | if (TEI==High) |
| 2181 | ++I; |
| 2182 | } else { |
| 2183 | DestBBs.push_back(Default); |
| 2184 | } |
| 2185 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2186 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2187 | // Calculate weight for each unique destination in CR. |
| 2188 | DenseMap<MachineBasicBlock*, uint32_t> DestWeights; |
| 2189 | if (FuncInfo.BPI) |
| 2190 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 2191 | DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr = |
| 2192 | DestWeights.find(I->BB); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2193 | if (Itr != DestWeights.end()) |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2194 | Itr->second += I->ExtraWeight; |
| 2195 | else |
| 2196 | DestWeights[I->BB] = I->ExtraWeight; |
| 2197 | } |
| 2198 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2199 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2200 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 2201 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2202 | E = DestBBs.end(); I != E; ++I) { |
| 2203 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 2204 | SuccsHandled[(*I)->getNumber()] = true; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2205 | DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr = |
| 2206 | DestWeights.find(*I); |
| 2207 | addSuccessorWithWeight(JumpTableBB, *I, |
| 2208 | Itr != DestWeights.end() ? Itr->second : 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2209 | } |
| 2210 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2211 | |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 2212 | // Create a jump table index for this jump table. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2213 | unsigned JTEncoding = TLI->getJumpTableEncoding(); |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 2214 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 2215 | ->createJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2216 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2217 | // Set the jump table information so that we can codegen it as a second |
| 2218 | // MachineBasicBlock |
| 2219 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2220 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB)); |
| 2221 | if (CR.CaseBB == SwitchBB) |
| 2222 | visitJumpTableHeader(JT, JTH, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2223 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2224 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2225 | return true; |
| 2226 | } |
| 2227 | |
| 2228 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 2229 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2230 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 2231 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2232 | const Value* SV, |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2233 | MachineBasicBlock* Default, |
| 2234 | MachineBasicBlock* SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2235 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2236 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2237 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2238 | |
| 2239 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2240 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 2241 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2242 | |
| 2243 | Case& FrontCase = *CR.Range.first; |
| 2244 | Case& BackCase = *(CR.Range.second-1); |
| 2245 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2246 | |
| 2247 | // Size is the number of Cases represented by this range. |
| 2248 | unsigned Size = CR.Range.second - CR.Range.first; |
| 2249 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2250 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2251 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2252 | double FMetric = 0; |
| 2253 | CaseItr Pivot = CR.Range.first + Size/2; |
| 2254 | |
| 2255 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 2256 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2257 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2258 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2259 | I!=E; ++I) |
| 2260 | TSize += I->size(); |
| 2261 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2262 | APInt LSize = FrontCase.size(); |
| 2263 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2264 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2265 | << "First: " << First << ", Last: " << Last <<'\n' |
| 2266 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2267 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 2268 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2269 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 2270 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2271 | APInt Range = ComputeRange(LEnd, RBegin); |
Stepan Dyatkovskiy | c2c52a6 | 2012-05-15 06:50:18 +0000 | [diff] [blame] | 2272 | assert((Range - 2ULL).isNonNegative() && |
| 2273 | "Invalid case distance"); |
Chris Lattner | c3e4e59 | 2011-04-09 06:57:13 +0000 | [diff] [blame] | 2274 | // Use volatile double here to avoid excess precision issues on some hosts, |
| 2275 | // e.g. that use 80-bit X87 registers. |
| 2276 | volatile double LDensity = |
| 2277 | (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2278 | (LEnd - First + 1ULL).roundToDouble(); |
Chris Lattner | c3e4e59 | 2011-04-09 06:57:13 +0000 | [diff] [blame] | 2279 | volatile double RDensity = |
| 2280 | (double)RSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2281 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2282 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2283 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2284 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2285 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 2286 | << "LDensity: " << LDensity |
| 2287 | << ", RDensity: " << RDensity << '\n' |
| 2288 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2289 | if (FMetric < Metric) { |
| 2290 | Pivot = J; |
| 2291 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2292 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | LSize += J->size(); |
| 2296 | RSize -= J->size(); |
| 2297 | } |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2298 | |
| 2299 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2300 | if (areJTsAllowed(*TLI)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2301 | // If our case is dense we *really* should handle it earlier! |
| 2302 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 2303 | } else { |
| 2304 | Pivot = CR.Range.first + Size/2; |
| 2305 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2306 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2307 | CaseRange LHSR(CR.Range.first, Pivot); |
| 2308 | CaseRange RHSR(Pivot, CR.Range.second); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 2309 | const Constant *C = Pivot->Low; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2310 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2311 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2312 | // 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] | 2313 | // 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] | 2314 | // 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] | 2315 | // 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] | 2316 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 2317 | // rather than creating a leaf node for it. |
| 2318 | if ((LHSR.second - LHSR.first) == 1 && |
| 2319 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2320 | cast<ConstantInt>(C)->getValue() == |
| 2321 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2322 | TrueBB = LHSR.first->BB; |
| 2323 | } else { |
| 2324 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2325 | CurMF->insert(BBI, TrueBB); |
| 2326 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2327 | |
| 2328 | // Put SV in a virtual register to make it available from the new blocks. |
| 2329 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2330 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2331 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2332 | // Similar to the optimization above, if the Value being switched on is |
| 2333 | // known to be less than the Constant CR.LT, and the current Case Value |
| 2334 | // is CR.LT - 1, then we can branch directly to the target block for |
| 2335 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 2336 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2337 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 2338 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2339 | FalseBB = RHSR.first->BB; |
| 2340 | } else { |
| 2341 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2342 | CurMF->insert(BBI, FalseBB); |
| 2343 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2344 | |
| 2345 | // Put SV in a virtual register to make it available from the new blocks. |
| 2346 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2350 | // 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] | 2351 | // Otherwise, branch to LHS. |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2352 | CaseBlock CB(ISD::SETULT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2353 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2354 | if (CR.CaseBB == SwitchBB) |
| 2355 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2356 | else |
| 2357 | SwitchCases.push_back(CB); |
| 2358 | |
| 2359 | return true; |
| 2360 | } |
| 2361 | |
| 2362 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 2363 | /// range span less, than machine word bitwidth, encode case range into series |
| 2364 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2365 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 2366 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2367 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2368 | MachineBasicBlock* Default, |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2369 | MachineBasicBlock* SwitchBB) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2370 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2371 | EVT PTy = TLI->getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2372 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2373 | |
| 2374 | Case& FrontCase = *CR.Range.first; |
| 2375 | Case& BackCase = *(CR.Range.second-1); |
| 2376 | |
| 2377 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2378 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2379 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2380 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 2381 | // If target does not have legal shift left, do not emit bit tests at all. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2382 | if (!TLI->isOperationLegal(ISD::SHL, TLI->getPointerTy())) |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 2383 | return false; |
| 2384 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2385 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2386 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2387 | I!=E; ++I) { |
| 2388 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2389 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2390 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2391 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2392 | // Count unique destinations |
| 2393 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 2394 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2395 | Dests.insert(I->BB); |
| 2396 | if (Dests.size() > 3) |
| 2397 | // Don't bother the code below, if there are too much unique destinations |
| 2398 | return false; |
| 2399 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2400 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2401 | << Dests.size() << '\n' |
| 2402 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2403 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2404 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2405 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2406 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2407 | APInt cmpRange = maxValue - minValue; |
| 2408 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2409 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2410 | << "Low bound: " << minValue << '\n' |
| 2411 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2412 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 2413 | if (cmpRange.uge(IntPtrBits) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2414 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 2415 | !(Dests.size() == 2 && numCmps >= 5) && |
| 2416 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 2417 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2418 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2419 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2420 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 2421 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2422 | // Optimize the case where all the case values fit in a |
| 2423 | // word without having to subtract minValue. In this case, |
| 2424 | // we can optimize away the subtraction. |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2425 | if (maxValue.ult(IntPtrBits)) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2426 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2427 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2428 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2429 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2430 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2431 | CaseBitsVector CasesBits; |
| 2432 | unsigned i, count = 0; |
| 2433 | |
| 2434 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2435 | MachineBasicBlock* Dest = I->BB; |
| 2436 | for (i = 0; i < count; ++i) |
| 2437 | if (Dest == CasesBits[i].BB) |
| 2438 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2439 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2440 | if (i == count) { |
| 2441 | assert((count < 3) && "Too much destinations to test!"); |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2442 | CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2443 | count++; |
| 2444 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2445 | |
| 2446 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 2447 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 2448 | |
| 2449 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 2450 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2451 | CasesBits[i].ExtraWeight += I->ExtraWeight; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2452 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2453 | for (uint64_t j = lo; j <= hi; j++) { |
| 2454 | CasesBits[i].Mask |= 1ULL << j; |
| 2455 | CasesBits[i].Bits++; |
| 2456 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2457 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2458 | } |
| 2459 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2460 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2461 | BitTestInfo BTC; |
| 2462 | |
| 2463 | // Figure out which block is immediately after the current one. |
| 2464 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2465 | ++BBI; |
| 2466 | |
| 2467 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2468 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2469 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2470 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2471 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2472 | << ", Bits: " << CasesBits[i].Bits |
| 2473 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2474 | |
| 2475 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2476 | CurMF->insert(BBI, CaseBB); |
| 2477 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2478 | CaseBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2479 | CasesBits[i].BB, CasesBits[i].ExtraWeight)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2480 | |
| 2481 | // Put SV in a virtual register to make it available from the new blocks. |
| 2482 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2483 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2484 | |
| 2485 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 2486 | -1U, MVT::Other, (CR.CaseBB == SwitchBB), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2487 | CR.CaseBB, Default, BTC); |
| 2488 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2489 | if (CR.CaseBB == SwitchBB) |
| 2490 | visitBitTestHeader(BTB, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2491 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2492 | BitTestCases.push_back(BTB); |
| 2493 | |
| 2494 | return true; |
| 2495 | } |
| 2496 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2497 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2498 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 2499 | const SwitchInst& SI) { |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2500 | |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2501 | /// Use a shorter form of declaration, and also |
| 2502 | /// show the we want to use CRSBuilder as Clusterifier. |
Stepan Dyatkovskiy | 4319a55 | 2012-06-02 07:26:00 +0000 | [diff] [blame] | 2503 | typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier; |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2504 | |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2505 | Clusterifier TheClusterifier; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2506 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2507 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2508 | // Start with "simple" cases |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 2509 | for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 2510 | i != e; ++i) { |
| 2511 | const BasicBlock *SuccBB = i.getCaseSuccessor(); |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2512 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB]; |
| 2513 | |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2514 | TheClusterifier.add(i.getCaseValueEx(), SMBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2515 | BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2516 | } |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2517 | |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2518 | TheClusterifier.optimize(); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2519 | |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2520 | size_t numCmps = 0; |
| 2521 | for (Clusterifier::RangeIterator i = TheClusterifier.begin(), |
| 2522 | e = TheClusterifier.end(); i != e; ++i, ++numCmps) { |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2523 | Clusterifier::Cluster &C = *i; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2524 | // Update edge weight for the cluster. |
| 2525 | unsigned W = C.first.Weight; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2526 | |
Stepan Dyatkovskiy | 484fc93 | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 2527 | // FIXME: Currently work with ConstantInt based numbers. |
| 2528 | // Changing it to APInt based is a pretty heavy for this commit. |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2529 | Cases.push_back(Case(C.first.getLow().toConstantInt(), |
| 2530 | C.first.getHigh().toConstantInt(), C.second, W)); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2531 | |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2532 | if (C.first.getLow() != C.first.getHigh()) |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2533 | // A range counts double, since it requires two compares. |
| 2534 | ++numCmps; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | return numCmps; |
| 2538 | } |
| 2539 | |
Jakob Stoklund Olesen | 2622f46 | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 2540 | void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First, |
| 2541 | MachineBasicBlock *Last) { |
| 2542 | // Update JTCases. |
| 2543 | for (unsigned i = 0, e = JTCases.size(); i != e; ++i) |
| 2544 | if (JTCases[i].first.HeaderBB == First) |
| 2545 | JTCases[i].first.HeaderBB = Last; |
| 2546 | |
| 2547 | // Update BitTestCases. |
| 2548 | for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i) |
| 2549 | if (BitTestCases[i].Parent == First) |
| 2550 | BitTestCases[i].Parent = Last; |
| 2551 | } |
| 2552 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2553 | void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 2554 | MachineBasicBlock *SwitchMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2555 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2556 | // Figure out which block is immediately after the current one. |
| 2557 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2558 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2559 | |
| 2560 | // If there is only the default destination, branch to it if it is not the |
| 2561 | // next basic block. Otherwise, just fall through. |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 2562 | if (!SI.getNumCases()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2563 | // Update machine-CFG edges. |
| 2564 | |
| 2565 | // If this is not a fall-through branch, emit the branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2566 | SwitchMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2567 | if (Default != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2568 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2569 | MVT::Other, getControlRoot(), |
| 2570 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2571 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2572 | return; |
| 2573 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2574 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2575 | // If there are any non-default case statements, create a vector of Cases |
| 2576 | // representing each one, and sort the vector so that we can efficiently |
| 2577 | // create a binary search tree from them. |
| 2578 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2579 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2580 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2581 | << ". Total compares: " << numCmps << '\n'); |
Duncan Sands | 17001ce | 2011-10-18 12:44:00 +0000 | [diff] [blame] | 2582 | (void)numCmps; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2583 | |
| 2584 | // Get the Value to be switched on and default basic blocks, which will be |
| 2585 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2586 | // search tree. |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 2587 | const Value *SV = SI.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2588 | |
| 2589 | // Push the initial CaseRec onto the worklist |
| 2590 | CaseRecVector WorkList; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2591 | WorkList.push_back(CaseRec(SwitchMBB,0,0, |
| 2592 | CaseRange(Cases.begin(),Cases.end()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2593 | |
| 2594 | while (!WorkList.empty()) { |
| 2595 | // Grab a record representing a case range to process off the worklist |
| 2596 | CaseRec CR = WorkList.back(); |
| 2597 | WorkList.pop_back(); |
| 2598 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2599 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2600 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2601 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2602 | // If the range has few cases (two or less) emit a series of specific |
| 2603 | // tests. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2604 | if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2605 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2606 | |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2607 | // 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] | 2608 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2609 | // lowering the switch to a binary tree of conditional branches. |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2610 | // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries(). |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2611 | if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2612 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2613 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2614 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2615 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2616 | handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2617 | } |
| 2618 | } |
| 2619 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2620 | void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 2621 | MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2622 | |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2623 | // Update machine-CFG edges with unique successors. |
Nadav Rotem | ee0ce15 | 2012-10-23 21:05:33 +0000 | [diff] [blame] | 2624 | SmallSet<BasicBlock*, 32> Done; |
| 2625 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) { |
| 2626 | BasicBlock *BB = I.getSuccessor(i); |
| 2627 | bool Inserted = Done.insert(BB); |
| 2628 | if (!Inserted) |
| 2629 | continue; |
| 2630 | |
| 2631 | MachineBasicBlock *Succ = FuncInfo.MBBMap[BB]; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 2632 | addSuccessorWithWeight(IndirectBrMBB, Succ); |
| 2633 | } |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2634 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2635 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2636 | MVT::Other, getControlRoot(), |
| 2637 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2638 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2639 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2640 | void SelectionDAGBuilder::visitFSub(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2641 | // -0.0 - X --> fneg |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2642 | Type *Ty = I.getType(); |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2643 | if (isa<Constant>(I.getOperand(0)) && |
| 2644 | I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) { |
| 2645 | SDValue Op2 = getValue(I.getOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2646 | setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(), |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2647 | Op2.getValueType(), Op2)); |
| 2648 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2649 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2650 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2651 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2654 | void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2655 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2656 | SDValue Op2 = getValue(I.getOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2657 | setValue(&I, DAG.getNode(OpCode, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2658 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2661 | void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2662 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2663 | SDValue Op2 = getValue(I.getOperand(1)); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2664 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2665 | EVT ShiftTy = TM.getTargetLowering()->getShiftAmountTy(Op2.getValueType()); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2666 | |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2667 | // Coerce the shift amount to the right type if we can. |
| 2668 | if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) { |
Chris Lattner | 915eeb4 | 2011-02-13 09:10:56 +0000 | [diff] [blame] | 2669 | unsigned ShiftSize = ShiftTy.getSizeInBits(); |
| 2670 | unsigned Op2Size = Op2.getValueType().getSizeInBits(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2671 | SDLoc DL = getCurSDLoc(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2672 | |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2673 | // If the operand is smaller than the shift count type, promote it. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2674 | if (ShiftSize > Op2Size) |
| 2675 | Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2676 | |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2677 | // If the operand is larger than the shift count type but the shift |
| 2678 | // count type has enough bits to represent any shift value, truncate |
| 2679 | // it now. This is a common case and it exposes the truncate to |
| 2680 | // optimization early. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2681 | else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2682 | Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2); |
| 2683 | // Otherwise we'll need to temporarily settle for some other convenient |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 2684 | // type. Type legalization will make adjustments once the shiftee is split. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2685 | else |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 2686 | Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2687 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2688 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2689 | setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2690 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2693 | void SelectionDAGBuilder::visitSDiv(const User &I) { |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2694 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2695 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2696 | |
| 2697 | // Turn exact SDivs into multiplications. |
| 2698 | // FIXME: This should be in DAGCombiner, but it doesn't have access to the |
| 2699 | // exact bit. |
Benjamin Kramer | 3492a4a | 2011-07-08 12:08:24 +0000 | [diff] [blame] | 2700 | if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() && |
| 2701 | !isa<ConstantSDNode>(Op1) && |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2702 | isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue()) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2703 | setValue(&I, TM.getTargetLowering()->BuildExactSDIV(Op1, Op2, |
| 2704 | getCurSDLoc(), DAG)); |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2705 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2706 | setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2707 | Op1, Op2)); |
| 2708 | } |
| 2709 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2710 | void SelectionDAGBuilder::visitICmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2711 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2712 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2713 | predicate = IC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2714 | else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2715 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2716 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2717 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2718 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2719 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2720 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2721 | setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2724 | void SelectionDAGBuilder::visitFCmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2725 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2726 | if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2727 | predicate = FC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2728 | else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2729 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2730 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2731 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2732 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2733 | if (TM.Options.NoNaNsFPMath) |
| 2734 | Condition = getFCmpCodeWithoutNaN(Condition); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2735 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2736 | setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2739 | void SelectionDAGBuilder::visitSelect(const User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2740 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2741 | ComputeValueVTs(*TM.getTargetLowering(), I.getType(), ValueVTs); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2742 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2743 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2744 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2745 | SmallVector<SDValue, 4> Values(NumValues); |
| 2746 | SDValue Cond = getValue(I.getOperand(0)); |
| 2747 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2748 | SDValue FalseVal = getValue(I.getOperand(2)); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 2749 | ISD::NodeType OpCode = Cond.getValueType().isVector() ? |
| 2750 | ISD::VSELECT : ISD::SELECT; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2751 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2752 | for (unsigned i = 0; i != NumValues; ++i) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2753 | Values[i] = DAG.getNode(OpCode, getCurSDLoc(), |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 2754 | TrueVal.getNode()->getValueType(TrueVal.getResNo()+i), |
Chris Lattner | b3e87b2 | 2010-03-12 07:15:36 +0000 | [diff] [blame] | 2755 | Cond, |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2756 | SDValue(TrueVal.getNode(), |
| 2757 | TrueVal.getResNo() + i), |
| 2758 | SDValue(FalseVal.getNode(), |
| 2759 | FalseVal.getResNo() + i)); |
| 2760 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2761 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2762 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2763 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2764 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2765 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2766 | void SelectionDAGBuilder::visitTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2767 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2768 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2769 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2770 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2773 | void SelectionDAGBuilder::visitZExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2774 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2775 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2776 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2777 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2778 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2781 | void SelectionDAGBuilder::visitSExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2782 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2783 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2784 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2785 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2786 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2789 | void SelectionDAGBuilder::visitFPTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2790 | // FPTrunc is never a no-op cast, no need to check |
| 2791 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2792 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2793 | EVT DestVT = TLI->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2794 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(), |
Pete Cooper | f57e1c2 | 2012-01-17 01:54:07 +0000 | [diff] [blame] | 2795 | DestVT, N, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2796 | DAG.getTargetConstant(0, TLI->getPointerTy()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2799 | void SelectionDAGBuilder::visitFPExt(const User &I) { |
Hal Finkel | 46bb70c | 2011-10-18 03:51:57 +0000 | [diff] [blame] | 2800 | // FPExt is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2801 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2802 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2803 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2806 | void SelectionDAGBuilder::visitFPToUI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2807 | // FPToUI is never a no-op cast, no need to check |
| 2808 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2809 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2810 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2813 | void SelectionDAGBuilder::visitFPToSI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2814 | // FPToSI is never a no-op cast, no need to check |
| 2815 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2816 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2817 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2820 | void SelectionDAGBuilder::visitUIToFP(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2821 | // UIToFP is never a no-op cast, no need to check |
| 2822 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2823 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2824 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2827 | void SelectionDAGBuilder::visitSIToFP(const User &I) { |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2828 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2829 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2830 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2831 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2834 | void SelectionDAGBuilder::visitPtrToInt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2835 | // What to do depends on the size of the integer and the size of the pointer. |
| 2836 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2837 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2838 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2839 | setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2842 | void SelectionDAGBuilder::visitIntToPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2843 | // What to do depends on the size of the integer and the size of the pointer. |
| 2844 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2845 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2846 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2847 | setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2850 | void SelectionDAGBuilder::visitBitCast(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2851 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2852 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2853 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2854 | // 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] | 2855 | // either a BITCAST or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2856 | if (DestVT != N.getValueType()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2857 | setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2858 | DestVT, N)); // convert types. |
| 2859 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2860 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2863 | void SelectionDAGBuilder::visitInsertElement(const User &I) { |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 2864 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2865 | SDValue InVec = getValue(I.getOperand(0)); |
| 2866 | SDValue InVal = getValue(I.getOperand(1)); |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 2867 | SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)), |
| 2868 | getCurSDLoc(), TLI.getVectorIdxTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2869 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2870 | TM.getTargetLowering()->getValueType(I.getType()), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2871 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2874 | void SelectionDAGBuilder::visitExtractElement(const User &I) { |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 2875 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2876 | SDValue InVec = getValue(I.getOperand(0)); |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 2877 | SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), |
| 2878 | getCurSDLoc(), TLI.getVectorIdxTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2879 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2880 | TM.getTargetLowering()->getValueType(I.getType()), |
| 2881 | InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2884 | // Utility for visitShuffleVector - Return true if every element in Mask, |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 2885 | // beginning from position Pos and ending in Pos+Size, falls within the |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2886 | // specified sequential range [L, L+Pos). or is undef. |
| 2887 | static bool isSequentialInRange(const SmallVectorImpl<int> &Mask, |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 2888 | unsigned Pos, unsigned Size, int Low) { |
| 2889 | for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low) |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2890 | if (Mask[i] >= 0 && Mask[i] != Low) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2891 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2892 | return true; |
| 2893 | } |
| 2894 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2895 | void SelectionDAGBuilder::visitShuffleVector(const User &I) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2896 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2897 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2898 | |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 2899 | SmallVector<int, 8> Mask; |
| 2900 | ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask); |
| 2901 | unsigned MaskNumElts = Mask.size(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2902 | |
| 2903 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2904 | EVT VT = TLI->getValueType(I.getType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2905 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2906 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2907 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2908 | if (SrcNumElts == MaskNumElts) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2909 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2910 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2911 | return; |
| 2912 | } |
| 2913 | |
| 2914 | // 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] | 2915 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2916 | // Mask is longer than the source vectors and is a multiple of the source |
| 2917 | // 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] | 2918 | // lengths match. |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2919 | if (SrcNumElts*2 == MaskNumElts) { |
| 2920 | // First check for Src1 in low and Src2 in high |
| 2921 | if (isSequentialInRange(Mask, 0, SrcNumElts, 0) && |
| 2922 | isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) { |
| 2923 | // The shuffle is concatenating two vectors together. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2924 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(), |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2925 | VT, Src1, Src2)); |
| 2926 | return; |
| 2927 | } |
| 2928 | // Then check for Src2 in low and Src1 in high |
| 2929 | if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) && |
| 2930 | isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) { |
| 2931 | // The shuffle is concatenating two vectors together. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2932 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(), |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2933 | VT, Src2, Src1)); |
| 2934 | return; |
| 2935 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2938 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2939 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2940 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2941 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2942 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2943 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2944 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2945 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2946 | MOps1[0] = Src1; |
| 2947 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2948 | |
| 2949 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2950 | getCurSDLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2951 | &MOps1[0], NumConcat); |
| 2952 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2953 | getCurSDLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2954 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2955 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2956 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2957 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2958 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2959 | int Idx = Mask[i]; |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 2960 | if (Idx >= (int)SrcNumElts) |
| 2961 | Idx -= SrcNumElts - MaskNumElts; |
| 2962 | MappedOps.push_back(Idx); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2963 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2964 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2965 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2966 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2967 | return; |
| 2968 | } |
| 2969 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2970 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2971 | // Analyze the access pattern of the vector to see if we can extract |
| 2972 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2973 | // the range of elements the mask access on both vectors. |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2974 | int MinRange[2] = { static_cast<int>(SrcNumElts), |
| 2975 | static_cast<int>(SrcNumElts)}; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2976 | int MaxRange[2] = {-1, -1}; |
| 2977 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2978 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2979 | int Idx = Mask[i]; |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2980 | unsigned Input = 0; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2981 | if (Idx < 0) |
| 2982 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2983 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2984 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2985 | Input = 1; |
| 2986 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2987 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2988 | if (Idx > MaxRange[Input]) |
| 2989 | MaxRange[Input] = Idx; |
| 2990 | if (Idx < MinRange[Input]) |
| 2991 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2992 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2993 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2994 | // Check if the access is smaller than the vector size and can we find |
| 2995 | // a reasonable extract index. |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2996 | int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not |
| 2997 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2998 | int StartIdx[2]; // StartIdx to extract from |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2999 | for (unsigned Input = 0; Input < 2; ++Input) { |
| 3000 | if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3001 | RangeUse[Input] = 0; // Unused |
| 3002 | StartIdx[Input] = 0; |
Craig Topper | f873dde | 2012-04-08 17:53:33 +0000 | [diff] [blame] | 3003 | continue; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 3004 | } |
Craig Topper | f873dde | 2012-04-08 17:53:33 +0000 | [diff] [blame] | 3005 | |
| 3006 | // Find a good start index that is a multiple of the mask length. Then |
| 3007 | // see if the rest of the elements are in range. |
| 3008 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
| 3009 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
| 3010 | StartIdx[Input] + MaskNumElts <= SrcNumElts) |
| 3011 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 3014 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3015 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3016 | return; |
| 3017 | } |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3018 | if (RangeUse[0] >= 0 && RangeUse[1] >= 0) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3019 | // Extract appropriate subvector and generate a vector shuffle |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3020 | for (unsigned Input = 0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 3021 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3022 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3023 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3024 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3025 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 3026 | Src, DAG.getConstant(StartIdx[Input], |
| 3027 | TLI->getVectorIdxTy())); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3028 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3029 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3030 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3031 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3032 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3033 | int Idx = Mask[i]; |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3034 | if (Idx >= 0) { |
| 3035 | if (Idx < (int)SrcNumElts) |
| 3036 | Idx -= StartIdx[0]; |
| 3037 | else |
| 3038 | Idx -= SrcNumElts + StartIdx[1] - MaskNumElts; |
| 3039 | } |
| 3040 | MappedOps.push_back(Idx); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3041 | } |
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 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3044 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3045 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3046 | } |
| 3047 | } |
| 3048 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3049 | // We can't use either concat vectors or extract subvectors so fall back to |
| 3050 | // replacing the shuffle with extract and build vector. |
| 3051 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3052 | EVT EltVT = VT.getVectorElementType(); |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 3053 | EVT IdxVT = TLI->getVectorIdxTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3054 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3055 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3056 | int Idx = Mask[i]; |
| 3057 | SDValue Res; |
| 3058 | |
| 3059 | if (Idx < 0) { |
| 3060 | Res = DAG.getUNDEF(EltVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3061 | } else { |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3062 | SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2; |
| 3063 | if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3064 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3065 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(), |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 3066 | EltVT, Src, DAG.getConstant(Idx, IdxVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3067 | } |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3068 | |
| 3069 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3070 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3071 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3072 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3073 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3076 | void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3077 | const Value *Op0 = I.getOperand(0); |
| 3078 | const Value *Op1 = I.getOperand(1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3079 | Type *AggTy = I.getType(); |
| 3080 | Type *ValTy = Op1->getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3081 | bool IntoUndef = isa<UndefValue>(Op0); |
| 3082 | bool FromUndef = isa<UndefValue>(Op1); |
| 3083 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3084 | unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3085 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3086 | const TargetLowering *TLI = TM.getTargetLowering(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3087 | SmallVector<EVT, 4> AggValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3088 | ComputeValueVTs(*TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3089 | SmallVector<EVT, 4> ValValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3090 | ComputeValueVTs(*TLI, ValTy, ValValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3091 | |
| 3092 | unsigned NumAggValues = AggValueVTs.size(); |
| 3093 | unsigned NumValValues = ValValueVTs.size(); |
| 3094 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 3095 | |
| 3096 | SDValue Agg = getValue(Op0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3097 | unsigned i = 0; |
| 3098 | // Copy the beginning value(s) from the original aggregate. |
| 3099 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3100 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3101 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 3102 | // Copy values from the inserted value(s). |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 3103 | if (NumValValues) { |
| 3104 | SDValue Val = getValue(Op1); |
| 3105 | for (; i != LinearIndex + NumValValues; ++i) |
| 3106 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
| 3107 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 3108 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3109 | // Copy remaining value(s) from the original aggregate. |
| 3110 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3111 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3112 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 3113 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3114 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3115 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 3116 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3119 | void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3120 | const Value *Op0 = I.getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3121 | Type *AggTy = Op0->getType(); |
| 3122 | Type *ValTy = I.getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3123 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 3124 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3125 | unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3126 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3127 | const TargetLowering *TLI = TM.getTargetLowering(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3128 | SmallVector<EVT, 4> ValValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3129 | ComputeValueVTs(*TLI, ValTy, ValValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3130 | |
| 3131 | unsigned NumValValues = ValValueVTs.size(); |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 3132 | |
| 3133 | // Ignore a extractvalue that produces an empty object |
| 3134 | if (!NumValValues) { |
| 3135 | setValue(&I, DAG.getUNDEF(MVT(MVT::Other))); |
| 3136 | return; |
| 3137 | } |
| 3138 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3139 | SmallVector<SDValue, 4> Values(NumValValues); |
| 3140 | |
| 3141 | SDValue Agg = getValue(Op0); |
| 3142 | // Copy out the selected value(s). |
| 3143 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 3144 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 3145 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3146 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 3147 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3148 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3149 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3150 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 3151 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3154 | void SelectionDAGBuilder::visitGetElementPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3155 | SDValue N = getValue(I.getOperand(0)); |
Nadav Rotem | 1c23920 | 2012-02-28 14:13:19 +0000 | [diff] [blame] | 3156 | // Note that the pointer operand may be a vector of pointers. Take the scalar |
| 3157 | // element which holds a pointer. |
| 3158 | Type *Ty = I.getOperand(0)->getType()->getScalarType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3159 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3160 | 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] | 3161 | OI != E; ++OI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3162 | const Value *Idx = *OI; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3163 | if (StructType *StTy = dyn_cast<StructType>(Ty)) { |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3164 | unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3165 | if (Field) { |
| 3166 | // N = N + Offset |
| 3167 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3168 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3169 | DAG.getConstant(Offset, N.getValueType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3170 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3171 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3172 | Ty = StTy->getElementType(Field); |
| 3173 | } else { |
| 3174 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 3175 | |
| 3176 | // If this is a constant subscript, handle it quickly. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3177 | const TargetLowering *TLI = TM.getTargetLowering(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3178 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 3179 | if (CI->isZero()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3180 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 3181 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 3182 | SDValue OffsVal; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3183 | EVT PTy = TLI->getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 3184 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3185 | if (PtrBits < 64) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3186 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3187 | TLI->getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3188 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3189 | else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 3190 | OffsVal = DAG.getIntPtrConstant(Offs); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3191 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3192 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 3193 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3194 | continue; |
| 3195 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3196 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3197 | // N = N + Idx * ElementSize; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3198 | APInt ElementSize = APInt(TLI->getPointerTy().getSizeInBits(), |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 3199 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3200 | SDValue IdxN = getValue(Idx); |
| 3201 | |
| 3202 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 3203 | // it. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3204 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3205 | |
| 3206 | // If this is a multiply by a power of two, turn it into a shl |
| 3207 | // immediately. This is a very common case. |
| 3208 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 3209 | if (ElementSize.isPowerOf2()) { |
| 3210 | unsigned Amt = ElementSize.logBase2(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3211 | IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3212 | N.getValueType(), IdxN, |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3213 | DAG.getConstant(Amt, IdxN.getValueType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3214 | } else { |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3215 | SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3216 | IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3217 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3218 | } |
| 3219 | } |
| 3220 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3221 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3222 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3223 | } |
| 3224 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3225 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3226 | setValue(&I, N); |
| 3227 | } |
| 3228 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3229 | void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3230 | // If this is a fixed sized alloca in the entry block of the function, |
| 3231 | // allocate it statically on the stack. |
| 3232 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 3233 | return; // getValue will auto-populate this. |
| 3234 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3235 | Type *Ty = I.getAllocatedType(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3236 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3237 | uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3238 | unsigned Align = |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3239 | std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3240 | I.getAlignment()); |
| 3241 | |
| 3242 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3243 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3244 | EVT IntPtr = TLI->getPointerTy(); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3245 | if (AllocSize.getValueType() != IntPtr) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3246 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3247 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3248 | AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr, |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3249 | AllocSize, |
| 3250 | DAG.getConstant(TySize, IntPtr)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3251 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3252 | // Handle alignment. If the requested alignment is less than or equal to |
| 3253 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 3254 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 3255 | unsigned StackAlign = TM.getFrameLowering()->getStackAlignment(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3256 | if (Align <= StackAlign) |
| 3257 | Align = 0; |
| 3258 | |
| 3259 | // Round the size of the allocation up to the stack alignment size |
| 3260 | // by add SA-1 to the size. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3261 | AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3262 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3263 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3264 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3265 | // Mask out the low bits for alignment purposes. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3266 | AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3267 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3268 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 3269 | |
| 3270 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3271 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3272 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3273 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3274 | setValue(&I, DSA); |
| 3275 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3276 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3277 | // Inform the Frame Information that we have just allocated a variable-sized |
| 3278 | // object. |
Bob Wilson | 8f637ad | 2013-02-08 20:35:15 +0000 | [diff] [blame] | 3279 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3282 | void SelectionDAGBuilder::visitLoad(const LoadInst &I) { |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3283 | if (I.isAtomic()) |
| 3284 | return visitAtomicLoad(I); |
| 3285 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3286 | const Value *SV = I.getOperand(0); |
| 3287 | SDValue Ptr = getValue(SV); |
| 3288 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3289 | Type *Ty = I.getType(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3290 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3291 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3292 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 3293 | bool isInvariant = I.getMetadata("invariant.load") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3294 | unsigned Alignment = I.getAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3295 | const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa); |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 3296 | const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3297 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3298 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3299 | SmallVector<uint64_t, 4> Offsets; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3300 | ComputeValueVTs(*TM.getTargetLowering(), Ty, ValueVTs, &Offsets); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3301 | unsigned NumValues = ValueVTs.size(); |
| 3302 | if (NumValues == 0) |
| 3303 | return; |
| 3304 | |
| 3305 | SDValue Root; |
| 3306 | bool ConstantMemory = false; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3307 | if (I.isVolatile() || NumValues > MaxParallelChains) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3308 | // Serialize volatile loads with other side effects. |
| 3309 | Root = getRoot(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3310 | else if (AA->pointsToConstantMemory( |
| 3311 | AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3312 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 3313 | Root = DAG.getEntryNode(); |
| 3314 | ConstantMemory = true; |
| 3315 | } else { |
| 3316 | // Do not serialize non-volatile loads against each other. |
| 3317 | Root = DAG.getRoot(); |
| 3318 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3319 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3320 | SmallVector<SDValue, 4> Values(NumValues); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3321 | SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains), |
| 3322 | NumValues)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3323 | EVT PtrVT = Ptr.getValueType(); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3324 | unsigned ChainI = 0; |
| 3325 | for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { |
| 3326 | // Serializing loads here may result in excessive register pressure, and |
| 3327 | // TokenFactor places arbitrary choke points on the scheduler. SD scheduling |
| 3328 | // could recover a bit by hoisting nodes upward in the chain by recognizing |
| 3329 | // they are side-effect free or do not alias. The optimizer should really |
| 3330 | // avoid this case by converting large object/array copies to llvm.memcpy |
| 3331 | // (MaxParallelChains should always remain as failsafe). |
| 3332 | if (ChainI == MaxParallelChains) { |
| 3333 | assert(PendingLoads.empty() && "PendingLoads must be serialized first"); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3334 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3335 | MVT::Other, &Chains[0], ChainI); |
| 3336 | Root = Chain; |
| 3337 | ChainI = 0; |
| 3338 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3339 | SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3340 | PtrVT, Ptr, |
| 3341 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3342 | SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root, |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 3343 | A, MachinePointerInfo(SV, Offsets[i]), isVolatile, |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 3344 | isNonTemporal, isInvariant, Alignment, TBAAInfo, |
| 3345 | Ranges); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3346 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3347 | Values[i] = L; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3348 | Chains[ChainI] = L.getValue(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3349 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3350 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3351 | if (!ConstantMemory) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3352 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3353 | MVT::Other, &Chains[0], ChainI); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3354 | if (isVolatile) |
| 3355 | DAG.setRoot(Chain); |
| 3356 | else |
| 3357 | PendingLoads.push_back(Chain); |
| 3358 | } |
| 3359 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3360 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3361 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 3362 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3363 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3364 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3365 | void SelectionDAGBuilder::visitStore(const StoreInst &I) { |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3366 | if (I.isAtomic()) |
| 3367 | return visitAtomicStore(I); |
| 3368 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3369 | const Value *SrcV = I.getOperand(0); |
| 3370 | const Value *PtrV = I.getOperand(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3371 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3372 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3373 | SmallVector<uint64_t, 4> Offsets; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3374 | ComputeValueVTs(*TM.getTargetLowering(), SrcV->getType(), ValueVTs, &Offsets); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3375 | unsigned NumValues = ValueVTs.size(); |
| 3376 | if (NumValues == 0) |
| 3377 | return; |
| 3378 | |
| 3379 | // Get the lowered operands. Note that we do this after |
| 3380 | // checking if NumResults is zero, because with zero results |
| 3381 | // the operands won't have values in the map. |
| 3382 | SDValue Src = getValue(SrcV); |
| 3383 | SDValue Ptr = getValue(PtrV); |
| 3384 | |
| 3385 | SDValue Root = getRoot(); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3386 | SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains), |
| 3387 | NumValues)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3388 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3389 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3390 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3391 | unsigned Alignment = I.getAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3392 | const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3393 | |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3394 | unsigned ChainI = 0; |
| 3395 | for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { |
| 3396 | // See visitLoad comments. |
| 3397 | if (ChainI == MaxParallelChains) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3398 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3399 | MVT::Other, &Chains[0], ChainI); |
| 3400 | Root = Chain; |
| 3401 | ChainI = 0; |
| 3402 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3403 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr, |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3404 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3405 | SDValue St = DAG.getStore(Root, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3406 | SDValue(Src.getNode(), Src.getResNo() + i), |
| 3407 | Add, MachinePointerInfo(PtrV, Offsets[i]), |
| 3408 | isVolatile, isNonTemporal, Alignment, TBAAInfo); |
| 3409 | Chains[ChainI] = St; |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3412 | SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3413 | MVT::Other, &Chains[0], ChainI); |
Devang Patel | 7e13efa | 2010-10-26 22:14:52 +0000 | [diff] [blame] | 3414 | DAG.setRoot(StoreNode); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3417 | static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3418 | SynchronizationScope Scope, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3419 | bool Before, SDLoc dl, |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3420 | SelectionDAG &DAG, |
| 3421 | const TargetLowering &TLI) { |
| 3422 | // Fence, if necessary |
| 3423 | if (Before) { |
Eli Friedman | 069e2ed | 2011-08-26 02:59:24 +0000 | [diff] [blame] | 3424 | if (Order == AcquireRelease || Order == SequentiallyConsistent) |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3425 | Order = Release; |
| 3426 | else if (Order == Acquire || Order == Monotonic) |
| 3427 | return Chain; |
| 3428 | } else { |
| 3429 | if (Order == AcquireRelease) |
| 3430 | Order = Acquire; |
| 3431 | else if (Order == Release || Order == Monotonic) |
| 3432 | return Chain; |
| 3433 | } |
| 3434 | SDValue Ops[3]; |
| 3435 | Ops[0] = Chain; |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3436 | Ops[1] = DAG.getConstant(Order, TLI.getPointerTy()); |
| 3437 | Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy()); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3438 | return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3); |
| 3439 | } |
| 3440 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3441 | void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3442 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3443 | AtomicOrdering Order = I.getOrdering(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3444 | SynchronizationScope Scope = I.getSynchScope(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3445 | |
| 3446 | SDValue InChain = getRoot(); |
| 3447 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3448 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3449 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3450 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3451 | DAG, *TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3452 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3453 | SDValue L = |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3454 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 3455 | getValue(I.getCompareOperand()).getSimpleValueType(), |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3456 | InChain, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3457 | getValue(I.getPointerOperand()), |
| 3458 | getValue(I.getCompareOperand()), |
| 3459 | getValue(I.getNewValOperand()), |
| 3460 | MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3461 | TLI->getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3462 | Scope); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3463 | |
| 3464 | SDValue OutChain = L.getValue(1); |
| 3465 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3466 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3467 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3468 | DAG, *TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3469 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3470 | setValue(&I, L); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3471 | DAG.setRoot(OutChain); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3475 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3476 | ISD::NodeType NT; |
| 3477 | switch (I.getOperation()) { |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 3478 | default: llvm_unreachable("Unknown atomicrmw operation"); |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3479 | case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break; |
| 3480 | case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break; |
| 3481 | case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break; |
| 3482 | case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break; |
| 3483 | case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break; |
| 3484 | case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break; |
| 3485 | case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break; |
| 3486 | case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break; |
| 3487 | case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break; |
| 3488 | case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break; |
| 3489 | case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break; |
| 3490 | } |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3491 | AtomicOrdering Order = I.getOrdering(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3492 | SynchronizationScope Scope = I.getSynchScope(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3493 | |
| 3494 | SDValue InChain = getRoot(); |
| 3495 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3496 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3497 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3498 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3499 | DAG, *TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3500 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3501 | SDValue L = |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3502 | DAG.getAtomic(NT, dl, |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 3503 | getValue(I.getValOperand()).getSimpleValueType(), |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3504 | InChain, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3505 | getValue(I.getPointerOperand()), |
| 3506 | getValue(I.getValOperand()), |
| 3507 | I.getPointerOperand(), 0 /* Alignment */, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3508 | TLI->getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3509 | Scope); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3510 | |
| 3511 | SDValue OutChain = L.getValue(1); |
| 3512 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3513 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3514 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3515 | DAG, *TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3516 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3517 | setValue(&I, L); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3518 | DAG.setRoot(OutChain); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3521 | void SelectionDAGBuilder::visitFence(const FenceInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3522 | SDLoc dl = getCurSDLoc(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3523 | const TargetLowering *TLI = TM.getTargetLowering(); |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 3524 | SDValue Ops[3]; |
| 3525 | Ops[0] = getRoot(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3526 | Ops[1] = DAG.getConstant(I.getOrdering(), TLI->getPointerTy()); |
| 3527 | Ops[2] = DAG.getConstant(I.getSynchScope(), TLI->getPointerTy()); |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 3528 | DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3)); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3531 | void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &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 | AtomicOrdering Order = I.getOrdering(); |
| 3534 | SynchronizationScope Scope = I.getSynchScope(); |
| 3535 | |
| 3536 | SDValue InChain = getRoot(); |
| 3537 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3538 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3539 | EVT VT = TLI->getValueType(I.getType()); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +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 load"); |
| 3543 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3544 | SDValue L = |
| 3545 | DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain, |
| 3546 | getValue(I.getPointerOperand()), |
| 3547 | I.getPointerOperand(), I.getAlignment(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3548 | TLI->getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3549 | Scope); |
| 3550 | |
| 3551 | SDValue OutChain = L.getValue(1); |
| 3552 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3553 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3554 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3555 | DAG, *TLI); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3556 | |
| 3557 | setValue(&I, L); |
| 3558 | DAG.setRoot(OutChain); |
| 3559 | } |
| 3560 | |
| 3561 | void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3562 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3563 | |
| 3564 | AtomicOrdering Order = I.getOrdering(); |
| 3565 | SynchronizationScope Scope = I.getSynchScope(); |
| 3566 | |
| 3567 | SDValue InChain = getRoot(); |
| 3568 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3569 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3570 | EVT VT = TLI->getValueType(I.getValueOperand()->getType()); |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3571 | |
Evan Cheng | 607acd6 | 2013-02-06 02:06:33 +0000 | [diff] [blame] | 3572 | if (I.getAlignment() < VT.getSizeInBits() / 8) |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3573 | report_fatal_error("Cannot generate unaligned atomic store"); |
| 3574 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3575 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3576 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3577 | DAG, *TLI); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3578 | |
| 3579 | SDValue OutChain = |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3580 | DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3581 | InChain, |
| 3582 | getValue(I.getPointerOperand()), |
| 3583 | getValue(I.getValueOperand()), |
| 3584 | I.getPointerOperand(), I.getAlignment(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3585 | TLI->getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3586 | Scope); |
| 3587 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3588 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3589 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3590 | DAG, *TLI); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3591 | |
| 3592 | DAG.setRoot(OutChain); |
| 3593 | } |
| 3594 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3595 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 3596 | /// node. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3597 | void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3598 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3599 | bool HasChain = !I.doesNotAccessMemory(); |
| 3600 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 3601 | |
| 3602 | // Build the operand list. |
| 3603 | SmallVector<SDValue, 8> Ops; |
| 3604 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 3605 | if (OnlyLoad) { |
| 3606 | // We don't need to serialize loads against other loads. |
| 3607 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3608 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3609 | Ops.push_back(getRoot()); |
| 3610 | } |
| 3611 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3612 | |
| 3613 | // Info is set by getTgtMemInstrinsic |
| 3614 | TargetLowering::IntrinsicInfo Info; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3615 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3616 | bool IsTgtIntrinsic = TLI->getTgtMemIntrinsic(Info, I, Intrinsic); |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3617 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3618 | // 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] | 3619 | if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID || |
| 3620 | Info.opc == ISD::INTRINSIC_W_CHAIN) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3621 | Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI->getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3622 | |
| 3623 | // Add all operands of the call to the operand list. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3624 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 3625 | SDValue Op = getValue(I.getArgOperand(i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3626 | Ops.push_back(Op); |
| 3627 | } |
| 3628 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3629 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3630 | ComputeValueVTs(*TLI, I.getType(), ValueVTs); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3631 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3632 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3633 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3634 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 3635 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3636 | |
| 3637 | // Create the node. |
| 3638 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3639 | if (IsTgtIntrinsic) { |
| 3640 | // This is target intrinsic that touches memory |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3641 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3642 | VTs, &Ops[0], Ops.size(), |
Chris Lattner | e9ba5dd | 2010-09-21 04:57:15 +0000 | [diff] [blame] | 3643 | Info.memVT, |
| 3644 | MachinePointerInfo(Info.ptrVal, Info.offset), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3645 | Info.align, Info.vol, |
| 3646 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3647 | } else if (!HasChain) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3648 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3649 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3650 | } else if (!I.getType()->isVoidTy()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3651 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3652 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3653 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3654 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3655 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3658 | if (HasChain) { |
| 3659 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 3660 | if (OnlyLoad) |
| 3661 | PendingLoads.push_back(Chain); |
| 3662 | else |
| 3663 | DAG.setRoot(Chain); |
| 3664 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3665 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3666 | if (!I.getType()->isVoidTy()) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3667 | if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3668 | EVT VT = TLI->getValueType(PTy); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3669 | Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3670 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3671 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3672 | setValue(&I, Result); |
| 3673 | } |
| 3674 | } |
| 3675 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3676 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3677 | /// number with exponent of 1: |
| 3678 | /// |
| 3679 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3680 | /// |
Matt Beaumont-Gay | 50e75bf | 2013-02-25 18:11:18 +0000 | [diff] [blame] | 3681 | /// where Op is the hexadecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3682 | static SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3683 | GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3684 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3685 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 3686 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 3687 | DAG.getConstant(0x3f800000, MVT::i32)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3688 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3691 | /// GetExponent - Get the exponent: |
| 3692 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3693 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3694 | /// |
Matt Beaumont-Gay | 50e75bf | 2013-02-25 18:11:18 +0000 | [diff] [blame] | 3695 | /// where Op is the hexadecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3696 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3697 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3698 | SDLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3699 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3700 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 3701 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3702 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3703 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 3704 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3705 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3708 | /// getF32Constant - Get 32-bit floating point constant. |
| 3709 | static SDValue |
| 3710 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3711 | return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)), |
| 3712 | MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3715 | /// expandExp - Lower an exp intrinsic. Handles the special sequences for |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3716 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3717 | static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3718 | const TargetLowering &TLI) { |
| 3719 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3720 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3721 | |
| 3722 | // Put the exponent in the right bit position for later addition to the |
| 3723 | // final result: |
| 3724 | // |
| 3725 | // #define LOG2OFe 1.4426950f |
| 3726 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3727 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3728 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3729 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3730 | |
| 3731 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3732 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3733 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3734 | |
| 3735 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3736 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3737 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3738 | |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3739 | SDValue TwoToFracPartOfX; |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3740 | if (LimitFloatPrecision <= 6) { |
| 3741 | // For floating-point precision of 6: |
| 3742 | // |
| 3743 | // TwoToFractionalPartOfX = |
| 3744 | // 0.997535578f + |
| 3745 | // (0.735607626f + 0.252464424f * x) * x; |
| 3746 | // |
| 3747 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3748 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3749 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3750 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3751 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3752 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3753 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3754 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3755 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3756 | // For floating-point precision of 12: |
| 3757 | // |
| 3758 | // TwoToFractionalPartOfX = |
| 3759 | // 0.999892986f + |
| 3760 | // (0.696457318f + |
| 3761 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3762 | // |
| 3763 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3764 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3765 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3766 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3767 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3768 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3769 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3770 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3771 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3772 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3773 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3774 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3775 | // For floating-point precision of 18: |
| 3776 | // |
| 3777 | // TwoToFractionalPartOfX = |
| 3778 | // 0.999999982f + |
| 3779 | // (0.693148872f + |
| 3780 | // (0.240227044f + |
| 3781 | // (0.554906021e-1f + |
| 3782 | // (0.961591928e-2f + |
| 3783 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3784 | // |
| 3785 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3786 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3787 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3788 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3789 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3790 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3791 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3792 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3793 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3794 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3795 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3796 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3797 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3798 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3799 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3800 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3801 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3802 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3803 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 3804 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3805 | } |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3806 | |
| 3807 | // Add the exponent into the result in integer domain. |
| 3808 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX); |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3809 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 3810 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 3811 | t13, IntegerPartOfX)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3814 | // No special expansion. |
| 3815 | return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3818 | /// expandLog - Lower a log intrinsic. Handles the special sequences for |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3819 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3820 | static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3821 | const TargetLowering &TLI) { |
| 3822 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3823 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3824 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3825 | |
| 3826 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3827 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3828 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3829 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3830 | |
| 3831 | // Get the significand and build it into a floating-point number with |
| 3832 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3833 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3834 | |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3835 | SDValue LogOfMantissa; |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3836 | if (LimitFloatPrecision <= 6) { |
| 3837 | // For floating-point precision of 6: |
| 3838 | // |
| 3839 | // LogofMantissa = |
| 3840 | // -1.1609546f + |
| 3841 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3842 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3843 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3844 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3845 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3846 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3847 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3848 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3849 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3850 | getF32Constant(DAG, 0x3f949a29)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3851 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3852 | // For floating-point precision of 12: |
| 3853 | // |
| 3854 | // LogOfMantissa = |
| 3855 | // -1.7417939f + |
| 3856 | // (2.8212026f + |
| 3857 | // (-1.4699568f + |
| 3858 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3859 | // |
| 3860 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3861 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3862 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3863 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3864 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3865 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3866 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3867 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3868 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3869 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3870 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3871 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3872 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3873 | getF32Constant(DAG, 0x3fdef31a)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3874 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3875 | // For floating-point precision of 18: |
| 3876 | // |
| 3877 | // LogOfMantissa = |
| 3878 | // -2.1072184f + |
| 3879 | // (4.2372794f + |
| 3880 | // (-3.7029485f + |
| 3881 | // (2.2781945f + |
| 3882 | // (-0.87823314f + |
| 3883 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3884 | // |
| 3885 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3886 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3887 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3888 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3889 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3890 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3891 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3892 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3893 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3894 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3895 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3896 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3897 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3898 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3899 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3900 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3901 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3902 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3903 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3904 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3905 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3906 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3907 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3910 | // No special expansion. |
| 3911 | return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3914 | /// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3915 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3916 | static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3917 | const TargetLowering &TLI) { |
| 3918 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3919 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3920 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3921 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3922 | // Get the exponent. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3923 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3924 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3925 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3926 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3927 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3928 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3929 | // Different possible minimax approximations of significand in |
| 3930 | // floating-point for various degrees of accuracy over [1,2]. |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3931 | SDValue Log2ofMantissa; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3932 | if (LimitFloatPrecision <= 6) { |
| 3933 | // For floating-point precision of 6: |
| 3934 | // |
| 3935 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3936 | // |
| 3937 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3938 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3939 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3940 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3941 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3942 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3943 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3944 | getF32Constant(DAG, 0x3fd6633d)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3945 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3946 | // For floating-point precision of 12: |
| 3947 | // |
| 3948 | // Log2ofMantissa = |
| 3949 | // -2.51285454f + |
| 3950 | // (4.07009056f + |
| 3951 | // (-2.12067489f + |
| 3952 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3953 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3954 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3955 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3956 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3957 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3958 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3959 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3960 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3961 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3962 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3963 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3964 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3965 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3966 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3967 | getF32Constant(DAG, 0x4020d29c)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3968 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3969 | // For floating-point precision of 18: |
| 3970 | // |
| 3971 | // Log2ofMantissa = |
| 3972 | // -3.0400495f + |
| 3973 | // (6.1129976f + |
| 3974 | // (-5.3420409f + |
| 3975 | // (3.2865683f + |
| 3976 | // (-1.2669343f + |
| 3977 | // (0.27515199f - |
| 3978 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3979 | // |
| 3980 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3981 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3982 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3983 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3984 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3985 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3986 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3987 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3988 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3989 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3990 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3991 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3992 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3993 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3994 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3995 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3996 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3997 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3998 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3999 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4000 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4001 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4002 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa); |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 4003 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4004 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4005 | // No special expansion. |
| 4006 | return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4009 | /// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4010 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4011 | static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4012 | const TargetLowering &TLI) { |
| 4013 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4014 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4015 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4016 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 4017 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4018 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4019 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4020 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4021 | |
| 4022 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 4023 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4024 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4025 | |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4026 | SDValue Log10ofMantissa; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4027 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4028 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4029 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4030 | // Log10ofMantissa = |
| 4031 | // -0.50419619f + |
| 4032 | // (0.60948995f - 0.10380950f * x) * x; |
| 4033 | // |
| 4034 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4035 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4036 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4037 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4038 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4039 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4040 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 4041 | getF32Constant(DAG, 0x3f011300)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4042 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4043 | // For floating-point precision of 12: |
| 4044 | // |
| 4045 | // Log10ofMantissa = |
| 4046 | // -0.64831180f + |
| 4047 | // (0.91751397f + |
| 4048 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 4049 | // |
| 4050 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4051 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4052 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4053 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4054 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4055 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4056 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4057 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4058 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4059 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
| 4060 | getF32Constant(DAG, 0x3f25f7c3)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4061 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4062 | // For floating-point precision of 18: |
| 4063 | // |
| 4064 | // Log10ofMantissa = |
| 4065 | // -0.84299375f + |
| 4066 | // (1.5327582f + |
| 4067 | // (-1.0688956f + |
| 4068 | // (0.49102474f + |
| 4069 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 4070 | // |
| 4071 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4072 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4073 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4074 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4075 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4076 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4077 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4078 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4079 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4080 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4081 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4082 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4083 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4084 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4085 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4086 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
| 4087 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4088 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4089 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4090 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa); |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 4091 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4092 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4093 | // No special expansion. |
| 4094 | return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4097 | /// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4098 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4099 | static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4100 | const TargetLowering &TLI) { |
| 4101 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4102 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4103 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4104 | |
| 4105 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4106 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 4107 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4108 | |
| 4109 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4110 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 4111 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4112 | |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4113 | SDValue TwoToFractionalPartOfX; |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4114 | if (LimitFloatPrecision <= 6) { |
| 4115 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4116 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4117 | // TwoToFractionalPartOfX = |
| 4118 | // 0.997535578f + |
| 4119 | // (0.735607626f + 0.252464424f * x) * x; |
| 4120 | // |
| 4121 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4122 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4123 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4124 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4125 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4126 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4127 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 4128 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4129 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4130 | // For floating-point precision of 12: |
| 4131 | // |
| 4132 | // TwoToFractionalPartOfX = |
| 4133 | // 0.999892986f + |
| 4134 | // (0.696457318f + |
| 4135 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 4136 | // |
| 4137 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4138 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4139 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4140 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4141 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4142 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4143 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4144 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4145 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4146 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 4147 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4148 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4149 | // For floating-point precision of 18: |
| 4150 | // |
| 4151 | // TwoToFractionalPartOfX = |
| 4152 | // 0.999999982f + |
| 4153 | // (0.693148872f + |
| 4154 | // (0.240227044f + |
| 4155 | // (0.554906021e-1f + |
| 4156 | // (0.961591928e-2f + |
| 4157 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 4158 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4159 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4160 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4161 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4162 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4163 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4164 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4165 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4166 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4167 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4168 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4169 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4170 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4171 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4172 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 4173 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4174 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4175 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4176 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 4177 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4178 | } |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4179 | |
| 4180 | // Add the exponent into the result in integer domain. |
| 4181 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, |
| 4182 | TwoToFractionalPartOfX); |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4183 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 4184 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 4185 | t13, IntegerPartOfX)); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4186 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4187 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4188 | // No special expansion. |
| 4189 | return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4192 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 4193 | /// limited-precision mode with x == 10.0f. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4194 | static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS, |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4195 | SelectionDAG &DAG, const TargetLowering &TLI) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4196 | bool IsExp10 = false; |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4197 | if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4198 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4199 | if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) { |
| 4200 | APFloat Ten(10.0f); |
| 4201 | IsExp10 = LHSC->isExactlyValue(Ten); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4202 | } |
| 4203 | } |
| 4204 | |
Craig Topper | c1aa638 | 2012-11-25 00:48:58 +0000 | [diff] [blame] | 4205 | if (IsExp10) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4206 | // Put the exponent in the right bit position for later addition to the |
| 4207 | // final result: |
| 4208 | // |
| 4209 | // #define LOG2OF10 3.3219281f |
| 4210 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4211 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4212 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4213 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4214 | |
| 4215 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4216 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 4217 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4218 | |
| 4219 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4220 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 4221 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4222 | |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4223 | SDValue TwoToFractionalPartOfX; |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4224 | if (LimitFloatPrecision <= 6) { |
| 4225 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4226 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4227 | // twoToFractionalPartOfX = |
| 4228 | // 0.997535578f + |
| 4229 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4230 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4231 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4232 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4233 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4234 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4235 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4236 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4237 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 4238 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4239 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4240 | // For floating-point precision of 12: |
| 4241 | // |
| 4242 | // TwoToFractionalPartOfX = |
| 4243 | // 0.999892986f + |
| 4244 | // (0.696457318f + |
| 4245 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 4246 | // |
| 4247 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4248 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4249 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4250 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4251 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4252 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4253 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4254 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4255 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4256 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 4257 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4258 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4259 | // For floating-point precision of 18: |
| 4260 | // |
| 4261 | // TwoToFractionalPartOfX = |
| 4262 | // 0.999999982f + |
| 4263 | // (0.693148872f + |
| 4264 | // (0.240227044f + |
| 4265 | // (0.554906021e-1f + |
| 4266 | // (0.961591928e-2f + |
| 4267 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 4268 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4269 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4270 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4271 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4272 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4273 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4274 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4275 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4276 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4277 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4278 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4279 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4280 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4281 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4282 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 4283 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4284 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4285 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4286 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 4287 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4288 | } |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4289 | |
| 4290 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX); |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4291 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 4292 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 4293 | t13, IntegerPartOfX)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4296 | // No special expansion. |
| 4297 | return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4300 | |
| 4301 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4302 | static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4303 | SelectionDAG &DAG) { |
| 4304 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 4305 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 4306 | // optimizing for size, we only want to do this if the expansion would produce |
| 4307 | // a small number of multiplies, otherwise we do the full expansion. |
| 4308 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 4309 | // Get the exponent as a positive value. |
| 4310 | unsigned Val = RHSC->getSExtValue(); |
| 4311 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4312 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4313 | // powi(x, 0) -> 1.0 |
| 4314 | if (Val == 0) |
| 4315 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 4316 | |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 4317 | const Function *F = DAG.getMachineFunction().getFunction(); |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 4318 | if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 4319 | Attribute::OptimizeForSize) || |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4320 | // If optimizing for size, don't insert too many multiplies. This |
| 4321 | // inserts up to 5 multiplies. |
| 4322 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 4323 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4324 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4325 | // powi(x,15) generates one more multiply than it should), but this has |
| 4326 | // the benefit of being both really simple and much better than a libcall. |
| 4327 | SDValue Res; // Logically starts equal to 1.0 |
| 4328 | SDValue CurSquare = LHS; |
| 4329 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 4330 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4331 | if (Res.getNode()) |
| 4332 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 4333 | else |
| 4334 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 4335 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4336 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4337 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 4338 | CurSquare, CurSquare); |
| 4339 | Val >>= 1; |
| 4340 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4341 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4342 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 4343 | if (RHSC->getSExtValue() < 0) |
| 4344 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 4345 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 4346 | return Res; |
| 4347 | } |
| 4348 | } |
| 4349 | |
| 4350 | // Otherwise, expand to a libcall. |
| 4351 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 4352 | } |
| 4353 | |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4354 | // getTruncatedArgReg - Find underlying register used for an truncated |
| 4355 | // argument. |
| 4356 | static unsigned getTruncatedArgReg(const SDValue &N) { |
| 4357 | if (N.getOpcode() != ISD::TRUNCATE) |
| 4358 | return 0; |
| 4359 | |
| 4360 | const SDValue &Ext = N.getOperand(0); |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 4361 | if (Ext.getOpcode() == ISD::AssertZext || |
| 4362 | Ext.getOpcode() == ISD::AssertSext) { |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4363 | const SDValue &CFR = Ext.getOperand(0); |
| 4364 | if (CFR.getOpcode() == ISD::CopyFromReg) |
| 4365 | return cast<RegisterSDNode>(CFR.getOperand(1))->getReg(); |
Craig Topper | 7eb46d8 | 2012-04-11 04:55:51 +0000 | [diff] [blame] | 4366 | if (CFR.getOpcode() == ISD::TRUNCATE) |
| 4367 | return getTruncatedArgReg(CFR); |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4368 | } |
| 4369 | return 0; |
| 4370 | } |
| 4371 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4372 | /// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function |
| 4373 | /// argument, create the corresponding DBG_VALUE machine instruction for it now. |
| 4374 | /// 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] | 4375 | bool |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 4376 | SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable, |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4377 | int64_t Offset, |
Dan Gohman | 5d11ea3 | 2010-05-01 00:33:16 +0000 | [diff] [blame] | 4378 | const SDValue &N) { |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4379 | const Argument *Arg = dyn_cast<Argument>(V); |
| 4380 | if (!Arg) |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4381 | return false; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4382 | |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 4383 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4384 | const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo(); |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4385 | |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 4386 | // Ignore inlined function arguments here. |
| 4387 | DIVariable DV(Variable); |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 4388 | if (DV.isInlinedFnArgument(MF.getFunction())) |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 4389 | return false; |
| 4390 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4391 | Optional<MachineOperand> Op; |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 4392 | // Some arguments' frame index is recorded during argument lowering. |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4393 | if (int FI = FuncInfo.getArgumentFrameIndex(Arg)) |
| 4394 | Op = MachineOperand::CreateFI(FI); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4395 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4396 | if (!Op && N.getNode()) { |
| 4397 | unsigned Reg; |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4398 | if (N.getOpcode() == ISD::CopyFromReg) |
| 4399 | Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
| 4400 | else |
| 4401 | Reg = getTruncatedArgReg(N); |
| 4402 | if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4403 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 4404 | unsigned PR = RegInfo.getLiveInPhysReg(Reg); |
| 4405 | if (PR) |
| 4406 | Reg = PR; |
| 4407 | } |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4408 | if (Reg) |
| 4409 | Op = MachineOperand::CreateReg(Reg, false); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4412 | if (!Op) { |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4413 | // Check if ValueMap has reg number. |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4414 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4415 | if (VMI != FuncInfo.ValueMap.end()) |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4416 | Op = MachineOperand::CreateReg(VMI->second, false); |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4417 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4418 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4419 | if (!Op && N.getNode()) |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4420 | // Check if frame index is available. |
| 4421 | if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode())) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4422 | if (FrameIndexSDNode *FINode = |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4423 | dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) |
| 4424 | Op = MachineOperand::CreateFI(FINode->getIndex()); |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4425 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4426 | if (!Op) |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4427 | return false; |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4428 | |
Adrian Prantl | 893ae83 | 2013-07-10 01:53:30 +0000 | [diff] [blame] | 4429 | // FIXME: This does not handle register-indirect values at offset 0. |
| 4430 | bool IsIndirect = Offset != 0; |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4431 | if (Op->isReg()) |
Adrian Prantl | 3517640 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 4432 | FuncInfo.ArgDbgValues.push_back(BuildMI(MF, getCurDebugLoc(), |
| 4433 | TII->get(TargetOpcode::DBG_VALUE), |
Adrian Prantl | 893ae83 | 2013-07-10 01:53:30 +0000 | [diff] [blame] | 4434 | IsIndirect, |
Adrian Prantl | 3517640 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 4435 | Op->getReg(), Offset, Variable)); |
| 4436 | else |
| 4437 | FuncInfo.ArgDbgValues.push_back( |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4438 | BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE)) |
| 4439 | .addOperand(*Op).addImm(Offset).addMetadata(Variable)); |
Adrian Prantl | 3517640 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 4440 | |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4441 | return true; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4442 | } |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 4444 | // VisualStudio defines setjmp as _setjmp |
Michael J. Spencer | 1f40960 | 2010-09-24 19:48:47 +0000 | [diff] [blame] | 4445 | #if defined(_MSC_VER) && defined(setjmp) && \ |
| 4446 | !defined(setjmp_undefined_for_msvc) |
| 4447 | # pragma push_macro("setjmp") |
| 4448 | # undef setjmp |
| 4449 | # define setjmp_undefined_for_msvc |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 4450 | #endif |
| 4451 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4452 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 4453 | /// we want to emit this as a call to a named external function, return the name |
| 4454 | /// otherwise lower it and return null. |
| 4455 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4456 | SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4457 | const TargetLowering *TLI = TM.getTargetLowering(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4458 | SDLoc sdl = getCurSDLoc(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4459 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4460 | SDValue Res; |
| 4461 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4462 | switch (Intrinsic) { |
| 4463 | default: |
| 4464 | // By default, turn this into a target intrinsic node. |
| 4465 | visitTargetIntrinsic(I, Intrinsic); |
| 4466 | return 0; |
| 4467 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 4468 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 4469 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 4470 | case Intrinsic::returnaddress: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4471 | setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI->getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4472 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4473 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 4474 | case Intrinsic::frameaddress: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4475 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI->getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4476 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4477 | return 0; |
| 4478 | case Intrinsic::setjmp: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4479 | return &"_setjmp"[!TLI->usesUnderscoreSetJmp()]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4480 | case Intrinsic::longjmp: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4481 | return &"_longjmp"[!TLI->usesUnderscoreLongJmp()]; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4482 | case Intrinsic::memcpy: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4483 | // Assert for address < 256 since we support only user defined address |
| 4484 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4485 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4486 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4487 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4488 | < 256 && |
| 4489 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4490 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4491 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4492 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4493 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4494 | if (!Align) |
| 4495 | 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] | 4496 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4497 | DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4498 | MachinePointerInfo(I.getArgOperand(0)), |
| 4499 | MachinePointerInfo(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4500 | return 0; |
| 4501 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4502 | case Intrinsic::memset: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4503 | // Assert for address < 256 since we support only user defined address |
| 4504 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4505 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4506 | < 256 && |
| 4507 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4508 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4509 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4510 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4511 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4512 | if (!Align) |
| 4513 | 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] | 4514 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4515 | DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4516 | MachinePointerInfo(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4517 | return 0; |
| 4518 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4519 | case Intrinsic::memmove: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4520 | // Assert for address < 256 since we support only user defined address |
| 4521 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4522 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4523 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4524 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4525 | < 256 && |
| 4526 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4527 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4528 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4529 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4530 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4531 | if (!Align) |
| 4532 | 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] | 4533 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4534 | DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4535 | MachinePointerInfo(I.getArgOperand(0)), |
| 4536 | MachinePointerInfo(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4537 | return 0; |
| 4538 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4539 | case Intrinsic::dbg_declare: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4540 | const DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 4541 | MDNode *Variable = DI.getVariable(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4542 | const Value *Address = DI.getAddress(); |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 4543 | DIVariable DIVar(Variable); |
| 4544 | assert((!DIVar || DIVar.isVariable()) && |
| 4545 | "Variable in DbgDeclareInst should be either null or a DIVariable."); |
| 4546 | if (!Address || !DIVar) { |
Eric Christopher | 8f2a88d | 2012-03-15 21:33:41 +0000 | [diff] [blame] | 4547 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Dale Johannesen | 8ac38f2 | 2010-02-08 21:53:27 +0000 | [diff] [blame] | 4548 | return 0; |
Eric Christopher | 8f2a88d | 2012-03-15 21:33:41 +0000 | [diff] [blame] | 4549 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4550 | |
Devang Patel | 3f74a11 | 2010-09-02 21:29:42 +0000 | [diff] [blame] | 4551 | // Check if address has undef value. |
| 4552 | if (isa<UndefValue>(Address) || |
| 4553 | (Address->use_empty() && !isa<Argument>(Address))) { |
Eric Christopher | 2441367 | 2012-02-23 03:39:39 +0000 | [diff] [blame] | 4554 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Devang Patel | 3f74a11 | 2010-09-02 21:29:42 +0000 | [diff] [blame] | 4555 | return 0; |
| 4556 | } |
| 4557 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4558 | SDValue &N = NodeMap[Address]; |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4559 | if (!N.getNode() && isa<Argument>(Address)) |
| 4560 | // Check unused arguments map. |
| 4561 | N = UnusedArgNodeMap[Address]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4562 | SDDbgValue *SDV; |
| 4563 | if (N.getNode()) { |
Devang Patel | 8e741ed | 2010-09-02 21:02:27 +0000 | [diff] [blame] | 4564 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 4565 | Address = BCI->getOperand(0); |
Eric Christopher | 178606d | 2012-02-24 01:59:08 +0000 | [diff] [blame] | 4566 | // Parameters are handled specially. |
| 4567 | bool isParameter = |
| 4568 | (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable || |
| 4569 | isa<Argument>(Address)); |
| 4570 | |
Devang Patel | 8e741ed | 2010-09-02 21:02:27 +0000 | [diff] [blame] | 4571 | const AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 4572 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4573 | if (isParameter && !AI) { |
| 4574 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode()); |
| 4575 | if (FINode) |
| 4576 | // Byval parameter. We have a frame index at this point. |
| 4577 | SDV = DAG.getDbgValue(Variable, FINode->getIndex(), |
| 4578 | 0, dl, SDNodeOrder); |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4579 | else { |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4580 | // Address is an argument, so try to emit its dbg value using |
| 4581 | // virtual register info from the FuncInfo.ValueMap. |
| 4582 | EmitFuncArgumentDbgValue(Address, Variable, 0, N); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4583 | return 0; |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4584 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4585 | } else if (AI) |
| 4586 | SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(), |
| 4587 | 0, dl, SDNodeOrder); |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4588 | else { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4589 | // Can't do anything with other non-AI cases yet. |
Eric Christopher | 2441367 | 2012-02-23 03:39:39 +0000 | [diff] [blame] | 4590 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Eric Christopher | 178606d | 2012-02-24 01:59:08 +0000 | [diff] [blame] | 4591 | DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t"); |
| 4592 | DEBUG(Address->dump()); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4593 | return 0; |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4594 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4595 | DAG.AddDbgValue(SDV, N.getNode(), isParameter); |
| 4596 | } else { |
Gabor Greif | fb4032f | 2010-10-01 10:32:19 +0000 | [diff] [blame] | 4597 | // 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] | 4598 | // virtual register info from the FuncInfo.ValueMap. |
Devang Patel | 6cd467b | 2010-08-26 22:53:27 +0000 | [diff] [blame] | 4599 | if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) { |
Devang Patel | 1397fdc | 2010-09-15 14:48:53 +0000 | [diff] [blame] | 4600 | // If variable is pinned by a alloca in dominating bb then |
| 4601 | // use StaticAllocaMap. |
| 4602 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
Devang Patel | 27ede1b | 2010-09-15 18:13:55 +0000 | [diff] [blame] | 4603 | if (AI->getParent() != DI.getParent()) { |
| 4604 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4605 | FuncInfo.StaticAllocaMap.find(AI); |
| 4606 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 4607 | SDV = DAG.getDbgValue(Variable, SI->second, |
| 4608 | 0, dl, SDNodeOrder); |
| 4609 | DAG.AddDbgValue(SDV, 0, false); |
| 4610 | return 0; |
| 4611 | } |
Devang Patel | 1397fdc | 2010-09-15 14:48:53 +0000 | [diff] [blame] | 4612 | } |
| 4613 | } |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 4614 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Devang Patel | 6cd467b | 2010-08-26 22:53:27 +0000 | [diff] [blame] | 4615 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4616 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4617 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4618 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4619 | case Intrinsic::dbg_value: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4620 | const DbgValueInst &DI = cast<DbgValueInst>(I); |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 4621 | DIVariable DIVar(DI.getVariable()); |
| 4622 | assert((!DIVar || DIVar.isVariable()) && |
| 4623 | "Variable in DbgValueInst should be either null or a DIVariable."); |
| 4624 | if (!DIVar) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4625 | return 0; |
| 4626 | |
| 4627 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4628 | uint64_t Offset = DI.getOffset(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4629 | const Value *V = DI.getValue(); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4630 | if (!V) |
| 4631 | return 0; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4632 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4633 | SDDbgValue *SDV; |
Devang Patel | 5787124 | 2011-08-03 23:13:55 +0000 | [diff] [blame] | 4634 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4635 | SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder); |
| 4636 | DAG.AddDbgValue(SDV, 0, false); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4637 | } else { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 4638 | // Do not use getValue() in here; we don't want to generate code at |
| 4639 | // this point if it hasn't been done yet. |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 4640 | SDValue N = NodeMap[V]; |
| 4641 | if (!N.getNode() && isa<Argument>(V)) |
| 4642 | // Check unused arguments map. |
| 4643 | N = UnusedArgNodeMap[V]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4644 | if (N.getNode()) { |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 4645 | if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) { |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4646 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 4647 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 4648 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 4649 | } |
Devang Patel | a778f5c | 2011-02-18 22:43:42 +0000 | [diff] [blame] | 4650 | } else if (!V->use_empty() ) { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 4651 | // Do not call getValue(V) yet, as we don't want to generate code. |
| 4652 | // Remember it for later. |
| 4653 | DanglingDebugInfo DDI(&DI, dl, SDNodeOrder); |
| 4654 | DanglingDebugInfoMap[V] = DDI; |
Devang Patel | 0991dfb | 2010-08-27 22:25:51 +0000 | [diff] [blame] | 4655 | } else { |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4656 | // 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] | 4657 | // data available is an unreferenced parameter. |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 4658 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4659 | } |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | // Build a debug info table entry. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4663 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4664 | V = BCI->getOperand(0); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4665 | const AllocaInst *AI = dyn_cast<AllocaInst>(V); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4666 | // Don't handle byval struct arguments or VLAs, for example. |
Eric Christopher | 7e1e18f | 2012-03-26 06:10:32 +0000 | [diff] [blame] | 4667 | if (!AI) { |
Eric Christopher | 9fc5c83 | 2012-03-28 07:34:36 +0000 | [diff] [blame] | 4668 | DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n"); |
| 4669 | DEBUG(dbgs() << " Last seen at:\n " << *V << "\n"); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4670 | return 0; |
Eric Christopher | 7e1e18f | 2012-03-26 06:10:32 +0000 | [diff] [blame] | 4671 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4672 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4673 | FuncInfo.StaticAllocaMap.find(AI); |
| 4674 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 4675 | return 0; // VLAs. |
| 4676 | int FI = SI->second; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4677 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4678 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 4679 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 4680 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4681 | return 0; |
| 4682 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4683 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4684 | case Intrinsic::eh_typeid_for: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4685 | // Find the type id for the given typeinfo. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4686 | GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0)); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4687 | unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV); |
| 4688 | Res = DAG.getConstant(TypeID, MVT::i32); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4689 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4690 | return 0; |
| 4691 | } |
| 4692 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4693 | case Intrinsic::eh_return_i32: |
| 4694 | case Intrinsic::eh_return_i64: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4695 | DAG.getMachineFunction().getMMI().setCallsEHReturn(true); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4696 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl, |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4697 | MVT::Other, |
| 4698 | getControlRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4699 | getValue(I.getArgOperand(0)), |
| 4700 | getValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4701 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4702 | case Intrinsic::eh_unwind_init: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4703 | DAG.getMachineFunction().getMMI().setCallsUnwindInit(true); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4704 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4705 | case Intrinsic::eh_dwarf_cfa: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4706 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4707 | TLI->getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4708 | SDValue Offset = DAG.getNode(ISD::ADD, sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4709 | TLI->getPointerTy(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4710 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4711 | TLI->getPointerTy()), |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4712 | CfaArg); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4713 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4714 | TLI->getPointerTy(), |
| 4715 | DAG.getConstant(0, TLI->getPointerTy())); |
| 4716 | setValue(&I, DAG.getNode(ISD::ADD, sdl, TLI->getPointerTy(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4717 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4718 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4719 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4720 | case Intrinsic::eh_sjlj_callsite: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4721 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4722 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0)); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4723 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4724 | assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4725 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4726 | MMI.setCurrentCallSite(CI->getZExtValue()); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4727 | return 0; |
| 4728 | } |
Bill Wendling | 6ef9417 | 2011-09-28 03:36:43 +0000 | [diff] [blame] | 4729 | case Intrinsic::eh_sjlj_functioncontext: { |
| 4730 | // Get and store the index of the function context. |
| 4731 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
Bill Wendling | adbf7b2 | 2011-09-28 03:52:41 +0000 | [diff] [blame] | 4732 | AllocaInst *FnCtx = |
| 4733 | cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts()); |
Bill Wendling | 6ef9417 | 2011-09-28 03:36:43 +0000 | [diff] [blame] | 4734 | int FI = FuncInfo.StaticAllocaMap[FnCtx]; |
| 4735 | MFI->setFunctionContextIndex(FI); |
| 4736 | return 0; |
| 4737 | } |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4738 | case Intrinsic::eh_sjlj_setjmp: { |
Bill Wendling | ce370cf | 2011-10-07 21:25:38 +0000 | [diff] [blame] | 4739 | SDValue Ops[2]; |
| 4740 | Ops[0] = getRoot(); |
| 4741 | Ops[1] = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4742 | SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl, |
Bill Wendling | ce370cf | 2011-10-07 21:25:38 +0000 | [diff] [blame] | 4743 | DAG.getVTList(MVT::i32, MVT::Other), |
| 4744 | Ops, 2); |
| 4745 | setValue(&I, Op.getValue(0)); |
| 4746 | DAG.setRoot(Op.getValue(1)); |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4747 | return 0; |
| 4748 | } |
Jim Grosbach | 5eb1951 | 2010-05-22 01:06:18 +0000 | [diff] [blame] | 4749 | case Intrinsic::eh_sjlj_longjmp: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4750 | DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other, |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 4751 | getRoot(), getValue(I.getArgOperand(0)))); |
| 4752 | return 0; |
| 4753 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4754 | |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4755 | case Intrinsic::x86_mmx_pslli_w: |
| 4756 | case Intrinsic::x86_mmx_pslli_d: |
| 4757 | case Intrinsic::x86_mmx_pslli_q: |
| 4758 | case Intrinsic::x86_mmx_psrli_w: |
| 4759 | case Intrinsic::x86_mmx_psrli_d: |
| 4760 | case Intrinsic::x86_mmx_psrli_q: |
| 4761 | case Intrinsic::x86_mmx_psrai_w: |
| 4762 | case Intrinsic::x86_mmx_psrai_d: { |
| 4763 | SDValue ShAmt = getValue(I.getArgOperand(1)); |
| 4764 | if (isa<ConstantSDNode>(ShAmt)) { |
| 4765 | visitTargetIntrinsic(I, Intrinsic); |
| 4766 | return 0; |
| 4767 | } |
| 4768 | unsigned NewIntrinsic = 0; |
| 4769 | EVT ShAmtVT = MVT::v2i32; |
| 4770 | switch (Intrinsic) { |
| 4771 | case Intrinsic::x86_mmx_pslli_w: |
| 4772 | NewIntrinsic = Intrinsic::x86_mmx_psll_w; |
| 4773 | break; |
| 4774 | case Intrinsic::x86_mmx_pslli_d: |
| 4775 | NewIntrinsic = Intrinsic::x86_mmx_psll_d; |
| 4776 | break; |
| 4777 | case Intrinsic::x86_mmx_pslli_q: |
| 4778 | NewIntrinsic = Intrinsic::x86_mmx_psll_q; |
| 4779 | break; |
| 4780 | case Intrinsic::x86_mmx_psrli_w: |
| 4781 | NewIntrinsic = Intrinsic::x86_mmx_psrl_w; |
| 4782 | break; |
| 4783 | case Intrinsic::x86_mmx_psrli_d: |
| 4784 | NewIntrinsic = Intrinsic::x86_mmx_psrl_d; |
| 4785 | break; |
| 4786 | case Intrinsic::x86_mmx_psrli_q: |
| 4787 | NewIntrinsic = Intrinsic::x86_mmx_psrl_q; |
| 4788 | break; |
| 4789 | case Intrinsic::x86_mmx_psrai_w: |
| 4790 | NewIntrinsic = Intrinsic::x86_mmx_psra_w; |
| 4791 | break; |
| 4792 | case Intrinsic::x86_mmx_psrai_d: |
| 4793 | NewIntrinsic = Intrinsic::x86_mmx_psra_d; |
| 4794 | break; |
| 4795 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 4796 | } |
| 4797 | |
| 4798 | // The vector shift intrinsics with scalars uses 32b shift amounts but |
| 4799 | // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits |
| 4800 | // to be zero. |
| 4801 | // We must do this early because v2i32 is not a legal type. |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4802 | SDValue ShOps[2]; |
| 4803 | ShOps[0] = ShAmt; |
| 4804 | ShOps[1] = DAG.getConstant(0, MVT::i32); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4805 | ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4806 | EVT DestVT = TLI->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4807 | ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt); |
| 4808 | Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT, |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4809 | DAG.getConstant(NewIntrinsic, MVT::i32), |
| 4810 | getValue(I.getArgOperand(0)), ShAmt); |
| 4811 | setValue(&I, Res); |
| 4812 | return 0; |
| 4813 | } |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4814 | case Intrinsic::x86_avx_vinsertf128_pd_256: |
| 4815 | case Intrinsic::x86_avx_vinsertf128_ps_256: |
Craig Topper | b45c969 | 2012-04-07 22:32:29 +0000 | [diff] [blame] | 4816 | case Intrinsic::x86_avx_vinsertf128_si_256: |
| 4817 | case Intrinsic::x86_avx2_vinserti128: { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4818 | EVT DestVT = TLI->getValueType(I.getType()); |
| 4819 | EVT ElVT = TLI->getValueType(I.getArgOperand(1)->getType()); |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4820 | uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) * |
| 4821 | ElVT.getVectorNumElements(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4822 | Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT, |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4823 | getValue(I.getArgOperand(0)), |
| 4824 | getValue(I.getArgOperand(1)), |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 4825 | DAG.getConstant(Idx, TLI->getVectorIdxTy())); |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4826 | setValue(&I, Res); |
| 4827 | return 0; |
| 4828 | } |
| 4829 | case Intrinsic::x86_avx_vextractf128_pd_256: |
| 4830 | case Intrinsic::x86_avx_vextractf128_ps_256: |
| 4831 | case Intrinsic::x86_avx_vextractf128_si_256: |
| 4832 | case Intrinsic::x86_avx2_vextracti128: { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4833 | EVT DestVT = TLI->getValueType(I.getType()); |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4834 | uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) * |
| 4835 | DestVT.getVectorNumElements(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4836 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT, |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4837 | getValue(I.getArgOperand(0)), |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 4838 | DAG.getConstant(Idx, TLI->getVectorIdxTy())); |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4839 | setValue(&I, Res); |
| 4840 | return 0; |
| 4841 | } |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4842 | case Intrinsic::convertff: |
| 4843 | case Intrinsic::convertfsi: |
| 4844 | case Intrinsic::convertfui: |
| 4845 | case Intrinsic::convertsif: |
| 4846 | case Intrinsic::convertuif: |
| 4847 | case Intrinsic::convertss: |
| 4848 | case Intrinsic::convertsu: |
| 4849 | case Intrinsic::convertus: |
| 4850 | case Intrinsic::convertuu: { |
| 4851 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4852 | switch (Intrinsic) { |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 4853 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4854 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4855 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4856 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4857 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4858 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4859 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4860 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4861 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4862 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4863 | } |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4864 | EVT DestVT = TLI->getValueType(I.getType()); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4865 | const Value *Op1 = I.getArgOperand(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4866 | Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4867 | DAG.getValueType(DestVT), |
| 4868 | DAG.getValueType(getValue(Op1).getValueType()), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4869 | getValue(I.getArgOperand(1)), |
| 4870 | getValue(I.getArgOperand(2)), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4871 | Code); |
| 4872 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4873 | return 0; |
| 4874 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4875 | case Intrinsic::powi: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4876 | setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4877 | getValue(I.getArgOperand(1)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4878 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4879 | case Intrinsic::log: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4880 | setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4881 | return 0; |
| 4882 | case Intrinsic::log2: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4883 | setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4884 | return 0; |
| 4885 | case Intrinsic::log10: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4886 | setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4887 | return 0; |
| 4888 | case Intrinsic::exp: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4889 | setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4890 | return 0; |
| 4891 | case Intrinsic::exp2: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4892 | setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4893 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4894 | case Intrinsic::pow: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4895 | setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4896 | getValue(I.getArgOperand(1)), DAG, *TLI)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4897 | return 0; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4898 | case Intrinsic::sqrt: |
Peter Collingbourne | b34d3aa | 2012-05-28 21:48:37 +0000 | [diff] [blame] | 4899 | case Intrinsic::fabs: |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4900 | case Intrinsic::sin: |
| 4901 | case Intrinsic::cos: |
Dan Gohman | 27db99f | 2012-07-26 17:43:27 +0000 | [diff] [blame] | 4902 | case Intrinsic::floor: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4903 | case Intrinsic::ceil: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4904 | case Intrinsic::trunc: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4905 | case Intrinsic::rint: |
Hal Finkel | 41418d1 | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 4906 | case Intrinsic::nearbyint: |
| 4907 | case Intrinsic::round: { |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4908 | unsigned Opcode; |
| 4909 | switch (Intrinsic) { |
| 4910 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 4911 | case Intrinsic::sqrt: Opcode = ISD::FSQRT; break; |
| 4912 | case Intrinsic::fabs: Opcode = ISD::FABS; break; |
| 4913 | case Intrinsic::sin: Opcode = ISD::FSIN; break; |
| 4914 | case Intrinsic::cos: Opcode = ISD::FCOS; break; |
| 4915 | case Intrinsic::floor: Opcode = ISD::FFLOOR; break; |
| 4916 | case Intrinsic::ceil: Opcode = ISD::FCEIL; break; |
| 4917 | case Intrinsic::trunc: Opcode = ISD::FTRUNC; break; |
| 4918 | case Intrinsic::rint: Opcode = ISD::FRINT; break; |
| 4919 | case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break; |
Hal Finkel | 41418d1 | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 4920 | case Intrinsic::round: Opcode = ISD::FROUND; break; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4923 | setValue(&I, DAG.getNode(Opcode, sdl, |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4924 | getValue(I.getArgOperand(0)).getValueType(), |
| 4925 | getValue(I.getArgOperand(0)))); |
| 4926 | return 0; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4927 | } |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 4928 | case Intrinsic::fma: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4929 | setValue(&I, DAG.getNode(ISD::FMA, sdl, |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 4930 | getValue(I.getArgOperand(0)).getValueType(), |
| 4931 | getValue(I.getArgOperand(0)), |
| 4932 | getValue(I.getArgOperand(1)), |
| 4933 | getValue(I.getArgOperand(2)))); |
| 4934 | return 0; |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4935 | case Intrinsic::fmuladd: { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4936 | EVT VT = TLI->getValueType(I.getType()); |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 4937 | if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict && |
Stephen Lin | e54885a | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 4938 | TLI->isFMAFasterThanFMulAndFAdd(VT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4939 | setValue(&I, DAG.getNode(ISD::FMA, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4940 | getValue(I.getArgOperand(0)).getValueType(), |
| 4941 | getValue(I.getArgOperand(0)), |
| 4942 | getValue(I.getArgOperand(1)), |
| 4943 | getValue(I.getArgOperand(2)))); |
| 4944 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4945 | SDValue Mul = DAG.getNode(ISD::FMUL, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4946 | getValue(I.getArgOperand(0)).getValueType(), |
| 4947 | getValue(I.getArgOperand(0)), |
| 4948 | getValue(I.getArgOperand(1))); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4949 | SDValue Add = DAG.getNode(ISD::FADD, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4950 | getValue(I.getArgOperand(0)).getValueType(), |
| 4951 | Mul, |
| 4952 | getValue(I.getArgOperand(2))); |
| 4953 | setValue(&I, Add); |
| 4954 | } |
| 4955 | return 0; |
| 4956 | } |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4957 | case Intrinsic::convert_to_fp16: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4958 | setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4959 | MVT::i16, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4960 | return 0; |
| 4961 | case Intrinsic::convert_from_fp16: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4962 | setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4963 | MVT::f32, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4964 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4965 | case Intrinsic::pcmarker: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4966 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4967 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4968 | return 0; |
| 4969 | } |
| 4970 | case Intrinsic::readcyclecounter: { |
| 4971 | SDValue Op = getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4972 | Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl, |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4973 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4974 | &Op, 1); |
| 4975 | setValue(&I, Res); |
| 4976 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4977 | return 0; |
| 4978 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4979 | case Intrinsic::bswap: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4980 | setValue(&I, DAG.getNode(ISD::BSWAP, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4981 | getValue(I.getArgOperand(0)).getValueType(), |
| 4982 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4983 | return 0; |
| 4984 | case Intrinsic::cttz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4985 | SDValue Arg = getValue(I.getArgOperand(0)); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4986 | ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4987 | EVT Ty = Arg.getValueType(); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4988 | setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4989 | sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4990 | return 0; |
| 4991 | } |
| 4992 | case Intrinsic::ctlz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4993 | SDValue Arg = getValue(I.getArgOperand(0)); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4994 | ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4995 | EVT Ty = Arg.getValueType(); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4996 | setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4997 | sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4998 | return 0; |
| 4999 | } |
| 5000 | case Intrinsic::ctpop: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5001 | SDValue Arg = getValue(I.getArgOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5002 | EVT Ty = Arg.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5003 | setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5004 | return 0; |
| 5005 | } |
| 5006 | case Intrinsic::stacksave: { |
| 5007 | SDValue Op = getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5008 | Res = DAG.getNode(ISD::STACKSAVE, sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5009 | DAG.getVTList(TLI->getPointerTy(), MVT::Other), &Op, 1); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5010 | setValue(&I, Res); |
| 5011 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5012 | return 0; |
| 5013 | } |
| 5014 | case Intrinsic::stackrestore: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5015 | Res = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5016 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5017 | return 0; |
| 5018 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 5019 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5020 | // Emit code into the DAG to store the stack guard onto the stack. |
| 5021 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5022 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5023 | EVT PtrTy = TLI->getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5024 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5025 | SDValue Src = getValue(I.getArgOperand(0)); // The guard's value. |
| 5026 | AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5027 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 5028 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5029 | MFI->setStackProtectorIndex(FI); |
| 5030 | |
| 5031 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 5032 | |
| 5033 | // Store the stack protector onto the stack. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5034 | Res = DAG.getStore(getRoot(), sdl, Src, FIN, |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 5035 | MachinePointerInfo::getFixedStack(FI), |
| 5036 | true, false, 0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5037 | setValue(&I, Res); |
| 5038 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5039 | return 0; |
| 5040 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5041 | case Intrinsic::objectsize: { |
| 5042 | // 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] | 5043 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5044 | |
| 5045 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 5046 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5047 | SDValue Arg = getValue(I.getCalledValue()); |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 5048 | EVT Ty = Arg.getValueType(); |
| 5049 | |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 5050 | if (CI->isZero()) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5051 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5052 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5053 | Res = DAG.getConstant(0, Ty); |
| 5054 | |
| 5055 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5056 | return 0; |
| 5057 | } |
Justin Holewinski | c2b7f5f | 2013-05-21 14:37:16 +0000 | [diff] [blame] | 5058 | case Intrinsic::annotation: |
| 5059 | case Intrinsic::ptr_annotation: |
| 5060 | // Drop the intrinsic, but forward the value |
| 5061 | setValue(&I, getValue(I.getOperand(0))); |
| 5062 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5063 | case Intrinsic::var_annotation: |
| 5064 | // Discard annotate attributes |
| 5065 | return 0; |
| 5066 | |
| 5067 | case Intrinsic::init_trampoline: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5068 | const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5069 | |
| 5070 | SDValue Ops[6]; |
| 5071 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5072 | Ops[1] = getValue(I.getArgOperand(0)); |
| 5073 | Ops[2] = getValue(I.getArgOperand(1)); |
| 5074 | Ops[3] = getValue(I.getArgOperand(2)); |
| 5075 | Ops[4] = DAG.getSrcValue(I.getArgOperand(0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5076 | Ops[5] = DAG.getSrcValue(F); |
| 5077 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5078 | Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5079 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5080 | DAG.setRoot(Res); |
| 5081 | return 0; |
| 5082 | } |
| 5083 | case Intrinsic::adjust_trampoline: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5084 | setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5085 | TLI->getPointerTy(), |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5086 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5087 | return 0; |
| 5088 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5089 | case Intrinsic::gcroot: |
| 5090 | if (GFI) { |
Bill Wendling | 95dd442 | 2012-05-01 22:50:45 +0000 | [diff] [blame] | 5091 | const Value *Alloca = I.getArgOperand(0)->stripPointerCasts(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5092 | const Constant *TypeMap = cast<Constant>(I.getArgOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5093 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5094 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 5095 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 5096 | } |
| 5097 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5098 | case Intrinsic::gcread: |
| 5099 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5100 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5101 | case Intrinsic::flt_rounds: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5102 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5103 | return 0; |
Jakub Staszak | 9da9934 | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 5104 | |
| 5105 | case Intrinsic::expect: { |
| 5106 | // Just replace __builtin_expect(exp, c) with EXP. |
| 5107 | setValue(&I, getValue(I.getArgOperand(0))); |
| 5108 | return 0; |
| 5109 | } |
| 5110 | |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5111 | case Intrinsic::debugtrap: |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5112 | case Intrinsic::trap: { |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5113 | StringRef TrapFuncName = TM.Options.getTrapFunctionName(); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5114 | if (TrapFuncName.empty()) { |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 5115 | ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ? |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5116 | ISD::TRAP : ISD::DEBUGTRAP; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5117 | DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot())); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5118 | return 0; |
| 5119 | } |
| 5120 | TargetLowering::ArgListTy Args; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5121 | TargetLowering:: |
| 5122 | CallLoweringInfo CLI(getRoot(), I.getType(), |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5123 | false, false, false, false, 0, CallingConv::C, |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 5124 | /*isTailCall=*/false, |
| 5125 | /*doesNotRet=*/false, /*isReturnValueUsed=*/true, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5126 | DAG.getExternalSymbol(TrapFuncName.data(), |
| 5127 | TLI->getPointerTy()), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5128 | Args, DAG, sdl); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5129 | std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5130 | DAG.setRoot(Result.second); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5131 | return 0; |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5132 | } |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5133 | |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 5134 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5135 | case Intrinsic::sadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5136 | case Intrinsic::usub_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5137 | case Intrinsic::ssub_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5138 | case Intrinsic::umul_with_overflow: |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5139 | case Intrinsic::smul_with_overflow: { |
| 5140 | ISD::NodeType Op; |
| 5141 | switch (Intrinsic) { |
| 5142 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 5143 | case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break; |
| 5144 | case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break; |
| 5145 | case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break; |
| 5146 | case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break; |
| 5147 | case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break; |
| 5148 | case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break; |
| 5149 | } |
| 5150 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 5151 | SDValue Op2 = getValue(I.getArgOperand(1)); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 5152 | |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5153 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5154 | setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2)); |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5155 | return 0; |
| 5156 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5157 | case Intrinsic::prefetch: { |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5158 | SDValue Ops[5]; |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5159 | unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5160 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5161 | Ops[1] = getValue(I.getArgOperand(0)); |
| 5162 | Ops[2] = getValue(I.getArgOperand(1)); |
| 5163 | Ops[3] = getValue(I.getArgOperand(2)); |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5164 | Ops[4] = getValue(I.getArgOperand(3)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5165 | DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl, |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5166 | DAG.getVTList(MVT::Other), |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5167 | &Ops[0], 5, |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5168 | EVT::getIntegerVT(*Context, 8), |
| 5169 | MachinePointerInfo(I.getArgOperand(0)), |
| 5170 | 0, /* align */ |
| 5171 | false, /* volatile */ |
| 5172 | rw==0, /* read */ |
| 5173 | rw==1)); /* write */ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5174 | return 0; |
| 5175 | } |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5176 | case Intrinsic::lifetime_start: |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5177 | case Intrinsic::lifetime_end: { |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5178 | bool IsStart = (Intrinsic == Intrinsic::lifetime_start); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5179 | // Stack coloring is not enabled in O0, discard region information. |
| 5180 | if (TM.getOptLevel() == CodeGenOpt::None) |
| 5181 | return 0; |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5182 | |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5183 | SmallVector<Value *, 4> Allocas; |
| 5184 | GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD); |
| 5185 | |
Craig Topper | f22fd3f | 2013-07-03 05:11:49 +0000 | [diff] [blame] | 5186 | for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(), |
| 5187 | E = Allocas.end(); Object != E; ++Object) { |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5188 | AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object); |
| 5189 | |
| 5190 | // Could not find an Alloca. |
| 5191 | if (!LifetimeObject) |
| 5192 | continue; |
| 5193 | |
| 5194 | int FI = FuncInfo.StaticAllocaMap[LifetimeObject]; |
| 5195 | |
| 5196 | SDValue Ops[2]; |
| 5197 | Ops[0] = getRoot(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5198 | Ops[1] = DAG.getFrameIndex(FI, TLI->getPointerTy(), true); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5199 | unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END); |
| 5200 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5201 | Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5202 | DAG.setRoot(Res); |
| 5203 | } |
Nadav Rotem | 5882e56 | 2013-02-01 19:25:23 +0000 | [diff] [blame] | 5204 | return 0; |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5205 | } |
| 5206 | case Intrinsic::invariant_start: |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5207 | // Discard region information. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5208 | setValue(&I, DAG.getUNDEF(TLI->getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5209 | return 0; |
| 5210 | case Intrinsic::invariant_end: |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5211 | // Discard region information. |
| 5212 | return 0; |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 5213 | case Intrinsic::donothing: |
| 5214 | // ignore |
| 5215 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5216 | } |
| 5217 | } |
| 5218 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5219 | void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5220 | bool isTailCall, |
| 5221 | MachineBasicBlock *LandingPad) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5222 | PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 5223 | FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 5224 | Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5225 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 5226 | MCSymbol *BeginLabel = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5227 | |
| 5228 | TargetLowering::ArgListTy Args; |
| 5229 | TargetLowering::ArgListEntry Entry; |
| 5230 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5231 | |
| 5232 | // Check whether the function can return without sret-demotion. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 5233 | SmallVector<ISD::OutputArg, 4> Outs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5234 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 5235 | GetReturnInfo(RetTy, CS.getAttributes(), Outs, *TLI); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5236 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5237 | bool CanLowerReturn = TLI->CanLowerReturn(CS.getCallingConv(), |
| 5238 | DAG.getMachineFunction(), |
| 5239 | FTy->isVarArg(), Outs, |
| 5240 | FTy->getContext()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5241 | |
| 5242 | SDValue DemoteStackSlot; |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5243 | int DemoteStackIdx = -100; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5244 | |
| 5245 | if (!CanLowerReturn) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5246 | uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize( |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5247 | FTy->getReturnType()); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5248 | unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment( |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5249 | FTy->getReturnType()); |
| 5250 | MachineFunction &MF = DAG.getMachineFunction(); |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5251 | DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5252 | Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5253 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5254 | DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI->getPointerTy()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5255 | Entry.Node = DemoteStackSlot; |
| 5256 | Entry.Ty = StackSlotPtrType; |
| 5257 | Entry.isSExt = false; |
| 5258 | Entry.isZExt = false; |
| 5259 | Entry.isInReg = false; |
| 5260 | Entry.isSRet = true; |
| 5261 | Entry.isNest = false; |
| 5262 | Entry.isByVal = false; |
Stephen Lin | 456ca04 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 5263 | Entry.isReturned = false; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5264 | Entry.Alignment = Align; |
| 5265 | Args.push_back(Entry); |
| 5266 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 5267 | } |
| 5268 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5269 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5270 | i != e; ++i) { |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 5271 | const Value *V = *i; |
| 5272 | |
| 5273 | // Skip empty types |
| 5274 | if (V->getType()->isEmptyTy()) |
| 5275 | continue; |
| 5276 | |
| 5277 | SDValue ArgNode = getValue(V); |
| 5278 | Entry.Node = ArgNode; Entry.Ty = V->getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5279 | |
| 5280 | unsigned attrInd = i - CS.arg_begin() + 1; |
Stephen Lin | 456ca04 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 5281 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 5282 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 5283 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 5284 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 5285 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 5286 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
| 5287 | Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned); |
| 5288 | Entry.Alignment = CS.getParamAlignment(attrInd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5289 | Args.push_back(Entry); |
| 5290 | } |
| 5291 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5292 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5293 | // Insert a label before the invoke call to mark the try range. This can be |
| 5294 | // used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5295 | BeginLabel = MMI.getContext().CreateTempSymbol(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 5296 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5297 | // For SjLj, keep track of which landing pads go with which invokes |
| 5298 | // so as to maintain the ordering of pads in the LSDA. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5299 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5300 | if (CallSiteIndex) { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5301 | MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
Bill Wendling | 30e6740 | 2011-10-05 22:24:35 +0000 | [diff] [blame] | 5302 | LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex); |
Bill Wendling | a8512ed | 2011-10-04 22:00:35 +0000 | [diff] [blame] | 5303 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5304 | // Now that the call site is handled, stop tracking it. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5305 | MMI.setCurrentCallSite(0); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5308 | // Both PendingLoads and PendingExports must be flushed here; |
| 5309 | // this call might not return. |
| 5310 | (void)getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5311 | DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5314 | // Check if target-independent constraints permit a tail call here. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5315 | // Target-dependent constraints are checked within TLI->LowerCallTo. |
| 5316 | if (isTailCall && !isInTailCallPosition(CS, *TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5317 | isTailCall = false; |
| 5318 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5319 | TargetLowering:: |
| 5320 | CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5321 | getCurSDLoc(), CS); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5322 | std::pair<SDValue,SDValue> Result = TLI->LowerCallTo(CLI); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5323 | assert((isTailCall || Result.second.getNode()) && |
| 5324 | "Non-null chain expected with non-tail call!"); |
| 5325 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 5326 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5327 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5328 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5329 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5330 | // The instruction result is the result of loading from the |
| 5331 | // hidden sret parameter. |
| 5332 | SmallVector<EVT, 1> PVTs; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5333 | Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5334 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5335 | ComputeValueVTs(*TLI, PtrRetTy, PVTs); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5336 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 5337 | EVT PtrVT = PVTs[0]; |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5338 | |
| 5339 | SmallVector<EVT, 4> RetTys; |
| 5340 | SmallVector<uint64_t, 4> Offsets; |
| 5341 | RetTy = FTy->getReturnType(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5342 | ComputeValueVTs(*TLI, RetTy, RetTys, &Offsets); |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5343 | |
| 5344 | unsigned NumValues = RetTys.size(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5345 | SmallVector<SDValue, 4> Values(NumValues); |
| 5346 | SmallVector<SDValue, 4> Chains(NumValues); |
| 5347 | |
| 5348 | for (unsigned i = 0; i < NumValues; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5349 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5350 | DemoteStackSlot, |
| 5351 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5352 | SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5353 | MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5354 | false, false, false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5355 | Values[i] = L; |
| 5356 | Chains[i] = L.getValue(1); |
| 5357 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5358 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5359 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5360 | MVT::Other, &Chains[0], NumValues); |
| 5361 | PendingLoads.push_back(Chain); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5362 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5363 | setValue(CS.getInstruction(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5364 | DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5365 | DAG.getVTList(&RetTys[0], RetTys.size()), |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5366 | &Values[0], Values.size())); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5367 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5368 | |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5369 | if (!Result.second.getNode()) { |
Evan Cheng | c249e48 | 2011-04-01 19:57:01 +0000 | [diff] [blame] | 5370 | // As a special case, a null chain means that a tail call has been emitted and |
| 5371 | // the DAG root is already updated. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5372 | HasTailCall = true; |
Tim Northover | e5a81a1 | 2013-07-06 12:58:45 +0000 | [diff] [blame] | 5373 | |
| 5374 | // Since there's no actual continuation from this block, nothing can be |
| 5375 | // relying on us setting vregs for them. |
| 5376 | PendingExports.clear(); |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5377 | } else { |
| 5378 | DAG.setRoot(Result.second); |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5379 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5380 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5381 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5382 | // Insert a label at the end of the invoke call to mark the try range. This |
| 5383 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5384 | MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5385 | DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5386 | |
| 5387 | // Inform MachineModuleInfo of range. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5388 | MMI.addInvoke(LandingPad, BeginLabel, EndLabel); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5389 | } |
| 5390 | } |
| 5391 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5392 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 5393 | /// value is equal or not-equal to zero. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5394 | static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) { |
| 5395 | 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] | 5396 | UI != E; ++UI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5397 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5398 | if (IC->isEquality()) |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5399 | if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5400 | if (C->isNullValue()) |
| 5401 | continue; |
| 5402 | // Unknown instruction. |
| 5403 | return false; |
| 5404 | } |
| 5405 | return true; |
| 5406 | } |
| 5407 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5408 | static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5409 | Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5410 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5411 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5412 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 5413 | // input is from a string literal. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5414 | if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5415 | // Cast pointer to the type we really want to load. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5416 | LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput), |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5417 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5418 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5419 | if (const Constant *LoadCst = |
| 5420 | ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput), |
| 5421 | Builder.TD)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5422 | return Builder.getValue(LoadCst); |
| 5423 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5424 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5425 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 5426 | // still constant memory, the input chain can be the entry node. |
| 5427 | SDValue Root; |
| 5428 | bool ConstantMemory = false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5429 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5430 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 5431 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 5432 | Root = Builder.DAG.getEntryNode(); |
| 5433 | ConstantMemory = true; |
| 5434 | } else { |
| 5435 | // Do not serialize non-volatile loads against each other. |
| 5436 | Root = Builder.DAG.getRoot(); |
| 5437 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5438 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5439 | SDValue Ptr = Builder.getValue(PtrVal); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5440 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5441 | Ptr, MachinePointerInfo(PtrVal), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5442 | false /*volatile*/, |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 5443 | false /*nontemporal*/, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5444 | false /*isinvariant*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5445 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5446 | if (!ConstantMemory) |
| 5447 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 5448 | return LoadVal; |
| 5449 | } |
| 5450 | |
| 5451 | |
| 5452 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 5453 | /// If so, return true and lower it, otherwise return false and it will be |
| 5454 | /// lowered like a normal call. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5455 | bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5456 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 5457 | if (I.getNumArgOperands() != 3) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5458 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5459 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5460 | const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 5461 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5462 | !I.getArgOperand(2)->getType()->isIntegerTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 5463 | !I.getType()->isIntegerTy()) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5464 | return false; |
| 5465 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5466 | const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2)); |
Richard Sandiford | ac168b8 | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5467 | if (Size && Size->getZExtValue() == 0) { |
| 5468 | EVT CallVT = TM.getTargetLowering()->getValueType(I.getType(), true); |
| 5469 | setValue(&I, DAG.getConstant(0, CallVT)); |
| 5470 | return true; |
| 5471 | } |
| 5472 | |
| 5473 | const Value *Arg0 = I.getArgOperand(0); |
| 5474 | const Value *Arg1 = I.getArgOperand(1); |
| 5475 | const Value *Arg2 = I.getArgOperand(2); |
| 5476 | const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo(); |
| 5477 | std::pair<SDValue, SDValue> Res = |
| 5478 | TSI.EmitTargetCodeForMemcmp(DAG, getCurSDLoc(), DAG.getRoot(), |
| 5479 | getValue(Arg0), getValue(Arg1), getValue(Arg2), |
| 5480 | MachinePointerInfo(Arg0), |
| 5481 | MachinePointerInfo(Arg1)); |
| 5482 | if (Res.first.getNode()) { |
| 5483 | setValue(&I, Res.first); |
| 5484 | DAG.setRoot(Res.second); |
| 5485 | return true; |
| 5486 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5487 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5488 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 5489 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5490 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 5491 | bool ActuallyDoIt = true; |
| 5492 | MVT LoadVT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5493 | Type *LoadTy; |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5494 | switch (Size->getZExtValue()) { |
| 5495 | default: |
| 5496 | LoadVT = MVT::Other; |
| 5497 | LoadTy = 0; |
| 5498 | ActuallyDoIt = false; |
| 5499 | break; |
| 5500 | case 2: |
| 5501 | LoadVT = MVT::i16; |
| 5502 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 5503 | break; |
| 5504 | case 4: |
| 5505 | LoadVT = MVT::i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5506 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5507 | break; |
| 5508 | case 8: |
| 5509 | LoadVT = MVT::i64; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5510 | LoadTy = Type::getInt64Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5511 | break; |
| 5512 | /* |
| 5513 | case 16: |
| 5514 | LoadVT = MVT::v4i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5515 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5516 | LoadTy = VectorType::get(LoadTy, 4); |
| 5517 | break; |
| 5518 | */ |
| 5519 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5520 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5521 | // This turns into unaligned loads. We only do this if the target natively |
| 5522 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 5523 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5524 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5525 | // Require that we can find a legal MVT, and only do this if the target |
| 5526 | // supports unaligned loads of that type. Expanding into byte loads would |
| 5527 | // bloat the code. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5528 | const TargetLowering *TLI = TM.getTargetLowering(); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5529 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 5530 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 5531 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5532 | if (!TLI->isTypeLegal(LoadVT) ||!TLI->allowsUnalignedMemoryAccesses(LoadVT)) |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5533 | ActuallyDoIt = false; |
| 5534 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5535 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5536 | if (ActuallyDoIt) { |
| 5537 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 5538 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5539 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5540 | SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal, |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5541 | ISD::SETNE); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5542 | EVT CallVT = TLI->getValueType(I.getType(), true); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5543 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurSDLoc(), CallVT)); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5544 | return true; |
| 5545 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5546 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5547 | |
| 5548 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5549 | return false; |
| 5550 | } |
| 5551 | |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5552 | /// visitUnaryFloatCall - If a call instruction is a unary floating-point |
| 5553 | /// operation (as expected), translate it to an SDNode with the specified opcode |
| 5554 | /// and return true. |
| 5555 | bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I, |
| 5556 | unsigned Opcode) { |
| 5557 | // Sanity check that it really is a unary floating-point call. |
| 5558 | if (I.getNumArgOperands() != 1 || |
| 5559 | !I.getArgOperand(0)->getType()->isFloatingPointTy() || |
| 5560 | I.getType() != I.getArgOperand(0)->getType() || |
| 5561 | !I.onlyReadsMemory()) |
| 5562 | return false; |
| 5563 | |
| 5564 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5565 | setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp)); |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5566 | return true; |
| 5567 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5568 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5569 | void SelectionDAGBuilder::visitCall(const CallInst &I) { |
Chris Lattner | 598751e | 2010-07-05 05:36:21 +0000 | [diff] [blame] | 5570 | // Handle inline assembly differently. |
| 5571 | if (isa<InlineAsm>(I.getCalledValue())) { |
| 5572 | visitInlineAsm(&I); |
| 5573 | return; |
| 5574 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5575 | |
Michael J. Spencer | 391b43b | 2010-10-21 20:49:23 +0000 | [diff] [blame] | 5576 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Michael J. Spencer | c9c137b | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 5577 | ComputeUsesVAFloatArgument(I, &MMI); |
Michael J. Spencer | 391b43b | 2010-10-21 20:49:23 +0000 | [diff] [blame] | 5578 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5579 | const char *RenameFn = 0; |
| 5580 | if (Function *F = I.getCalledFunction()) { |
| 5581 | if (F->isDeclaration()) { |
Chris Lattner | 598751e | 2010-07-05 05:36:21 +0000 | [diff] [blame] | 5582 | if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 5583 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 5584 | RenameFn = visitIntrinsicCall(I, IID); |
| 5585 | if (!RenameFn) |
| 5586 | return; |
| 5587 | } |
| 5588 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5589 | if (unsigned IID = F->getIntrinsicID()) { |
| 5590 | RenameFn = visitIntrinsicCall(I, IID); |
| 5591 | if (!RenameFn) |
| 5592 | return; |
| 5593 | } |
| 5594 | } |
| 5595 | |
| 5596 | // Check for well-known libc/libm calls. If the function is internal, it |
| 5597 | // can't be a library call. |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5598 | LibFunc::Func Func; |
| 5599 | if (!F->hasLocalLinkage() && F->hasName() && |
| 5600 | LibInfo->getLibFunc(F->getName(), Func) && |
| 5601 | LibInfo->hasOptimizedCodeGen(Func)) { |
| 5602 | switch (Func) { |
| 5603 | default: break; |
| 5604 | case LibFunc::copysign: |
| 5605 | case LibFunc::copysignf: |
| 5606 | case LibFunc::copysignl: |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 5607 | if (I.getNumArgOperands() == 2 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5608 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 5609 | I.getType() == I.getArgOperand(0)->getType() && |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5610 | I.getType() == I.getArgOperand(1)->getType() && |
| 5611 | I.onlyReadsMemory()) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5612 | SDValue LHS = getValue(I.getArgOperand(0)); |
| 5613 | SDValue RHS = getValue(I.getArgOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5614 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(), |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 5615 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5616 | return; |
| 5617 | } |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5618 | break; |
| 5619 | case LibFunc::fabs: |
| 5620 | case LibFunc::fabsf: |
| 5621 | case LibFunc::fabsl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5622 | if (visitUnaryFloatCall(I, ISD::FABS)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5623 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5624 | break; |
| 5625 | case LibFunc::sin: |
| 5626 | case LibFunc::sinf: |
| 5627 | case LibFunc::sinl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5628 | if (visitUnaryFloatCall(I, ISD::FSIN)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5629 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5630 | break; |
| 5631 | case LibFunc::cos: |
| 5632 | case LibFunc::cosf: |
| 5633 | case LibFunc::cosl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5634 | if (visitUnaryFloatCall(I, ISD::FCOS)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5635 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5636 | break; |
| 5637 | case LibFunc::sqrt: |
| 5638 | case LibFunc::sqrtf: |
| 5639 | case LibFunc::sqrtl: |
Preston Gurd | b704d23 | 2013-05-27 15:44:35 +0000 | [diff] [blame] | 5640 | case LibFunc::sqrt_finite: |
| 5641 | case LibFunc::sqrtf_finite: |
| 5642 | case LibFunc::sqrtl_finite: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5643 | if (visitUnaryFloatCall(I, ISD::FSQRT)) |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 5644 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5645 | break; |
| 5646 | case LibFunc::floor: |
| 5647 | case LibFunc::floorf: |
| 5648 | case LibFunc::floorl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5649 | if (visitUnaryFloatCall(I, ISD::FFLOOR)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5650 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5651 | break; |
| 5652 | case LibFunc::nearbyint: |
| 5653 | case LibFunc::nearbyintf: |
| 5654 | case LibFunc::nearbyintl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5655 | if (visitUnaryFloatCall(I, ISD::FNEARBYINT)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5656 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5657 | break; |
| 5658 | case LibFunc::ceil: |
| 5659 | case LibFunc::ceilf: |
| 5660 | case LibFunc::ceill: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5661 | if (visitUnaryFloatCall(I, ISD::FCEIL)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5662 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5663 | break; |
| 5664 | case LibFunc::rint: |
| 5665 | case LibFunc::rintf: |
| 5666 | case LibFunc::rintl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5667 | if (visitUnaryFloatCall(I, ISD::FRINT)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5668 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5669 | break; |
Hal Finkel | 41418d1 | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 5670 | case LibFunc::round: |
| 5671 | case LibFunc::roundf: |
| 5672 | case LibFunc::roundl: |
| 5673 | if (visitUnaryFloatCall(I, ISD::FROUND)) |
| 5674 | return; |
| 5675 | break; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5676 | case LibFunc::trunc: |
| 5677 | case LibFunc::truncf: |
| 5678 | case LibFunc::truncl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5679 | if (visitUnaryFloatCall(I, ISD::FTRUNC)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5680 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5681 | break; |
| 5682 | case LibFunc::log2: |
| 5683 | case LibFunc::log2f: |
| 5684 | case LibFunc::log2l: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5685 | if (visitUnaryFloatCall(I, ISD::FLOG2)) |
Owen Anderson | 4e0adfa | 2011-12-15 00:54:12 +0000 | [diff] [blame] | 5686 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5687 | break; |
| 5688 | case LibFunc::exp2: |
| 5689 | case LibFunc::exp2f: |
| 5690 | case LibFunc::exp2l: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5691 | if (visitUnaryFloatCall(I, ISD::FEXP2)) |
Owen Anderson | 4e0adfa | 2011-12-15 00:54:12 +0000 | [diff] [blame] | 5692 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5693 | break; |
| 5694 | case LibFunc::memcmp: |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5695 | if (visitMemCmpCall(I)) |
| 5696 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5697 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5698 | } |
| 5699 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5700 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5701 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5702 | SDValue Callee; |
| 5703 | if (!RenameFn) |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5704 | Callee = getValue(I.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5705 | else |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5706 | Callee = DAG.getExternalSymbol(RenameFn, |
| 5707 | TM.getTargetLowering()->getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5708 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 5709 | // Check if we can potentially perform a tail call. More detailed checking is |
| 5710 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 5711 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5714 | namespace { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5715 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5716 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 5717 | /// lowering. |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5718 | class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 5719 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5720 | /// CallOperand - If this is the result output operand or a clobber |
| 5721 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 5722 | /// This gets modified as the asm is processed. |
| 5723 | SDValue CallOperand; |
| 5724 | |
| 5725 | /// AssignedRegs - If this is a register or register class operand, this |
| 5726 | /// contains the set of register corresponding to the operand. |
| 5727 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5728 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5729 | explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5730 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 5731 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5732 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5733 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5734 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5735 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5736 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5737 | const TargetLowering &TLI, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5738 | const DataLayout *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5739 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5740 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5741 | if (isa<BasicBlock>(CallOperandVal)) |
| 5742 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5743 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5744 | llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5745 | |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5746 | // FIXME: code duplicated from TargetLowering::ParseConstraints(). |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5747 | // If this is an indirect operand, the operand is a pointer to the |
| 5748 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5749 | if (isIndirect) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5750 | llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5751 | if (!PtrTy) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5752 | report_fatal_error("Indirect operand for inline asm not a pointer!"); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5753 | OpTy = PtrTy->getElementType(); |
| 5754 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5755 | |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5756 | // Look for vector wrapped in a struct. e.g. { <16 x i8> }. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5757 | if (StructType *STy = dyn_cast<StructType>(OpTy)) |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5758 | if (STy->getNumElements() == 1) |
| 5759 | OpTy = STy->getElementType(0); |
| 5760 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5761 | // If OpTy is not a single value, it may be a struct/union that we |
| 5762 | // can tile with integers. |
| 5763 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 5764 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 5765 | switch (BitSize) { |
| 5766 | default: break; |
| 5767 | case 1: |
| 5768 | case 8: |
| 5769 | case 16: |
| 5770 | case 32: |
| 5771 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 5772 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5773 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5774 | break; |
| 5775 | } |
| 5776 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5777 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5778 | return TLI.getValueType(OpTy, true); |
| 5779 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5780 | }; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5781 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 5782 | typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector; |
| 5783 | |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5784 | } // end anonymous namespace |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5785 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5786 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 5787 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 5788 | /// register allocator to handle the assignment process. However, if the asm |
| 5789 | /// 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] | 5790 | /// allocation. This produces generally horrible, but correct, code. |
| 5791 | /// |
| 5792 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5793 | /// |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5794 | static void GetRegistersForValue(SelectionDAG &DAG, |
| 5795 | const TargetLowering &TLI, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5796 | SDLoc DL, |
Benjamin Kramer | 8b93ff2 | 2012-02-24 14:01:17 +0000 | [diff] [blame] | 5797 | SDISelAsmOperandInfo &OpInfo) { |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5798 | LLVMContext &Context = *DAG.getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5799 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5800 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5801 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5802 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5803 | // If this is a constraint for a single physreg, or a constraint for a |
| 5804 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5805 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5806 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5807 | OpInfo.ConstraintVT); |
| 5808 | |
| 5809 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5810 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5811 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5812 | // cast of the input value. More generally, handle any case where the input |
| 5813 | // value disagrees with the register class we plan to stick this in. |
| 5814 | if (OpInfo.Type == InlineAsm::isInput && |
| 5815 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5816 | // 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] | 5817 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5818 | // vector types). |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5819 | MVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5820 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5821 | OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5822 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5823 | OpInfo.ConstraintVT = RegVT; |
| 5824 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5825 | // If the input is a FP value and we want it in FP registers, do a |
| 5826 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5827 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5828 | // machine. |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5829 | RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits()); |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5830 | OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5831 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5832 | OpInfo.ConstraintVT = RegVT; |
| 5833 | } |
| 5834 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5835 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5836 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5837 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5838 | |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5839 | MVT RegVT; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5840 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5841 | |
| 5842 | // If this is a constraint for a specific physical register, like {r17}, |
| 5843 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5844 | if (unsigned AssignedReg = PhysReg.first) { |
| 5845 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5846 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5847 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5848 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5849 | // Get the actual register value type. This is important, because the user |
| 5850 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5851 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5852 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5853 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5854 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5855 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5856 | |
| 5857 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5858 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5859 | TargetRegisterClass::iterator I = RC->begin(); |
| 5860 | for (; *I != AssignedReg; ++I) |
| 5861 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5862 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5863 | // Already added the first reg. |
| 5864 | --NumRegs; ++I; |
| 5865 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5866 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5867 | Regs.push_back(*I); |
| 5868 | } |
| 5869 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5870 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5871 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5872 | return; |
| 5873 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5874 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5875 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5876 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5877 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5878 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5879 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5880 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5881 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5882 | // Create the appropriate number of virtual registers. |
| 5883 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5884 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5885 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5886 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5887 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5888 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5889 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5890 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5891 | // Otherwise, we couldn't allocate enough registers for this. |
| 5892 | } |
| 5893 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5894 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5895 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5896 | void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) { |
| 5897 | const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5898 | |
| 5899 | /// ConstraintOperands - Information about all of the constraints. |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 5900 | SDISelAsmOperandInfoVector ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5901 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5902 | const TargetLowering *TLI = TM.getTargetLowering(); |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5903 | TargetLowering::AsmOperandInfoVector |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5904 | TargetConstraints = TLI->ParseConstraints(CS); |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5905 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5906 | bool hasMemory = false; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5907 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5908 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5909 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5910 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 5911 | ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i])); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5912 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5913 | |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5914 | MVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5915 | |
| 5916 | // Compute the value type for each operand. |
| 5917 | switch (OpInfo.Type) { |
| 5918 | case InlineAsm::isOutput: |
| 5919 | // Indirect outputs just consume an argument. |
| 5920 | if (OpInfo.isIndirect) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5921 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5922 | break; |
| 5923 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5924 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5925 | // The return value of the call is this value. As such, there is no |
| 5926 | // corresponding argument. |
Nick Lewycky | 8de3400 | 2011-09-30 22:19:53 +0000 | [diff] [blame] | 5927 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5928 | if (StructType *STy = dyn_cast<StructType>(CS.getType())) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5929 | OpVT = TLI->getSimpleValueType(STy->getElementType(ResNo)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5930 | } else { |
| 5931 | assert(ResNo == 0 && "Asm only has one result!"); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5932 | OpVT = TLI->getSimpleValueType(CS.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5933 | } |
| 5934 | ++ResNo; |
| 5935 | break; |
| 5936 | case InlineAsm::isInput: |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5937 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5938 | break; |
| 5939 | case InlineAsm::isClobber: |
| 5940 | // Nothing to do. |
| 5941 | break; |
| 5942 | } |
| 5943 | |
| 5944 | // If this is an input or an indirect output, process the call argument. |
| 5945 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5946 | if (OpInfo.CallOperandVal) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5947 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5948 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5949 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5950 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5951 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5952 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5953 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), *TLI, TD). |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5954 | getSimpleVT(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5955 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5956 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5957 | OpInfo.ConstraintVT = OpVT; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5958 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5959 | // Indirect operand accesses access memory. |
| 5960 | if (OpInfo.isIndirect) |
| 5961 | hasMemory = true; |
| 5962 | else { |
| 5963 | for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) { |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5964 | TargetLowering::ConstraintType |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5965 | CType = TLI->getConstraintType(OpInfo.Codes[j]); |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5966 | if (CType == TargetLowering::C_Memory) { |
| 5967 | hasMemory = true; |
| 5968 | break; |
| 5969 | } |
| 5970 | } |
| 5971 | } |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5972 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5973 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5974 | SDValue Chain, Flag; |
| 5975 | |
| 5976 | // We won't need to flush pending loads if this asm doesn't touch |
| 5977 | // memory and is nonvolatile. |
| 5978 | if (hasMemory || IA->hasSideEffects()) |
| 5979 | Chain = getRoot(); |
| 5980 | else |
| 5981 | Chain = DAG.getRoot(); |
| 5982 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5983 | // Second pass over the constraints: compute which constraint option to use |
| 5984 | // and assign registers to constraints that want a specific physreg. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5985 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5986 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5987 | |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5988 | // If this is an output operand with a matching input operand, look up the |
| 5989 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5990 | // other is floating point, or their sizes are different, flag it as an |
| 5991 | // error. |
| 5992 | if (OpInfo.hasMatchingInput()) { |
| 5993 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5994 | |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5995 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 5996 | std::pair<unsigned, const TargetRegisterClass*> MatchRC = |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5997 | TLI->getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5998 | OpInfo.ConstraintVT); |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 5999 | std::pair<unsigned, const TargetRegisterClass*> InputRC = |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6000 | TLI->getRegForInlineAsmConstraint(Input.ConstraintCode, |
| 6001 | Input.ConstraintVT); |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 6002 | if ((OpInfo.ConstraintVT.isInteger() != |
| 6003 | Input.ConstraintVT.isInteger()) || |
Eric Christopher | 5427ede | 2011-07-14 20:13:52 +0000 | [diff] [blame] | 6004 | (MatchRC.second != InputRC.second)) { |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 6005 | report_fatal_error("Unsupported asm: input constraint" |
| 6006 | " with a matching output constraint of" |
| 6007 | " incompatible type!"); |
| 6008 | } |
| 6009 | Input.ConstraintVT = OpInfo.ConstraintVT; |
| 6010 | } |
| 6011 | } |
| 6012 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6013 | // Compute the constraint code and ConstraintType to use. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6014 | TLI->ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6015 | |
Eric Christopher | fffe363 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 6016 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 6017 | OpInfo.Type == InlineAsm::isClobber) |
| 6018 | continue; |
| 6019 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6020 | // If this is a memory input, and if the operand is not indirect, do what we |
| 6021 | // need to to provide an address for the memory input. |
| 6022 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 6023 | !OpInfo.isIndirect) { |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 6024 | assert((OpInfo.isMultipleAlternative || |
| 6025 | (OpInfo.Type == InlineAsm::isInput)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6026 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6027 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6028 | // Memory operands really want the address of the value. If we don't have |
| 6029 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 6030 | // it to a stack slot. |
Eric Christopher | e0b42c0 | 2011-06-03 17:21:23 +0000 | [diff] [blame] | 6031 | // TODO: This isn't quite right. We need to handle these according to |
| 6032 | // the addressing mode that the constraint wants. Also, this may take |
| 6033 | // an additional register for the computation and we don't want that |
| 6034 | // either. |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 6035 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6036 | // If the operand is a float, integer, or vector constant, spill to a |
| 6037 | // constant pool entry to get its address. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6038 | const Value *OpVal = OpInfo.CallOperandVal; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6039 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 6040 | isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6041 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6042 | TLI->getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6043 | } else { |
| 6044 | // Otherwise, create a stack slot and emit a store to it before the |
| 6045 | // asm. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6046 | Type *Ty = OpVal->getType(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6047 | uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty); |
| 6048 | unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6049 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 6050 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6051 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI->getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6052 | Chain = DAG.getStore(Chain, getCurSDLoc(), |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 6053 | OpInfo.CallOperand, StackSlot, |
| 6054 | MachinePointerInfo::getFixedStack(SSFI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6055 | false, false, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6056 | OpInfo.CallOperand = StackSlot; |
| 6057 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6058 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6059 | // There is no longer a Value* corresponding to this operand. |
| 6060 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6061 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6062 | // It is now an indirect operand. |
| 6063 | OpInfo.isIndirect = true; |
| 6064 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6065 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6066 | // If this constraint is for a specific register, allocate it before |
| 6067 | // anything else. |
| 6068 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6069 | GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6070 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6071 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6072 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 6073 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6074 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 6075 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6076 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6077 | // C_Register operands have already been allocated, Other/Memory don't need |
| 6078 | // to be. |
| 6079 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6080 | GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6081 | } |
| 6082 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6083 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 6084 | std::vector<SDValue> AsmNodeOperands; |
| 6085 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 6086 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 6087 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6088 | TLI->getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6089 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6090 | // If we have a !srcloc metadata node associated with it, we want to attach |
| 6091 | // this to the ultimately generated inline asm machineinstr. To do this, we |
| 6092 | // pass in the third operand as this (potentially null) inline asm MDNode. |
| 6093 | const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc"); |
| 6094 | AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6095 | |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6096 | // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore |
| 6097 | // bits as operand 3. |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 6098 | unsigned ExtraInfo = 0; |
| 6099 | if (IA->hasSideEffects()) |
| 6100 | ExtraInfo |= InlineAsm::Extra_HasSideEffects; |
| 6101 | if (IA->isAlignStack()) |
| 6102 | ExtraInfo |= InlineAsm::Extra_IsAlignStack; |
Chad Rosier | 77fffa6 | 2012-09-05 22:17:43 +0000 | [diff] [blame] | 6103 | // Set the asm dialect. |
Chad Rosier | 2f1d815 | 2012-09-05 22:40:13 +0000 | [diff] [blame] | 6104 | ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect; |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6105 | |
| 6106 | // Determine if this InlineAsm MayLoad or MayStore based on the constraints. |
| 6107 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 6108 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
| 6109 | |
| 6110 | // Compute the constraint code and ConstraintType to use. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6111 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6112 | |
Chad Rosier | dfa4cec | 2012-10-30 20:01:12 +0000 | [diff] [blame] | 6113 | // Ideally, we would only check against memory constraints. However, the |
| 6114 | // meaning of an other constraint can be target-specific and we can't easily |
| 6115 | // reason about it. Therefore, be conservative and set MayLoad/MayStore |
| 6116 | // for other constriants as well. |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6117 | if (OpInfo.ConstraintType == TargetLowering::C_Memory || |
| 6118 | OpInfo.ConstraintType == TargetLowering::C_Other) { |
| 6119 | if (OpInfo.Type == InlineAsm::isInput) |
| 6120 | ExtraInfo |= InlineAsm::Extra_MayLoad; |
| 6121 | else if (OpInfo.Type == InlineAsm::isOutput) |
| 6122 | ExtraInfo |= InlineAsm::Extra_MayStore; |
Eric Christopher | fffe363 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 6123 | else if (OpInfo.Type == InlineAsm::isClobber) |
| 6124 | ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore); |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6125 | } |
| 6126 | } |
| 6127 | |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 6128 | AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6129 | TLI->getPointerTy())); |
Dale Johannesen | f1e309e | 2010-07-02 20:16:09 +0000 | [diff] [blame] | 6130 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6131 | // Loop over all of the inputs, copying the operand values into the |
| 6132 | // appropriate registers and processing the output regs. |
| 6133 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6134 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6135 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 6136 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6137 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6138 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 6139 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 6140 | |
| 6141 | switch (OpInfo.Type) { |
| 6142 | case InlineAsm::isOutput: { |
| 6143 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 6144 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 6145 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 6146 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 6147 | |
| 6148 | // Add information to the INLINEASM node to know about this output. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6149 | unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
| 6150 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6151 | TLI->getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6152 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 6153 | break; |
| 6154 | } |
| 6155 | |
| 6156 | // Otherwise, this is a register or register class output. |
| 6157 | |
| 6158 | // Copy the output from the appropriate register. Find a register that |
| 6159 | // we can use. |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6160 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 6161 | LLVMContext &Ctx = *DAG.getContext(); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6162 | Ctx.emitError(CS.getInstruction(), |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6163 | "couldn't allocate output register for constraint '" + |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6164 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6165 | return; |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6166 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6167 | |
| 6168 | // If this is an indirect operand, store through the pointer after the |
| 6169 | // asm. |
| 6170 | if (OpInfo.isIndirect) { |
| 6171 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 6172 | OpInfo.CallOperandVal)); |
| 6173 | } else { |
| 6174 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 6175 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6176 | // Concatenate this output onto the outputs list. |
| 6177 | RetValRegs.append(OpInfo.AssignedRegs); |
| 6178 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6180 | // Add information to the INLINEASM node to know that this register is |
| 6181 | // set. |
Eric Christopher | b0bee81 | 2013-07-30 22:50:44 +0000 | [diff] [blame] | 6182 | OpInfo.AssignedRegs |
| 6183 | .AddInlineAsmOperands(OpInfo.isEarlyClobber |
| 6184 | ? InlineAsm::Kind_RegDefEarlyClobber |
| 6185 | : InlineAsm::Kind_RegDef, |
| 6186 | false, 0, DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6187 | break; |
| 6188 | } |
| 6189 | case InlineAsm::isInput: { |
| 6190 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6191 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 6192 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6193 | // If this is required to match an output register we have already set, |
| 6194 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 6195 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6196 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6197 | // Scan until we find the definition we already emitted of this operand. |
| 6198 | // When we find it, create a RegsForValue operand. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6199 | unsigned CurOp = InlineAsm::Op_FirstOperand; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6200 | for (; OperandNo; --OperandNo) { |
| 6201 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6202 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6203 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6204 | assert((InlineAsm::isRegDefKind(OpFlag) || |
| 6205 | InlineAsm::isRegDefEarlyClobberKind(OpFlag) || |
| 6206 | InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6207 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6210 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6211 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6212 | if (InlineAsm::isRegDefKind(OpFlag) || |
| 6213 | InlineAsm::isRegDefEarlyClobberKind(OpFlag)) { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6214 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6215 | if (OpInfo.isIndirect) { |
| 6216 | // This happens on gcc/testsuite/gcc.dg/pr8788-1.c |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 6217 | LLVMContext &Ctx = *DAG.getContext(); |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6218 | Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:" |
| 6219 | " don't know how to handle tied " |
| 6220 | "indirect register inputs"); |
| 6221 | return; |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6222 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6223 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6224 | RegsForValue MatchedRegs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6225 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 6226 | MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6227 | MatchedRegs.RegVTs.push_back(RegVT); |
| 6228 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6229 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6230 | i != e; ++i) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6231 | if (const TargetRegisterClass *RC = TLI->getRegClassFor(RegVT)) |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6232 | MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC)); |
| 6233 | else { |
| 6234 | LLVMContext &Ctx = *DAG.getContext(); |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6235 | Ctx.emitError(CS.getInstruction(), |
| 6236 | "inline asm error: This value" |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6237 | " type register class is not natively supported!"); |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6238 | return; |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6239 | } |
| 6240 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6241 | // Use the produced MatchedRegs object to |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6242 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6243 | Chain, &Flag, CS.getInstruction()); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6244 | MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6245 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6246 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6247 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6248 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6249 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6250 | assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!"); |
| 6251 | assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 && |
| 6252 | "Unexpected number of operands"); |
| 6253 | // Add information to the INLINEASM node to know about this input. |
| 6254 | // See InlineAsm.h isUseOperandTiedToDef. |
| 6255 | OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag, |
| 6256 | OpInfo.getMatchedOperand()); |
| 6257 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6258 | TLI->getPointerTy())); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6259 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 6260 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6261 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6262 | |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6263 | // Treat indirect 'X' constraint as memory. |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6264 | if (OpInfo.ConstraintType == TargetLowering::C_Other && |
| 6265 | OpInfo.isIndirect) |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6266 | OpInfo.ConstraintType = TargetLowering::C_Memory; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6267 | |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6268 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6269 | std::vector<SDValue> Ops; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6270 | TLI->LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode, |
| 6271 | Ops, DAG); |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6272 | if (Ops.empty()) { |
| 6273 | LLVMContext &Ctx = *DAG.getContext(); |
| 6274 | Ctx.emitError(CS.getInstruction(), |
| 6275 | "invalid operand for inline asm constraint '" + |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6276 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6277 | return; |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6278 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6279 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6280 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6281 | unsigned ResOpType = |
| 6282 | InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6283 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6284 | TLI->getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6285 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 6286 | break; |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6287 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6288 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6289 | if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6290 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6291 | assert(InOperandVal.getValueType() == TLI->getPointerTy() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6292 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6293 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6294 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6295 | unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 6296 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6297 | TLI->getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6298 | AsmNodeOperands.push_back(InOperandVal); |
| 6299 | break; |
| 6300 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6301 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6302 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 6303 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 6304 | "Unknown constraint type!"); |
Eric Christopher | 9eb4f8a | 2012-07-02 21:16:43 +0000 | [diff] [blame] | 6305 | |
| 6306 | // TODO: Support this. |
| 6307 | if (OpInfo.isIndirect) { |
| 6308 | LLVMContext &Ctx = *DAG.getContext(); |
| 6309 | Ctx.emitError(CS.getInstruction(), |
| 6310 | "Don't know how to handle indirect register inputs yet " |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6311 | "for constraint '" + |
| 6312 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6313 | return; |
Eric Christopher | 9eb4f8a | 2012-07-02 21:16:43 +0000 | [diff] [blame] | 6314 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6315 | |
| 6316 | // Copy the input into the appropriate registers. |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6317 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 6318 | LLVMContext &Ctx = *DAG.getContext(); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6319 | Ctx.emitError(CS.getInstruction(), |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6320 | "couldn't allocate input reg for constraint '" + |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6321 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6322 | return; |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6323 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6324 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6325 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6326 | Chain, &Flag, CS.getInstruction()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6327 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6328 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6329 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6330 | break; |
| 6331 | } |
| 6332 | case InlineAsm::isClobber: { |
| 6333 | // Add the clobbered value to the operand list, so that the register |
| 6334 | // allocator is aware that the physreg got clobbered. |
| 6335 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Jakob Stoklund Olesen | f792fa9 | 2011-06-27 04:08:33 +0000 | [diff] [blame] | 6336 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6337 | false, 0, DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6338 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6339 | break; |
| 6340 | } |
| 6341 | } |
| 6342 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6343 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6344 | // 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] | 6345 | AsmNodeOperands[InlineAsm::Op_InputChain] = Chain; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6346 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6347 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6348 | Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(), |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 6349 | DAG.getVTList(MVT::Other, MVT::Glue), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6350 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 6351 | Flag = Chain.getValue(1); |
| 6352 | |
| 6353 | // If this asm returns a register value, copy the result from that register |
| 6354 | // and set it as the value of the call. |
| 6355 | if (!RetValRegs.Regs.empty()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6356 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6357 | Chain, &Flag, CS.getInstruction()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6358 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6359 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 6360 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6361 | EVT ResultType = TLI->getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6362 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6363 | // If any of the results of the inline asm is a vector, it may have the |
| 6364 | // wrong width/num elts. This can happen for register classes that can |
| 6365 | // contain multiple different value types. The preg or vreg allocated may |
| 6366 | // not have the same VT as was expected. Convert it to the right type |
| 6367 | // with bit_convert. |
| 6368 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6369 | Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 6370 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6371 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6372 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6373 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 6374 | // If a result value was tied to an input value, the computed result may |
| 6375 | // have a wider width than the expected result. Extract the relevant |
| 6376 | // portion. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6377 | Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6378 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6379 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6380 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 6381 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6382 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6383 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 6384 | // Don't need to use this as a chain in this case. |
| 6385 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 6386 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6387 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6388 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6389 | std::vector<std::pair<SDValue, const Value *> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6390 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6391 | // Process indirect outputs, first output all of the flagged copies out of |
| 6392 | // physregs. |
| 6393 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 6394 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6395 | const Value *Ptr = IndirectStoresToEmit[i].second; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6396 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6397 | Chain, &Flag, IA); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6398 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
| 6399 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6400 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6401 | // Emit the non-flagged stores from the physregs. |
| 6402 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6403 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6404 | SDValue Val = DAG.getStore(Chain, getCurSDLoc(), |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6405 | StoresToEmit[i].first, |
| 6406 | getValue(StoresToEmit[i].second), |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 6407 | MachinePointerInfo(StoresToEmit[i].second), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6408 | false, false, 0); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6409 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6412 | if (!OutChains.empty()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6413 | Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6414 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6415 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6416 | DAG.setRoot(Chain); |
| 6417 | } |
| 6418 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6419 | void SelectionDAGBuilder::visitVAStart(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6420 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6421 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6422 | getValue(I.getArgOperand(0)), |
| 6423 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6426 | void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6427 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 6428 | const DataLayout &TD = *TLI->getDataLayout(); |
| 6429 | SDValue V = DAG.getVAArg(TLI->getValueType(I.getType()), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 6430 | getRoot(), getValue(I.getOperand(0)), |
Rafael Espindola | cbeeae2 | 2010-07-11 04:01:49 +0000 | [diff] [blame] | 6431 | DAG.getSrcValue(I.getOperand(0)), |
Rafael Espindola | 9d544d0 | 2010-07-12 18:11:17 +0000 | [diff] [blame] | 6432 | TD.getABITypeAlignment(I.getType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6433 | setValue(&I, V); |
| 6434 | DAG.setRoot(V.getValue(1)); |
| 6435 | } |
| 6436 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6437 | void SelectionDAGBuilder::visitVAEnd(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6438 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6439 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6440 | getValue(I.getArgOperand(0)), |
| 6441 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6442 | } |
| 6443 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6444 | void SelectionDAGBuilder::visitVACopy(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6445 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6446 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6447 | getValue(I.getArgOperand(0)), |
| 6448 | getValue(I.getArgOperand(1)), |
| 6449 | DAG.getSrcValue(I.getArgOperand(0)), |
| 6450 | DAG.getSrcValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6451 | } |
| 6452 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6453 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6454 | /// implementation, which just calls LowerCall. |
| 6455 | /// FIXME: When all targets are |
| 6456 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6457 | std::pair<SDValue, SDValue> |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6458 | TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const { |
Stephen Lin | 3484da9 | 2013-04-30 22:49:28 +0000 | [diff] [blame] | 6459 | // Handle the incoming return values from the call. |
| 6460 | CLI.Ins.clear(); |
| 6461 | SmallVector<EVT, 4> RetTys; |
| 6462 | ComputeValueVTs(*this, CLI.RetTy, RetTys); |
| 6463 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 6464 | EVT VT = RetTys[I]; |
| 6465 | MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT); |
| 6466 | unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT); |
| 6467 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 6468 | ISD::InputArg MyFlags; |
| 6469 | MyFlags.VT = RegisterVT; |
| 6470 | MyFlags.Used = CLI.IsReturnValueUsed; |
| 6471 | if (CLI.RetSExt) |
| 6472 | MyFlags.Flags.setSExt(); |
| 6473 | if (CLI.RetZExt) |
| 6474 | MyFlags.Flags.setZExt(); |
| 6475 | if (CLI.IsInReg) |
| 6476 | MyFlags.Flags.setInReg(); |
| 6477 | CLI.Ins.push_back(MyFlags); |
| 6478 | } |
| 6479 | } |
| 6480 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6481 | // Handle all of the outgoing arguments. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6482 | CLI.Outs.clear(); |
| 6483 | CLI.OutVals.clear(); |
| 6484 | ArgListTy &Args = CLI.Args; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6485 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6486 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6487 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 6488 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 6489 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6490 | EVT VT = ValueVTs[Value]; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6491 | Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6492 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 6493 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6494 | ISD::ArgFlagsTy Flags; |
| 6495 | unsigned OriginalAlignment = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6496 | getDataLayout()->getABITypeAlignment(ArgTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6497 | |
| 6498 | if (Args[i].isZExt) |
| 6499 | Flags.setZExt(); |
| 6500 | if (Args[i].isSExt) |
| 6501 | Flags.setSExt(); |
| 6502 | if (Args[i].isInReg) |
| 6503 | Flags.setInReg(); |
| 6504 | if (Args[i].isSRet) |
| 6505 | Flags.setSRet(); |
| 6506 | if (Args[i].isByVal) { |
| 6507 | Flags.setByVal(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6508 | PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 6509 | Type *ElementTy = Ty->getElementType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6510 | Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6511 | // For ByVal, alignment should come from FE. BE will guess if this |
| 6512 | // info is not there but there are cases it cannot get right. |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6513 | unsigned FrameAlign; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6514 | if (Args[i].Alignment) |
| 6515 | FrameAlign = Args[i].Alignment; |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6516 | else |
| 6517 | FrameAlign = getByValTypeAlignment(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6518 | Flags.setByValAlign(FrameAlign); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6519 | } |
| 6520 | if (Args[i].isNest) |
| 6521 | Flags.setNest(); |
| 6522 | Flags.setOrigAlign(OriginalAlignment); |
| 6523 | |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6524 | MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6525 | unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6526 | SmallVector<SDValue, 4> Parts(NumParts); |
| 6527 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 6528 | |
| 6529 | if (Args[i].isSExt) |
| 6530 | ExtendKind = ISD::SIGN_EXTEND; |
| 6531 | else if (Args[i].isZExt) |
| 6532 | ExtendKind = ISD::ZERO_EXTEND; |
| 6533 | |
Stephen Lin | 3484da9 | 2013-04-30 22:49:28 +0000 | [diff] [blame] | 6534 | // Conservatively only handle 'returned' on non-vectors for now |
| 6535 | if (Args[i].isReturned && !Op.getValueType().isVector()) { |
| 6536 | assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues && |
| 6537 | "unexpected use of 'returned'"); |
| 6538 | // Before passing 'returned' to the target lowering code, ensure that |
| 6539 | // either the register MVT and the actual EVT are the same size or that |
| 6540 | // the return value and argument are extended in the same way; in these |
| 6541 | // cases it's safe to pass the argument register value unchanged as the |
| 6542 | // return register value (although it's at the target's option whether |
| 6543 | // to do so) |
| 6544 | // TODO: allow code generation to take advantage of partially preserved |
| 6545 | // registers rather than clobbering the entire register when the |
| 6546 | // parameter extension method is not compatible with the return |
| 6547 | // extension method |
| 6548 | if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) || |
| 6549 | (ExtendKind != ISD::ANY_EXTEND && |
| 6550 | CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt)) |
| 6551 | Flags.setReturned(); |
| 6552 | } |
| 6553 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6554 | getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6555 | PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6556 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6557 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6558 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 6559 | ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(), |
Manman Ren | 0a1544d | 2012-11-01 23:49:58 +0000 | [diff] [blame] | 6560 | i < CLI.NumFixedArgs, |
| 6561 | i, j*Parts[j].getValueType().getStoreSize()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6562 | if (NumParts > 1 && j == 0) |
| 6563 | MyFlags.Flags.setSplit(); |
| 6564 | else if (j != 0) |
| 6565 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6566 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6567 | CLI.Outs.push_back(MyFlags); |
| 6568 | CLI.OutVals.push_back(Parts[j]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6569 | } |
| 6570 | } |
| 6571 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6572 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6573 | SmallVector<SDValue, 4> InVals; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6574 | CLI.Chain = LowerCall(CLI, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6575 | |
| 6576 | // Verify that the target's LowerCall behaved as expected. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6577 | assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6578 | "LowerCall didn't return a valid chain!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6579 | assert((!CLI.IsTailCall || InVals.empty()) && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6580 | "LowerCall emitted a return value for a tail call!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6581 | assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6582 | "LowerCall didn't emit the correct number of values!"); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6583 | |
| 6584 | // For a tail call, the return value is merely live-out and there aren't |
| 6585 | // any nodes in the DAG representing it. Return a special value to |
| 6586 | // indicate that a tail call has been emitted and no more Instructions |
| 6587 | // should be processed in the current block. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6588 | if (CLI.IsTailCall) { |
| 6589 | CLI.DAG.setRoot(CLI.Chain); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6590 | return std::make_pair(SDValue(), SDValue()); |
| 6591 | } |
| 6592 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6593 | DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) { |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 6594 | assert(InVals[i].getNode() && |
| 6595 | "LowerCall emitted a null value!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6596 | assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() && |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 6597 | "LowerCall emitted a value with the wrong type!"); |
| 6598 | }); |
| 6599 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6600 | // Collect the legal value parts into potentially illegal values |
| 6601 | // that correspond to the original function's return values. |
| 6602 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6603 | if (CLI.RetSExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6604 | AssertOp = ISD::AssertSext; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6605 | else if (CLI.RetZExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6606 | AssertOp = ISD::AssertZext; |
| 6607 | SmallVector<SDValue, 4> ReturnValues; |
| 6608 | unsigned CurReg = 0; |
| 6609 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6610 | EVT VT = RetTys[I]; |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6611 | MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6612 | unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6613 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6614 | ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg], |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6615 | NumRegs, RegisterVT, VT, NULL, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 6616 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6617 | CurReg += NumRegs; |
| 6618 | } |
| 6619 | |
| 6620 | // For a function returning void, there is no return value. We can't create |
| 6621 | // 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] | 6622 | // that case, nothing will actually look at the value. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6623 | if (ReturnValues.empty()) |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6624 | return std::make_pair(SDValue(), CLI.Chain); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6625 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6626 | SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL, |
| 6627 | CLI.DAG.getVTList(&RetTys[0], RetTys.size()), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6628 | &ReturnValues[0], ReturnValues.size()); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6629 | return std::make_pair(Res, CLI.Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6630 | } |
| 6631 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 6632 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 6633 | SmallVectorImpl<SDValue> &Results, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6634 | SelectionDAG &DAG) const { |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 6635 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 6636 | if (Res.getNode()) |
| 6637 | Results.push_back(Res); |
| 6638 | } |
| 6639 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6640 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6641 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6642 | } |
| 6643 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6644 | void |
| 6645 | SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) { |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 6646 | SDValue Op = getNonRegisterValue(V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6647 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 6648 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 6649 | "Copy from a reg to the same reg!"); |
| 6650 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 6651 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6652 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 6653 | RegsForValue RFV(V->getContext(), *TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6654 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6655 | RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6656 | PendingExports.push_back(Chain); |
| 6657 | } |
| 6658 | |
| 6659 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 6660 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6661 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 6662 | /// entry block, return true. This includes arguments used by switches, since |
| 6663 | /// the switch may expand into multiple basic blocks. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6664 | static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6665 | // With FastISel active, we may be splitting blocks, so force creation |
| 6666 | // of virtual registers for all non-dead arguments. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6667 | if (FastISel) |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6668 | return A->use_empty(); |
| 6669 | |
| 6670 | const BasicBlock *Entry = A->getParent()->begin(); |
| 6671 | for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end(); |
| 6672 | UI != E; ++UI) { |
| 6673 | const User *U = *UI; |
| 6674 | if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U)) |
| 6675 | return false; // Use not in entry block. |
| 6676 | } |
| 6677 | return true; |
| 6678 | } |
| 6679 | |
Eli Bendersky | 6437d38 | 2013-02-28 23:09:18 +0000 | [diff] [blame] | 6680 | void SelectionDAGISel::LowerArguments(const Function &F) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6681 | SelectionDAG &DAG = SDB->DAG; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6682 | SDLoc dl = SDB->getCurSDLoc(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6683 | const TargetLowering *TLI = getTargetLowering(); |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6684 | const DataLayout *TD = TLI->getDataLayout(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6685 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6686 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6687 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6688 | // Put in an sret pointer parameter before all the other parameters. |
| 6689 | SmallVector<EVT, 1> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6690 | ComputeValueVTs(*getTargetLowering(), |
| 6691 | PointerType::getUnqual(F.getReturnType()), ValueVTs); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6692 | |
| 6693 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 6694 | // or one register. |
| 6695 | ISD::ArgFlagsTy Flags; |
| 6696 | Flags.setSRet(); |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6697 | MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]); |
Stepan Dyatkovskiy | 661afe7 | 2012-10-10 11:37:36 +0000 | [diff] [blame] | 6698 | ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6699 | Ins.push_back(RetArg); |
| 6700 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 6701 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6702 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6703 | unsigned Idx = 1; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6704 | 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] | 6705 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6706 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6707 | ComputeValueVTs(*TLI, I->getType(), ValueVTs); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6708 | bool isArgValueUsed = !I->use_empty(); |
| 6709 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 6710 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6711 | EVT VT = ValueVTs[Value]; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6712 | Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6713 | ISD::ArgFlagsTy Flags; |
| 6714 | unsigned OriginalAlignment = |
| 6715 | TD->getABITypeAlignment(ArgTy); |
| 6716 | |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6717 | if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6718 | Flags.setZExt(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6719 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6720 | Flags.setSExt(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6721 | if (F.getAttributes().hasAttribute(Idx, Attribute::InReg)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6722 | Flags.setInReg(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6723 | if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6724 | Flags.setSRet(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6725 | if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6726 | Flags.setByVal(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6727 | PointerType *Ty = cast<PointerType>(I->getType()); |
| 6728 | Type *ElementTy = Ty->getElementType(); |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6729 | Flags.setByValSize(TD->getTypeAllocSize(ElementTy)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6730 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 6731 | // 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] | 6732 | unsigned FrameAlign; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6733 | if (F.getParamAlignment(Idx)) |
| 6734 | FrameAlign = F.getParamAlignment(Idx); |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6735 | else |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6736 | FrameAlign = TLI->getByValTypeAlignment(ElementTy); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6737 | Flags.setByValAlign(FrameAlign); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6738 | } |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6739 | if (F.getAttributes().hasAttribute(Idx, Attribute::Nest)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6740 | Flags.setNest(); |
| 6741 | Flags.setOrigAlign(OriginalAlignment); |
| 6742 | |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6743 | MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT); |
| 6744 | unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6745 | for (unsigned i = 0; i != NumRegs; ++i) { |
Stepan Dyatkovskiy | 661afe7 | 2012-10-10 11:37:36 +0000 | [diff] [blame] | 6746 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed, |
| 6747 | Idx-1, i*RegisterVT.getStoreSize()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6748 | if (NumRegs > 1 && i == 0) |
| 6749 | MyFlags.Flags.setSplit(); |
| 6750 | // if it isn't first piece, alignment must be 1 |
| 6751 | else if (i > 0) |
| 6752 | MyFlags.Flags.setOrigAlign(1); |
| 6753 | Ins.push_back(MyFlags); |
| 6754 | } |
| 6755 | } |
| 6756 | } |
| 6757 | |
| 6758 | // Call the target to set up the argument values. |
| 6759 | SmallVector<SDValue, 8> InVals; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6760 | SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 6761 | F.isVarArg(), Ins, |
| 6762 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6763 | |
| 6764 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6765 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6766 | "LowerFormalArguments didn't return a valid chain!"); |
| 6767 | assert(InVals.size() == Ins.size() && |
| 6768 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 6769 | DEBUG({ |
| 6770 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 6771 | assert(InVals[i].getNode() && |
| 6772 | "LowerFormalArguments emitted a null value!"); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 6773 | assert(EVT(Ins[i].VT) == InVals[i].getValueType() && |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 6774 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 6775 | } |
| 6776 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6777 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6778 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6779 | DAG.setRoot(NewRoot); |
| 6780 | |
| 6781 | // Set up the argument values. |
| 6782 | unsigned i = 0; |
| 6783 | Idx = 1; |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6784 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6785 | // Create a virtual register for the sret pointer, and put in a copy |
| 6786 | // from the sret argument into it. |
| 6787 | SmallVector<EVT, 1> ValueVTs; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6788 | ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 6789 | MVT VT = ValueVTs[0].getSimpleVT(); |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6790 | MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6791 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6792 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6793 | RegVT, VT, NULL, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6794 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6795 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6796 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6797 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT)); |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6798 | FuncInfo->DemoteRegister = SRetReg; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6799 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6800 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6801 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6802 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6803 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 6804 | // Idx indexes LLVM arguments. Don't touch it. |
| 6805 | ++i; |
| 6806 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6807 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6808 | 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] | 6809 | ++I, ++Idx) { |
| 6810 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6811 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6812 | ComputeValueVTs(*TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6813 | unsigned NumValues = ValueVTs.size(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 6814 | |
| 6815 | // If this argument is unused then remember its value. It is used to generate |
| 6816 | // debugging information. |
Adrian Prantl | df68803 | 2013-05-16 23:44:12 +0000 | [diff] [blame] | 6817 | if (I->use_empty() && NumValues) { |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 6818 | SDB->setUnusedArgValue(I, InVals[i]); |
| 6819 | |
Adrian Prantl | df68803 | 2013-05-16 23:44:12 +0000 | [diff] [blame] | 6820 | // Also remember any frame index for use in FastISel. |
| 6821 | if (FrameIndexSDNode *FI = |
| 6822 | dyn_cast<FrameIndexSDNode>(InVals[i].getNode())) |
| 6823 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
| 6824 | } |
| 6825 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6826 | for (unsigned Val = 0; Val != NumValues; ++Val) { |
| 6827 | EVT VT = ValueVTs[Val]; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6828 | MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT); |
| 6829 | unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6830 | |
| 6831 | if (!I->use_empty()) { |
| 6832 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6833 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6834 | AssertOp = ISD::AssertSext; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6835 | else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6836 | AssertOp = ISD::AssertZext; |
| 6837 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6838 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6839 | NumParts, PartVT, VT, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6840 | NULL, AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6841 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6842 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6843 | i += NumParts; |
| 6844 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6845 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6846 | // We don't need to do anything else for unused arguments. |
| 6847 | if (ArgValues.empty()) |
| 6848 | continue; |
| 6849 | |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6850 | // Note down frame index. |
| 6851 | if (FrameIndexSDNode *FI = |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 6852 | dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode())) |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6853 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 6854 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6855 | SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6856 | SDB->getCurSDLoc()); |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6857 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6858 | SDB->setValue(I, Res); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6859 | if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) { |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6860 | if (LoadSDNode *LNode = |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6861 | dyn_cast<LoadSDNode>(Res.getOperand(0).getNode())) |
| 6862 | if (FrameIndexSDNode *FI = |
| 6863 | dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) |
| 6864 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
| 6865 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6866 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6867 | // If this argument is live outside of the entry block, insert a copy from |
| 6868 | // 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] | 6869 | if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6870 | // If we can, though, try to skip creating an unnecessary vreg. |
| 6871 | // 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] | 6872 | // general. It's also subtly incompatible with the hacks FastISel |
| 6873 | // uses with vregs. |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6874 | unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg(); |
| 6875 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 6876 | FuncInfo->ValueMap[I] = Reg; |
| 6877 | continue; |
| 6878 | } |
| 6879 | } |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6880 | if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6881 | FuncInfo->InitializeRegForValue(I); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6882 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6883 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6884 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6885 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6886 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6887 | |
| 6888 | // Finally, if the target has anything special to do, allow it to do so. |
| 6889 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 6890 | EmitFunctionEntryCode(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6891 | } |
| 6892 | |
| 6893 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6894 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6895 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6896 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6897 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6898 | /// the end. |
| 6899 | /// |
| 6900 | void |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6901 | SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6902 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6903 | |
| 6904 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6905 | |
| 6906 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6907 | // from this block. |
| 6908 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6909 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6910 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6911 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6912 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6913 | // If this terminator has multiple identical successors (common for |
| 6914 | // switches), only handle each succ once. |
| 6915 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6916 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6917 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6918 | |
| 6919 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6920 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6921 | // emitted yet. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6922 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 6923 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6924 | // Ignore dead phi's. |
| 6925 | if (PN->use_empty()) continue; |
| 6926 | |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 6927 | // Skip empty types |
| 6928 | if (PN->getType()->isEmptyTy()) |
| 6929 | continue; |
| 6930 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6931 | unsigned Reg; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6932 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6933 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6934 | if (const Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6935 | unsigned &RegOut = ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6936 | if (RegOut == 0) { |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 6937 | RegOut = FuncInfo.CreateRegs(C->getType()); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6938 | CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6939 | } |
| 6940 | Reg = RegOut; |
| 6941 | } else { |
Dan Gohman | c25ad63 | 2010-07-01 01:33:21 +0000 | [diff] [blame] | 6942 | DenseMap<const Value *, unsigned>::iterator I = |
| 6943 | FuncInfo.ValueMap.find(PHIOp); |
| 6944 | if (I != FuncInfo.ValueMap.end()) |
| 6945 | Reg = I->second; |
| 6946 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6947 | assert(isa<AllocaInst>(PHIOp) && |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6948 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6949 | "Didn't codegen value into a register!??"); |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 6950 | Reg = FuncInfo.CreateRegs(PHIOp->getType()); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6951 | CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6952 | } |
| 6953 | } |
| 6954 | |
| 6955 | // Remember that this register needs to added to the machine PHI node as |
| 6956 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6957 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6958 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 6959 | ComputeValueVTs(*TLI, PN->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6960 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6961 | EVT VT = ValueVTs[vti]; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6962 | unsigned NumRegisters = TLI->getNumRegisters(*DAG.getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6963 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6964 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6965 | Reg += NumRegisters; |
| 6966 | } |
| 6967 | } |
| 6968 | } |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6969 | |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6970 | ConstantsOut.clear(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6971 | } |