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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 52 | #include "llvm/Support/MathExtras.h" |
| 53 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetFrameLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 57 | #include "llvm/Target/TargetLibraryInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 58 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 59 | #include "llvm/Target/TargetOptions.h" |
Richard Sandiford | ac168b8 | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 60 | #include "llvm/Target/TargetSelectionDAGInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 61 | #include <algorithm> |
| 62 | using namespace llvm; |
| 63 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 64 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 65 | /// some float libcalls (6, 8 or 12 bits). |
| 66 | static unsigned LimitFloatPrecision; |
| 67 | |
| 68 | static cl::opt<unsigned, true> |
| 69 | LimitFPPrecision("limit-float-precision", |
| 70 | cl::desc("Generate low-precision inline sequences " |
| 71 | "for some float libcalls"), |
| 72 | cl::location(LimitFloatPrecision), |
| 73 | cl::init(0)); |
| 74 | |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 75 | // Limit the width of DAG chains. This is important in general to prevent |
| 76 | // prevent DAG-based analysis from blowing up. For example, alias analysis and |
| 77 | // load clustering may not complete in reasonable time. It is difficult to |
| 78 | // recognize and avoid this situation within each individual analysis, and |
| 79 | // 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] | 80 | // the safe approach, and will be especially important with global DAGs. |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 81 | // |
| 82 | // MaxParallelChains default is arbitrarily high to avoid affecting |
| 83 | // 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] | 84 | // sequence over this should have been converted to llvm.memcpy by the |
| 85 | // frontend. It easy to induce this behavior with .ll code such as: |
| 86 | // %buffer = alloca [4096 x i8] |
| 87 | // %data = load [4096 x i8]* %argPtr |
| 88 | // store [4096 x i8] %data, [4096 x i8]* %buffer |
Andrew Trick | 778583a | 2011-03-11 17:46:59 +0000 | [diff] [blame] | 89 | static const unsigned MaxParallelChains = 64; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 90 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 91 | static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 92 | const SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 93 | MVT PartVT, EVT ValueVT, const Value *V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 94 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 95 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 96 | /// combined into the value they represent. If the parts combine to a type |
| 97 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 98 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 99 | /// (ISD::AssertSext). |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 100 | static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 101 | const SDValue *Parts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 102 | unsigned NumParts, MVT PartVT, EVT ValueVT, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 103 | const Value *V, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 104 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 105 | if (ValueVT.isVector()) |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 106 | return getCopyFromPartsVector(DAG, DL, Parts, NumParts, |
| 107 | PartVT, ValueVT, V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 108 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 109 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 110 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 111 | SDValue Val = Parts[0]; |
| 112 | |
| 113 | if (NumParts > 1) { |
| 114 | // Assemble the value from multiple parts. |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 115 | if (ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 116 | unsigned PartBits = PartVT.getSizeInBits(); |
| 117 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 118 | |
| 119 | // Assemble the power of 2 part. |
| 120 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 121 | 1 << Log2_32(NumParts) : NumParts; |
| 122 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 123 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 124 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 125 | SDValue Lo, Hi; |
| 126 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 127 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 128 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 129 | if (RoundParts > 2) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 130 | Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 131 | PartVT, HalfVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 132 | Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 133 | RoundParts / 2, PartVT, HalfVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 134 | } else { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 135 | Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]); |
| 136 | Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 137 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 138 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 139 | if (TLI.isBigEndian()) |
| 140 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 142 | Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 143 | |
| 144 | if (RoundParts < NumParts) { |
| 145 | // Assemble the trailing non-power-of-2 part. |
| 146 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 147 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 148 | Hi = getCopyFromParts(DAG, DL, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 149 | Parts + RoundParts, OddParts, PartVT, OddVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 150 | |
| 151 | // Combine the round and odd parts. |
| 152 | Lo = Val; |
| 153 | if (TLI.isBigEndian()) |
| 154 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 155 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 156 | Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi); |
| 157 | Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 158 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 159 | TLI.getPointerTy())); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 160 | Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo); |
| 161 | Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 162 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 163 | } else if (PartVT.isFloatingPoint()) { |
| 164 | // FP split into multiple FP parts (for ppcf128) |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 165 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 166 | "Unexpected split"); |
| 167 | SDValue Lo, Hi; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 168 | Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]); |
| 169 | Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 170 | if (TLI.isBigEndian()) |
| 171 | std::swap(Lo, Hi); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 172 | Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 173 | } else { |
| 174 | // FP split into integer parts (soft fp) |
| 175 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 176 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 177 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 178 | Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | // 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] | 183 | EVT PartEVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 184 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 185 | if (PartEVT == ValueVT) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 186 | return Val; |
| 187 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 188 | if (PartEVT.isInteger() && ValueVT.isInteger()) { |
| 189 | if (ValueVT.bitsLT(PartEVT)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 190 | // For a truncate, see if we have any information to |
| 191 | // indicate whether the truncated bits will always be |
| 192 | // zero or sign-extension. |
| 193 | if (AssertOp != ISD::DELETED_NODE) |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 194 | Val = DAG.getNode(AssertOp, DL, PartEVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 195 | DAG.getValueType(ValueVT)); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 196 | return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 198 | return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 201 | if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 202 | // FP_ROUND's are always exact here. |
| 203 | if (ValueVT.bitsLT(Val.getValueType())) |
| 204 | return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, |
Pete Cooper | f57e1c2 | 2012-01-17 01:54:07 +0000 | [diff] [blame] | 205 | DAG.getTargetConstant(1, TLI.getPointerTy())); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 207 | return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 210 | if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits()) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 211 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 212 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 213 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 216 | /// getCopyFromPartsVector - Create a value that contains the specified legal |
| 217 | /// parts combined into the value they represent. If the parts combine to a |
| 218 | /// type larger then ValueVT then AssertOp can be used to specify whether the |
| 219 | /// extra bits are known to be zero (ISD::AssertZext) or sign extended from |
| 220 | /// ValueVT (ISD::AssertSext). |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 221 | static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 222 | const SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 223 | MVT PartVT, EVT ValueVT, const Value *V) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 224 | assert(ValueVT.isVector() && "Not a vector value"); |
| 225 | assert(NumParts > 0 && "No parts to assemble!"); |
| 226 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 227 | SDValue Val = Parts[0]; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 229 | // Handle a multi-element vector. |
| 230 | if (NumParts > 1) { |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 231 | EVT IntermediateVT; |
| 232 | MVT RegisterVT; |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 233 | unsigned NumIntermediates; |
| 234 | unsigned NumRegs = |
| 235 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
| 236 | NumIntermediates, RegisterVT); |
| 237 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 238 | NumParts = NumRegs; // Silence a compiler warning. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 239 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 240 | assert(RegisterVT == Parts[0].getSimpleValueType() && |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 241 | "Part type doesn't match part!"); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 243 | // Assemble the parts into intermediate operands. |
| 244 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 245 | if (NumIntermediates == NumParts) { |
| 246 | // If the register was not expanded, truncate or copy the value, |
| 247 | // as appropriate. |
| 248 | for (unsigned i = 0; i != NumParts; ++i) |
| 249 | Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 250 | PartVT, IntermediateVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 251 | } else if (NumParts > 0) { |
| 252 | // If the intermediate type was expanded, build the intermediate |
| 253 | // operands from the parts. |
| 254 | assert(NumParts % NumIntermediates == 0 && |
| 255 | "Must expand into a divisible number of parts!"); |
| 256 | unsigned Factor = NumParts / NumIntermediates; |
| 257 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 258 | Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 259 | PartVT, IntermediateVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 260 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 262 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 263 | // intermediate operands. |
| 264 | Val = DAG.getNode(IntermediateVT.isVector() ? |
| 265 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL, |
| 266 | ValueVT, &Ops[0], NumIntermediates); |
| 267 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 269 | // 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] | 270 | EVT PartEVT = Val.getValueType(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 271 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 272 | if (PartEVT == ValueVT) |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 273 | return Val; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 274 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 275 | if (PartEVT.isVector()) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 276 | // If the element type of the source/dest vectors are the same, but the |
| 277 | // parts vector has more elements than the value vector, then we have a |
| 278 | // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the |
| 279 | // elements we want. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 280 | if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) { |
| 281 | assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() && |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 282 | "Cannot narrow, it would be a lossy transformation"); |
| 283 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 284 | DAG.getConstant(0, TLI.getVectorIdxTy())); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 287 | // Vector/Vector bitcast. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 288 | if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 289 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
| 290 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 291 | assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() && |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 292 | "Cannot handle this kind of promotion"); |
| 293 | // Promoted vector extract |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 294 | bool Smaller = ValueVT.bitsLE(PartEVT); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 295 | return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 296 | DL, ValueVT, Val); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 297 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 298 | } |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 299 | |
Eric Christopher | 9aaa02a | 2011-06-01 19:55:10 +0000 | [diff] [blame] | 300 | // Trivial bitcast if the types are the same size and the destination |
| 301 | // vector type is legal. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 302 | if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() && |
Eric Christopher | 9aaa02a | 2011-06-01 19:55:10 +0000 | [diff] [blame] | 303 | TLI.isTypeLegal(ValueVT)) |
| 304 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 305 | |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 306 | // Handle cases such as i8 -> <1 x i1> |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 307 | if (ValueVT.getVectorNumElements() != 1) { |
| 308 | LLVMContext &Ctx = *DAG.getContext(); |
| 309 | Twine ErrMsg("non-trivial scalar-to-vector conversion"); |
| 310 | if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 311 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 312 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 313 | ErrMsg = ErrMsg + ", possible invalid constraint for vector type"; |
| 314 | Ctx.emitError(I, ErrMsg); |
| 315 | } else { |
| 316 | Ctx.emitError(ErrMsg); |
| 317 | } |
Chad Rosier | f0b0755 | 2013-05-01 19:49:26 +0000 | [diff] [blame] | 318 | return DAG.getUNDEF(ValueVT); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 319 | } |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 320 | |
| 321 | if (ValueVT.getVectorNumElements() == 1 && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 322 | ValueVT.getVectorElementType() != PartEVT) { |
| 323 | bool Smaller = ValueVT.bitsLE(PartEVT); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 324 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 325 | DL, ValueVT.getScalarType(), Val); |
| 326 | } |
| 327 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 328 | return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val); |
| 329 | } |
| 330 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 331 | static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 332 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 333 | MVT PartVT, const Value *V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 334 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 335 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 336 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 337 | /// 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] | 338 | static void getCopyToParts(SelectionDAG &DAG, SDLoc DL, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 339 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 340 | MVT PartVT, const Value *V, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 341 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 342 | EVT ValueVT = Val.getValueType(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 343 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 344 | // Handle the vector case separately. |
| 345 | if (ValueVT.isVector()) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 346 | return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 347 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 348 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 349 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 350 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 351 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 352 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 353 | if (NumParts == 0) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 354 | return; |
| 355 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 356 | assert(!ValueVT.isVector() && "Vector case handled elsewhere"); |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 357 | EVT PartEVT = PartVT; |
| 358 | if (PartEVT == ValueVT) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 359 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 360 | Parts[0] = Val; |
| 361 | return; |
| 362 | } |
| 363 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 364 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 365 | // If the parts cover more bits than the value has, promote the value. |
| 366 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 367 | assert(NumParts == 1 && "Do not know what to promote to!"); |
| 368 | Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val); |
| 369 | } else { |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 370 | assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && |
| 371 | ValueVT.isInteger() && |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 372 | "Unknown mismatch!"); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 373 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 374 | Val = DAG.getNode(ExtendKind, DL, ValueVT, Val); |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 375 | if (PartVT == MVT::x86mmx) |
| 376 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 377 | } |
| 378 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 379 | // Different types of the same size. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 380 | assert(NumParts == 1 && PartEVT != ValueVT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 381 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 382 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 383 | // If the parts cover less bits than value has, truncate the value. |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 384 | assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && |
| 385 | ValueVT.isInteger() && |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 386 | "Unknown mismatch!"); |
| 387 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 388 | Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 389 | if (PartVT == MVT::x86mmx) |
| 390 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // The value may have changed - recompute ValueVT. |
| 394 | ValueVT = Val.getValueType(); |
| 395 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 396 | "Failed to tile the value with PartVT!"); |
| 397 | |
| 398 | if (NumParts == 1) { |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 399 | if (PartEVT != ValueVT) { |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 400 | LLVMContext &Ctx = *DAG.getContext(); |
| 401 | Twine ErrMsg("scalar-to-vector conversion failed"); |
| 402 | if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 403 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 404 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 405 | ErrMsg = ErrMsg + ", possible invalid constraint for vector type"; |
| 406 | Ctx.emitError(I, ErrMsg); |
| 407 | } else { |
| 408 | Ctx.emitError(ErrMsg); |
| 409 | } |
| 410 | } |
| 411 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 412 | Parts[0] = Val; |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | // Expand the value into multiple parts. |
| 417 | if (NumParts & (NumParts - 1)) { |
| 418 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 419 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 420 | "Do not know what to expand to!"); |
| 421 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 422 | unsigned RoundBits = RoundParts * PartBits; |
| 423 | unsigned OddParts = NumParts - RoundParts; |
| 424 | SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val, |
| 425 | DAG.getIntPtrConstant(RoundBits)); |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 426 | getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 427 | |
| 428 | if (TLI.isBigEndian()) |
| 429 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 430 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 431 | |
| 432 | NumParts = RoundParts; |
| 433 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 434 | Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
| 435 | } |
| 436 | |
| 437 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 438 | // EXTRACT_ELEMENT. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 439 | Parts[0] = DAG.getNode(ISD::BITCAST, DL, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 440 | EVT::getIntegerVT(*DAG.getContext(), |
| 441 | ValueVT.getSizeInBits()), |
| 442 | Val); |
| 443 | |
| 444 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 445 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 446 | unsigned ThisBits = StepSize * PartBits / 2; |
| 447 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
| 448 | SDValue &Part0 = Parts[i]; |
| 449 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 450 | |
| 451 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, |
| 452 | ThisVT, Part0, DAG.getIntPtrConstant(1)); |
| 453 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, |
| 454 | ThisVT, Part0, DAG.getIntPtrConstant(0)); |
| 455 | |
| 456 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 457 | Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0); |
| 458 | Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (TLI.isBigEndian()) |
| 464 | std::reverse(Parts, Parts + OrigNumParts); |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /// getCopyToPartsVector - Create a series of nodes that contain the specified |
| 469 | /// value split into legal parts. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 470 | static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 471 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 472 | MVT PartVT, const Value *V) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 473 | EVT ValueVT = Val.getValueType(); |
| 474 | assert(ValueVT.isVector() && "Not a vector"); |
| 475 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 476 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 477 | if (NumParts == 1) { |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 478 | EVT PartEVT = PartVT; |
| 479 | if (PartEVT == ValueVT) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 480 | // Nothing to do. |
| 481 | } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
| 482 | // Bitconvert vector->vector case. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 483 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 484 | } else if (PartVT.isVector() && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 485 | PartEVT.getVectorElementType() == ValueVT.getVectorElementType() && |
| 486 | PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 487 | EVT ElementVT = PartVT.getVectorElementType(); |
| 488 | // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in |
| 489 | // undef elements. |
| 490 | SmallVector<SDValue, 16> Ops; |
| 491 | for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i) |
| 492 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 493 | ElementVT, Val, DAG.getConstant(i, |
| 494 | TLI.getVectorIdxTy()))); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 495 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 496 | for (unsigned i = ValueVT.getVectorNumElements(), |
| 497 | e = PartVT.getVectorNumElements(); i != e; ++i) |
| 498 | Ops.push_back(DAG.getUNDEF(ElementVT)); |
| 499 | |
| 500 | Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size()); |
| 501 | |
| 502 | // FIXME: Use CONCAT for 2x -> 4x. |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 503 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 504 | //SDValue UndefElts = DAG.getUNDEF(VectorTy); |
| 505 | //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 506 | } else if (PartVT.isVector() && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 507 | PartEVT.getVectorElementType().bitsGE( |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 508 | ValueVT.getVectorElementType()) && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 509 | PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) { |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 510 | |
| 511 | // Promoted vector extract |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 512 | bool Smaller = PartEVT.bitsLE(ValueVT); |
Nadav Rotem | c6341e6 | 2011-06-19 08:49:38 +0000 | [diff] [blame] | 513 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 514 | DL, PartVT, Val); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 515 | } else{ |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 516 | // Vector -> scalar conversion. |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 517 | assert(ValueVT.getVectorNumElements() == 1 && |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 518 | "Only trivial vector-to-scalar conversions should get here!"); |
| 519 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 520 | PartVT, Val, DAG.getConstant(0, TLI.getVectorIdxTy())); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 521 | |
| 522 | bool Smaller = ValueVT.bitsLE(PartVT); |
| 523 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 524 | DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 525 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 526 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 527 | Parts[0] = Val; |
| 528 | return; |
| 529 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 530 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 531 | // Handle a multi-element vector. |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 532 | EVT IntermediateVT; |
| 533 | MVT RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 534 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 535 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
Devang Patel | 8f09bea | 2010-08-26 20:32:32 +0000 | [diff] [blame] | 536 | IntermediateVT, |
| 537 | NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 538 | unsigned NumElements = ValueVT.getVectorNumElements(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 539 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 540 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 541 | NumParts = NumRegs; // Silence a compiler warning. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 542 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 543 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 544 | // Split the vector into intermediate operands. |
| 545 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 546 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 547 | if (IntermediateVT.isVector()) |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 548 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 549 | IntermediateVT, Val, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 550 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 551 | TLI.getVectorIdxTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 552 | else |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 553 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 554 | IntermediateVT, Val, |
| 555 | DAG.getConstant(i, TLI.getVectorIdxTy())); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 556 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 557 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 558 | // Split the intermediate operands into legal parts. |
| 559 | if (NumParts == NumIntermediates) { |
| 560 | // If the register was not expanded, promote or copy the value, |
| 561 | // as appropriate. |
| 562 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 563 | getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 564 | } else if (NumParts > 0) { |
| 565 | // If the intermediate type was expanded, split each the value into |
| 566 | // legal parts. |
| 567 | assert(NumParts % NumIntermediates == 0 && |
| 568 | "Must expand into a divisible number of parts!"); |
| 569 | unsigned Factor = NumParts / NumIntermediates; |
| 570 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 571 | getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 575 | namespace { |
| 576 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 577 | /// that a particular set of values is assigned, and the type information |
| 578 | /// about the value. The most common situation is to represent one value at a |
| 579 | /// time, but struct or array values are handled element-wise as multiple |
| 580 | /// values. The splitting of aggregates is performed recursively, so that we |
| 581 | /// never have aggregate-typed registers. The values at this point do not |
| 582 | /// necessarily have legal types, so each value may require one or more |
| 583 | /// registers of some legal type. |
| 584 | /// |
| 585 | struct RegsForValue { |
| 586 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 587 | /// may need be promoted or synthesized from one or more registers. |
| 588 | /// |
| 589 | SmallVector<EVT, 4> ValueVTs; |
| 590 | |
| 591 | /// RegVTs - The value types of the registers. This is the same size as |
| 592 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 593 | /// register or registers are. (Individual values are never synthesized |
| 594 | /// from more than one type of register.) |
| 595 | /// |
| 596 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 597 | /// getRegisterType member function, however when with physical registers |
| 598 | /// it is necessary to have a separate record of the types. |
| 599 | /// |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 600 | SmallVector<MVT, 4> RegVTs; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 601 | |
| 602 | /// Regs - This list holds the registers assigned to the values. |
| 603 | /// Each legal or promoted value requires one register, and each |
| 604 | /// expanded value requires multiple registers. |
| 605 | /// |
| 606 | SmallVector<unsigned, 4> Regs; |
| 607 | |
| 608 | RegsForValue() {} |
| 609 | |
| 610 | RegsForValue(const SmallVector<unsigned, 4> ®s, |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 611 | MVT regvt, EVT valuevt) |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 612 | : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 613 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 614 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 615 | unsigned Reg, Type *Ty) { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 616 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 617 | |
| 618 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 619 | EVT ValueVT = ValueVTs[Value]; |
| 620 | unsigned NumRegs = tli.getNumRegisters(Context, ValueVT); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 621 | MVT RegisterVT = tli.getRegisterType(Context, ValueVT); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 622 | for (unsigned i = 0; i != NumRegs; ++i) |
| 623 | Regs.push_back(Reg + i); |
| 624 | RegVTs.push_back(RegisterVT); |
| 625 | Reg += NumRegs; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 630 | bool areValueTypesLegal(const TargetLowering &TLI) { |
| 631 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 632 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 633 | if (!TLI.isTypeLegal(RegisterVT)) |
| 634 | return false; |
| 635 | } |
| 636 | return true; |
| 637 | } |
| 638 | |
| 639 | /// append - Add the specified values to this one. |
| 640 | void append(const RegsForValue &RHS) { |
| 641 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 642 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 643 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 644 | } |
| 645 | |
| 646 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 647 | /// this value and returns the result as a ValueVTs value. This uses |
| 648 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 649 | /// If the Flag pointer is NULL, no flag is used. |
| 650 | SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 651 | SDLoc dl, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 652 | SDValue &Chain, SDValue *Flag, |
| 653 | const Value *V = 0) const; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 654 | |
| 655 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 656 | /// specified value into the registers specified by this object. This uses |
| 657 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 658 | /// If the Flag pointer is NULL, no flag is used. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 659 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 660 | SDValue &Chain, SDValue *Flag, const Value *V) const; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 661 | |
| 662 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 663 | /// operand list. This adds the code marker, matching input operand index |
| 664 | /// (if applicable), and includes the number of values added into it. |
| 665 | void AddInlineAsmOperands(unsigned Kind, |
| 666 | bool HasMatching, unsigned MatchingIdx, |
| 667 | SelectionDAG &DAG, |
| 668 | std::vector<SDValue> &Ops) const; |
| 669 | }; |
| 670 | } |
| 671 | |
| 672 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 673 | /// this value and returns the result as a ValueVT value. This uses |
| 674 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 675 | /// If the Flag pointer is NULL, no flag is used. |
| 676 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
| 677 | FunctionLoweringInfo &FuncInfo, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 678 | SDLoc dl, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 679 | SDValue &Chain, SDValue *Flag, |
| 680 | const Value *V) const { |
Dan Gohman | 7da5d3f | 2010-07-26 18:15:41 +0000 | [diff] [blame] | 681 | // A Value with type {} or [0 x %t] needs no registers. |
| 682 | if (ValueVTs.empty()) |
| 683 | return SDValue(); |
| 684 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 685 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 686 | |
| 687 | // Assemble the legal parts into the final values. |
| 688 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 689 | SmallVector<SDValue, 8> Parts; |
| 690 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 691 | // Copy the legal parts from the registers. |
| 692 | EVT ValueVT = ValueVTs[Value]; |
| 693 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 694 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 695 | |
| 696 | Parts.resize(NumRegs); |
| 697 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 698 | SDValue P; |
| 699 | if (Flag == 0) { |
| 700 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
| 701 | } else { |
| 702 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
| 703 | *Flag = P.getValue(2); |
| 704 | } |
| 705 | |
| 706 | Chain = P.getValue(1); |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 707 | Parts[i] = P; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 708 | |
| 709 | // If the source register was virtual and if we know something about it, |
| 710 | // add an assert node. |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 711 | if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) || |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 712 | !RegisterVT.isInteger() || RegisterVT.isVector()) |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 713 | continue; |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 714 | |
| 715 | const FunctionLoweringInfo::LiveOutInfo *LOI = |
| 716 | FuncInfo.GetLiveOutRegInfo(Regs[Part+i]); |
| 717 | if (!LOI) |
| 718 | continue; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 719 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 720 | unsigned RegSize = RegisterVT.getSizeInBits(); |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 721 | unsigned NumSignBits = LOI->NumSignBits; |
| 722 | unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes(); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 723 | |
Quentin Colombet | a3fb49c | 2013-06-18 20:14:39 +0000 | [diff] [blame] | 724 | if (NumZeroBits == RegSize) { |
| 725 | // The current value is a zero. |
| 726 | // Explicitly express that as it would be easier for |
| 727 | // optimizations to kick in. |
| 728 | Parts[i] = DAG.getConstant(0, RegisterVT); |
| 729 | continue; |
| 730 | } |
| 731 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 732 | // FIXME: We capture more information than the dag can represent. For |
| 733 | // now, just use the tightest assertzext/assertsext possible. |
| 734 | bool isSExt = true; |
| 735 | EVT FromVT(MVT::Other); |
| 736 | if (NumSignBits == RegSize) |
| 737 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 738 | else if (NumZeroBits >= RegSize-1) |
| 739 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 740 | else if (NumSignBits > RegSize-8) |
| 741 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
| 742 | else if (NumZeroBits >= RegSize-8) |
| 743 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 744 | else if (NumSignBits > RegSize-16) |
| 745 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
| 746 | else if (NumZeroBits >= RegSize-16) |
| 747 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
| 748 | else if (NumSignBits > RegSize-32) |
| 749 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
| 750 | else if (NumZeroBits >= RegSize-32) |
| 751 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
| 752 | else |
| 753 | continue; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 754 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 755 | // Add an assertion node. |
| 756 | assert(FromVT != MVT::Other); |
| 757 | Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
| 758 | RegisterVT, P, DAG.getValueType(FromVT)); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 762 | NumRegs, RegisterVT, ValueVT, V); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 763 | Part += NumRegs; |
| 764 | Parts.clear(); |
| 765 | } |
| 766 | |
| 767 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 768 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 769 | &Values[0], ValueVTs.size()); |
| 770 | } |
| 771 | |
| 772 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 773 | /// specified value into the registers specified by this object. This uses |
| 774 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 775 | /// If the Flag pointer is NULL, no flag is used. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 776 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 777 | SDValue &Chain, SDValue *Flag, |
| 778 | const Value *V) const { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 779 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 780 | |
| 781 | // Get the list of the values's legal parts. |
| 782 | unsigned NumRegs = Regs.size(); |
| 783 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 784 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 785 | EVT ValueVT = ValueVTs[Value]; |
| 786 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 787 | MVT RegisterVT = RegVTs[Value]; |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 788 | ISD::NodeType ExtendKind = |
| 789 | TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 791 | getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 792 | &Parts[Part], NumParts, RegisterVT, V, ExtendKind); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 793 | Part += NumParts; |
| 794 | } |
| 795 | |
| 796 | // Copy the parts into the registers. |
| 797 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 798 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 799 | SDValue Part; |
| 800 | if (Flag == 0) { |
| 801 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
| 802 | } else { |
| 803 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
| 804 | *Flag = Part.getValue(1); |
| 805 | } |
| 806 | |
| 807 | Chains[i] = Part.getValue(0); |
| 808 | } |
| 809 | |
| 810 | if (NumRegs == 1 || Flag) |
| 811 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
| 812 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 813 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 814 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 815 | // user as well as a successor (the TF operands are flagged to the user). |
| 816 | // c1, f1 = CopyToReg |
| 817 | // c2, f2 = CopyToReg |
| 818 | // c3 = TokenFactor c1, c2 |
| 819 | // ... |
| 820 | // = op c3, ..., f2 |
| 821 | Chain = Chains[NumRegs-1]; |
| 822 | else |
| 823 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
| 824 | } |
| 825 | |
| 826 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 827 | /// operand list. This adds the code marker and includes the number of |
| 828 | /// values added into it. |
| 829 | void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching, |
| 830 | unsigned MatchingIdx, |
| 831 | SelectionDAG &DAG, |
| 832 | std::vector<SDValue> &Ops) const { |
| 833 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 834 | |
| 835 | unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size()); |
| 836 | if (HasMatching) |
| 837 | Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx); |
Jakob Stoklund Olesen | 459b74b | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 838 | else if (!Regs.empty() && |
| 839 | TargetRegisterInfo::isVirtualRegister(Regs.front())) { |
| 840 | // Put the register class of the virtual registers in the flag word. That |
| 841 | // way, later passes can recompute register class constraints for inline |
| 842 | // assembly as well as normal instructions. |
| 843 | // Don't do this for tied operands that can use the regclass information |
| 844 | // from the def. |
| 845 | const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 846 | const TargetRegisterClass *RC = MRI.getRegClass(Regs.front()); |
| 847 | Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID()); |
| 848 | } |
| 849 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 850 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
| 851 | Ops.push_back(Res); |
| 852 | |
| 853 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 854 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 855 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 856 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 857 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
| 858 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
| 859 | } |
| 860 | } |
| 861 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 862 | |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 863 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa, |
| 864 | const TargetLibraryInfo *li) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 865 | AA = &aa; |
| 866 | GFI = gfi; |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 867 | LibInfo = li; |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 868 | TD = DAG.getTarget().getDataLayout(); |
Richard Smith | cb1f68d | 2012-08-22 00:42:39 +0000 | [diff] [blame] | 869 | Context = DAG.getContext(); |
Bill Wendling | 4ed1fb0 | 2011-10-15 01:00:26 +0000 | [diff] [blame] | 870 | LPadToCallSiteMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 873 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 874 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 875 | /// for a new block. This doesn't clear out information about |
| 876 | /// additional blocks that are needed to complete switch lowering |
| 877 | /// or PHI node updating; that information is cleared out as it is |
| 878 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 879 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 880 | NodeMap.clear(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 881 | UnusedArgNodeMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 882 | PendingLoads.clear(); |
| 883 | PendingExports.clear(); |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 884 | CurInst = NULL; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 885 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 888 | /// clearDanglingDebugInfo - Clear the dangling debug information |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 889 | /// map. This function is separated from the clear so that debug |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 890 | /// information that is dangling in a basic block can be properly |
| 891 | /// resolved in a different basic block. This allows the |
| 892 | /// SelectionDAG to resolve dangling debug information attached |
| 893 | /// to PHI nodes. |
| 894 | void SelectionDAGBuilder::clearDanglingDebugInfo() { |
| 895 | DanglingDebugInfoMap.clear(); |
| 896 | } |
| 897 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 898 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 899 | /// flushing any PendingLoad items. This must be done before emitting |
| 900 | /// a store or any other node that may need to be ordered after any |
| 901 | /// prior load instructions. |
| 902 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 903 | SDValue SelectionDAGBuilder::getRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 904 | if (PendingLoads.empty()) |
| 905 | return DAG.getRoot(); |
| 906 | |
| 907 | if (PendingLoads.size() == 1) { |
| 908 | SDValue Root = PendingLoads[0]; |
| 909 | DAG.setRoot(Root); |
| 910 | PendingLoads.clear(); |
| 911 | return Root; |
| 912 | } |
| 913 | |
| 914 | // Otherwise, we have to make a token factor node. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 915 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 916 | &PendingLoads[0], PendingLoads.size()); |
| 917 | PendingLoads.clear(); |
| 918 | DAG.setRoot(Root); |
| 919 | return Root; |
| 920 | } |
| 921 | |
| 922 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 923 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 924 | /// to do this before emitting a terminator instruction. |
| 925 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 926 | SDValue SelectionDAGBuilder::getControlRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 927 | SDValue Root = DAG.getRoot(); |
| 928 | |
| 929 | if (PendingExports.empty()) |
| 930 | return Root; |
| 931 | |
| 932 | // Turn all of the CopyToReg chains into one factored node. |
| 933 | if (Root.getOpcode() != ISD::EntryToken) { |
| 934 | unsigned i = 0, e = PendingExports.size(); |
| 935 | for (; i != e; ++i) { |
| 936 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 937 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 938 | break; // Don't add the root if we already indirectly depend on it. |
| 939 | } |
| 940 | |
| 941 | if (i == e) |
| 942 | PendingExports.push_back(Root); |
| 943 | } |
| 944 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 945 | Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 946 | &PendingExports[0], |
| 947 | PendingExports.size()); |
| 948 | PendingExports.clear(); |
| 949 | DAG.setRoot(Root); |
| 950 | return Root; |
| 951 | } |
| 952 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 953 | void SelectionDAGBuilder::visit(const Instruction &I) { |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 954 | // Set up outgoing PHI node register values before emitting the terminator. |
| 955 | if (isa<TerminatorInst>(&I)) |
| 956 | HandlePHINodesInSuccessorBlocks(I.getParent()); |
| 957 | |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 958 | ++SDNodeOrder; |
| 959 | |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 960 | CurInst = &I; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 961 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 962 | visit(I.getOpcode(), I); |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 963 | |
Dan Gohman | 92884f7 | 2010-04-20 15:03:56 +0000 | [diff] [blame] | 964 | if (!isa<TerminatorInst>(&I) && !HasTailCall) |
| 965 | CopyToExportRegsIfNeeded(&I); |
| 966 | |
Andrew Trick | ea5db0c | 2013-05-25 02:20:36 +0000 | [diff] [blame] | 967 | CurInst = NULL; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 970 | void SelectionDAGBuilder::visitPHI(const PHINode &) { |
| 971 | llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!"); |
| 972 | } |
| 973 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 974 | void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 975 | // Note: this doesn't use InstVisitor, because it has to work with |
| 976 | // ConstantExpr's in addition to instructions. |
| 977 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 978 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 979 | // Build the switch statement using the Instruction.def file. |
| 980 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Galina Kistanova | 72ea0c9 | 2012-07-19 04:50:12 +0000 | [diff] [blame] | 981 | case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break; |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 982 | #include "llvm/IR/Instruction.def" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 983 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 984 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 985 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 986 | // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, |
| 987 | // generate the debug data structures now that we've seen its definition. |
| 988 | void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V, |
| 989 | SDValue Val) { |
| 990 | DanglingDebugInfo &DDI = DanglingDebugInfoMap[V]; |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 991 | if (DDI.getDI()) { |
| 992 | const DbgValueInst *DI = DDI.getDI(); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 993 | DebugLoc dl = DDI.getdl(); |
| 994 | unsigned DbgSDNodeOrder = DDI.getSDNodeOrder(); |
Devang Patel | 4cf81c4 | 2010-08-26 23:35:15 +0000 | [diff] [blame] | 995 | MDNode *Variable = DI->getVariable(); |
| 996 | uint64_t Offset = DI->getOffset(); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 997 | SDDbgValue *SDV; |
| 998 | if (Val.getNode()) { |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 999 | if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1000 | SDV = DAG.getDbgValue(Variable, Val.getNode(), |
| 1001 | Val.getResNo(), Offset, dl, DbgSDNodeOrder); |
| 1002 | DAG.AddDbgValue(SDV, Val.getNode(), false); |
| 1003 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1004 | } else |
Adrian Prantl | 5da4e4f | 2013-05-22 18:02:19 +0000 | [diff] [blame] | 1005 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1006 | DanglingDebugInfoMap[V] = DanglingDebugInfo(); |
| 1007 | } |
| 1008 | } |
| 1009 | |
Nick Lewycky | 8de3400 | 2011-09-30 22:19:53 +0000 | [diff] [blame] | 1010 | /// getValue - Return an SDValue for the given Value. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1011 | SDValue SelectionDAGBuilder::getValue(const Value *V) { |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1012 | // If we already have an SDValue for this value, use it. It's important |
| 1013 | // to do this first, so that we don't create a CopyFromReg if we already |
| 1014 | // have a regular SDValue. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1015 | SDValue &N = NodeMap[V]; |
| 1016 | if (N.getNode()) return N; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1017 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1018 | // If there's a virtual register allocated and initialized for this |
| 1019 | // value, use it. |
| 1020 | DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V); |
| 1021 | if (It != FuncInfo.ValueMap.end()) { |
| 1022 | unsigned InReg = It->second; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1023 | RegsForValue RFV(*DAG.getContext(), *TM.getTargetLowering(), |
| 1024 | InReg, V->getType()); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1025 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1026 | N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V); |
Devang Patel | 8f31428 | 2011-01-25 18:09:58 +0000 | [diff] [blame] | 1027 | resolveDanglingDebugInfo(V, N); |
| 1028 | return N; |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | // Otherwise create a new SDValue and remember it. |
| 1032 | SDValue Val = getValueImpl(V); |
| 1033 | NodeMap[V] = Val; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1034 | resolveDanglingDebugInfo(V, Val); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1035 | return Val; |
| 1036 | } |
| 1037 | |
| 1038 | /// getNonRegisterValue - Return an SDValue for the given Value, but |
| 1039 | /// don't look in FuncInfo.ValueMap for a virtual register. |
| 1040 | SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) { |
| 1041 | // If we already have an SDValue for this value, use it. |
| 1042 | SDValue &N = NodeMap[V]; |
| 1043 | if (N.getNode()) return N; |
| 1044 | |
| 1045 | // Otherwise create a new SDValue and remember it. |
| 1046 | SDValue Val = getValueImpl(V); |
| 1047 | NodeMap[V] = Val; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1048 | resolveDanglingDebugInfo(V, Val); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1049 | return Val; |
| 1050 | } |
| 1051 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1052 | /// getValueImpl - Helper function for getValue and getNonRegisterValue. |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1053 | /// Create an SDValue for the given value. |
| 1054 | SDValue SelectionDAGBuilder::getValueImpl(const Value *V) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1055 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1056 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1057 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1058 | EVT VT = TLI->getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1059 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1060 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1061 | return DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1062 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1063 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1064 | return DAG.getGlobalAddress(GV, getCurSDLoc(), VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1065 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1066 | if (isa<ConstantPointerNull>(C)) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1067 | return DAG.getConstant(0, TLI->getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1068 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1069 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1070 | return DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1071 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1072 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1073 | return DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1074 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1075 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1076 | visit(CE->getOpcode(), *CE); |
| 1077 | SDValue N1 = NodeMap[V]; |
Dan Gohman | ac7d05c | 2010-04-16 16:55:18 +0000 | [diff] [blame] | 1078 | assert(N1.getNode() && "visit didn't populate the NodeMap!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1079 | return N1; |
| 1080 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1081 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1082 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 1083 | SmallVector<SDValue, 4> Constants; |
| 1084 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 1085 | OI != OE; ++OI) { |
| 1086 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 1087 | // If the operand is an empty aggregate, there are no values. |
| 1088 | if (!Val) continue; |
| 1089 | // Add each leaf value from the operand to the Constants list |
| 1090 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1091 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 1092 | Constants.push_back(SDValue(Val, i)); |
| 1093 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1094 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1095 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1096 | getCurSDLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1097 | } |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 1098 | |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1099 | if (const ConstantDataSequential *CDS = |
| 1100 | dyn_cast<ConstantDataSequential>(C)) { |
| 1101 | SmallVector<SDValue, 4> Ops; |
Chris Lattner | 0f193b8 | 2012-01-25 01:27:20 +0000 | [diff] [blame] | 1102 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1103 | SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode(); |
| 1104 | // Add each leaf value from the operand to the Constants list |
| 1105 | // to form a flattened list of all the values. |
| 1106 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 1107 | Ops.push_back(SDValue(Val, i)); |
| 1108 | } |
| 1109 | |
| 1110 | if (isa<ArrayType>(CDS->getType())) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1111 | return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc()); |
| 1112 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1113 | VT, &Ops[0], Ops.size()); |
| 1114 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1115 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1116 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1117 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 1118 | "Unknown struct or array constant!"); |
| 1119 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1120 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1121 | ComputeValueVTs(*TLI, C->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1122 | unsigned NumElts = ValueVTs.size(); |
| 1123 | if (NumElts == 0) |
| 1124 | return SDValue(); // empty struct |
| 1125 | SmallVector<SDValue, 4> Constants(NumElts); |
| 1126 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1127 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1128 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1129 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1130 | else if (EltVT.isFloatingPoint()) |
| 1131 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 1132 | else |
| 1133 | Constants[i] = DAG.getConstant(0, EltVT); |
| 1134 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1135 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1136 | return DAG.getMergeValues(&Constants[0], NumElts, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1137 | getCurSDLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1140 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1141 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1143 | VectorType *VecTy = cast<VectorType>(V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1144 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1145 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1146 | // Now that we know the number and type of the elements, get that number of |
| 1147 | // elements into the Ops array based on what kind of constant it is. |
| 1148 | SmallVector<SDValue, 16> Ops; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1149 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1150 | for (unsigned i = 0; i != NumElements; ++i) |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1151 | Ops.push_back(getValue(CV->getOperand(i))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1152 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1153 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1154 | EVT EltVT = TLI->getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1155 | |
| 1156 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1157 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1158 | Op = DAG.getConstantFP(0, EltVT); |
| 1159 | else |
| 1160 | Op = DAG.getConstant(0, EltVT); |
| 1161 | Ops.assign(NumElements, Op); |
| 1162 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1163 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1164 | // Create a BUILD_VECTOR node. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1165 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1166 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1167 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1168 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1169 | // If this is a static alloca, generate it as the frameindex instead of |
| 1170 | // computation. |
| 1171 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 1172 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 1173 | FuncInfo.StaticAllocaMap.find(AI); |
| 1174 | if (SI != FuncInfo.StaticAllocaMap.end()) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1175 | return DAG.getFrameIndex(SI->second, TLI->getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1176 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1177 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1178 | // If this is an instruction which fast-isel has deferred, select it now. |
| 1179 | if (const Instruction *Inst = dyn_cast<Instruction>(V)) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1180 | unsigned InReg = FuncInfo.InitializeRegForValue(Inst); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1181 | RegsForValue RFV(*DAG.getContext(), *TLI, InReg, Inst->getType()); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1182 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1183 | return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1184 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1185 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1186 | llvm_unreachable("Can't get register for value!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1189 | void SelectionDAGBuilder::visitRet(const ReturnInst &I) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1190 | const TargetLowering *TLI = TM.getTargetLowering(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1191 | SDValue Chain = getControlRoot(); |
| 1192 | SmallVector<ISD::OutputArg, 8> Outs; |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1193 | SmallVector<SDValue, 8> OutVals; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1194 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 1195 | if (!FuncInfo.CanLowerReturn) { |
| 1196 | unsigned DemoteReg = FuncInfo.DemoteRegister; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1197 | const Function *F = I.getParent()->getParent(); |
| 1198 | |
| 1199 | // Emit a store of the return value through the virtual register. |
| 1200 | // Leave Outs empty so that LowerReturn won't try to load return |
| 1201 | // registers the usual way. |
| 1202 | SmallVector<EVT, 1> PtrValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1203 | ComputeValueVTs(*TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1204 | PtrValueVTs); |
| 1205 | |
| 1206 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 1207 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1208 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1209 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1210 | SmallVector<uint64_t, 4> Offsets; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1211 | ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1212 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1213 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1214 | SmallVector<SDValue, 4> Chains(NumValues); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1215 | for (unsigned i = 0; i != NumValues; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1216 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 1217 | RetPtr.getValueType(), RetPtr, |
| 1218 | DAG.getIntPtrConstant(Offsets[i])); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1219 | Chains[i] = |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1220 | DAG.getStore(Chain, getCurSDLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1221 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 1222 | // FIXME: better loc info would be nice. |
| 1223 | Add, MachinePointerInfo(), false, false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1226 | Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1227 | MVT::Other, &Chains[0], NumValues); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 1228 | } else if (I.getNumOperands() != 0) { |
| 1229 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1230 | ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 1231 | unsigned NumValues = ValueVTs.size(); |
| 1232 | if (NumValues) { |
| 1233 | SDValue RetOp = getValue(I.getOperand(0)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1234 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 1235 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1236 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1237 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1238 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1239 | const Function *F = I.getParent()->getParent(); |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1240 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1241 | Attribute::SExt)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1242 | ExtendKind = ISD::SIGN_EXTEND; |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1243 | else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1244 | Attribute::ZExt)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1245 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1246 | |
Cameron Zwarich | 7bbf0ee | 2011-03-17 14:53:37 +0000 | [diff] [blame] | 1247 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1248 | VT = TLI->getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1249 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1250 | unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), VT); |
| 1251 | MVT PartVT = TLI->getRegisterType(*DAG.getContext(), VT); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1252 | SmallVector<SDValue, 4> Parts(NumParts); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1253 | getCopyToParts(DAG, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1254 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 1255 | &Parts[0], NumParts, PartVT, &I, ExtendKind); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1256 | |
| 1257 | // 'inreg' on function refers to return value |
| 1258 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1259 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1260 | Attribute::InReg)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1261 | Flags.setInReg(); |
| 1262 | |
| 1263 | // Propagate extension type if any |
Cameron Zwarich | 8df6bf5 | 2011-03-16 22:20:07 +0000 | [diff] [blame] | 1264 | if (ExtendKind == ISD::SIGN_EXTEND) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1265 | Flags.setSExt(); |
Cameron Zwarich | 8df6bf5 | 2011-03-16 22:20:07 +0000 | [diff] [blame] | 1266 | else if (ExtendKind == ISD::ZERO_EXTEND) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1267 | Flags.setZExt(); |
| 1268 | |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1269 | for (unsigned i = 0; i < NumParts; ++i) { |
| 1270 | Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(), |
Manman Ren | 0a1544d | 2012-11-01 23:49:58 +0000 | [diff] [blame] | 1271 | /*isfixed=*/true, 0, 0)); |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1272 | OutVals.push_back(Parts[i]); |
| 1273 | } |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1274 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1275 | } |
| 1276 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1277 | |
| 1278 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1279 | CallingConv::ID CallConv = |
| 1280 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1281 | Chain = TM.getTargetLowering()->LowerReturn(Chain, CallConv, isVarArg, |
| 1282 | Outs, OutVals, getCurSDLoc(), |
| 1283 | DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1284 | |
| 1285 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1286 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1287 | "LowerReturn didn't return a valid chain!"); |
| 1288 | |
| 1289 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1290 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1293 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1294 | /// created for it, emit nodes to copy the value into the virtual |
| 1295 | /// registers. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1296 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) { |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 1297 | // Skip empty types |
| 1298 | if (V->getType()->isEmptyTy()) |
| 1299 | return; |
| 1300 | |
Dan Gohman | 33b7a29 | 2010-04-16 17:15:02 +0000 | [diff] [blame] | 1301 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1302 | if (VMI != FuncInfo.ValueMap.end()) { |
| 1303 | assert(!V->use_empty() && "Unused value assigned virtual registers!"); |
| 1304 | CopyValueToVirtualRegister(V, VMI->second); |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1308 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1309 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1310 | /// CopyTo/FromReg. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1311 | void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1312 | // No need to export constants. |
| 1313 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1314 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1315 | // Already exported? |
| 1316 | if (FuncInfo.isExportedInst(V)) return; |
| 1317 | |
| 1318 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1319 | CopyValueToVirtualRegister(V, Reg); |
| 1320 | } |
| 1321 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1322 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1323 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1324 | // The operands of the setcc have to be in this block. We don't know |
| 1325 | // how to export them from some other block. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1326 | if (const Instruction *VI = dyn_cast<Instruction>(V)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1327 | // Can export from current BB. |
| 1328 | if (VI->getParent() == FromBB) |
| 1329 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1330 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1331 | // Is already exported, noop. |
| 1332 | return FuncInfo.isExportedInst(V); |
| 1333 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1334 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1335 | // If this is an argument, we can export it if the BB is the entry block or |
| 1336 | // if it is already exported. |
| 1337 | if (isa<Argument>(V)) { |
| 1338 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1339 | return true; |
| 1340 | |
| 1341 | // Otherwise, can only export this if it is already exported. |
| 1342 | return FuncInfo.isExportedInst(V); |
| 1343 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1344 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1345 | // Otherwise, constants can always be exported. |
| 1346 | return true; |
| 1347 | } |
| 1348 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1349 | /// Return branch probability calculated by BranchProbabilityInfo for IR blocks. |
Jakub Staszak | 25101bb | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 1350 | uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src, |
| 1351 | const MachineBasicBlock *Dst) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1352 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
| 1353 | if (!BPI) |
| 1354 | return 0; |
Jakub Staszak | 95ece8e | 2011-07-29 20:05:36 +0000 | [diff] [blame] | 1355 | const BasicBlock *SrcBB = Src->getBasicBlock(); |
| 1356 | const BasicBlock *DstBB = Dst->getBasicBlock(); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1357 | return BPI->getEdgeWeight(SrcBB, DstBB); |
| 1358 | } |
| 1359 | |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 1360 | void SelectionDAGBuilder:: |
| 1361 | addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst, |
| 1362 | uint32_t Weight /* = 0 */) { |
| 1363 | if (!Weight) |
| 1364 | Weight = getEdgeWeight(Src, Dst); |
| 1365 | Src->addSuccessor(Dst, Weight); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1369 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1370 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1371 | return I->getParent() == BB; |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1375 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1376 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1377 | /// AND operator tree. |
| 1378 | /// |
| 1379 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1380 | SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1381 | MachineBasicBlock *TBB, |
| 1382 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1383 | MachineBasicBlock *CurBB, |
| 1384 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1385 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1386 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1387 | // If the leaf of the tree is a comparison, merge the condition into |
| 1388 | // the caseblock. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1389 | if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1390 | // The operands of the cmp have to be in this block. We don't know |
| 1391 | // how to export them from some other block. If this is the first block |
| 1392 | // of the sequence, no exporting is needed. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1393 | if (CurBB == SwitchBB || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1394 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1395 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1396 | ISD::CondCode Condition; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1397 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1398 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1399 | } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1400 | Condition = getFCmpCondCode(FC->getPredicate()); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1401 | if (TM.Options.NoNaNsFPMath) |
| 1402 | Condition = getFCmpCodeWithoutNaN(Condition); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1403 | } else { |
| 1404 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1405 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1406 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1407 | |
| 1408 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1409 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1410 | SwitchCases.push_back(CB); |
| 1411 | return; |
| 1412 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1416 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1417 | NULL, TBB, FBB, CurBB); |
| 1418 | SwitchCases.push_back(CB); |
| 1419 | } |
| 1420 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1421 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1422 | void SelectionDAGBuilder::FindMergedConditions(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1423 | MachineBasicBlock *TBB, |
| 1424 | MachineBasicBlock *FBB, |
| 1425 | MachineBasicBlock *CurBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1426 | MachineBasicBlock *SwitchBB, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1427 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1428 | // 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] | 1429 | const Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1430 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1431 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1432 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1433 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1434 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1435 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1436 | return; |
| 1437 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1438 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1439 | // Create TmpBB after CurBB. |
| 1440 | MachineFunction::iterator BBI = CurBB; |
| 1441 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1442 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1443 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1444 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1445 | if (Opc == Instruction::Or) { |
| 1446 | // Codegen X | Y as: |
| 1447 | // jmp_if_X TBB |
| 1448 | // jmp TmpBB |
| 1449 | // TmpBB: |
| 1450 | // jmp_if_Y TBB |
| 1451 | // jmp FBB |
| 1452 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1453 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1454 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1455 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1456 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1457 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1458 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1459 | } else { |
| 1460 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1461 | // Codegen X & Y as: |
| 1462 | // jmp_if_X TmpBB |
| 1463 | // jmp FBB |
| 1464 | // TmpBB: |
| 1465 | // jmp_if_Y TBB |
| 1466 | // jmp FBB |
| 1467 | // |
| 1468 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1469 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1470 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1471 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1472 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1473 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1474 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1479 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1480 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1481 | bool |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 1482 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1483 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1484 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1485 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1486 | // will get folded into a single comparison, so don't emit two blocks. |
| 1487 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1488 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1489 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1490 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1491 | return false; |
| 1492 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1493 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1494 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1495 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1496 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1497 | Cases[0].CC == Cases[1].CC && |
| 1498 | isa<Constant>(Cases[0].CmpRHS) && |
| 1499 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1500 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1501 | return false; |
| 1502 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1503 | return false; |
| 1504 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 1505 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1506 | return true; |
| 1507 | } |
| 1508 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1509 | void SelectionDAGBuilder::visitBr(const BranchInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1510 | MachineBasicBlock *BrMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1511 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1512 | // Update machine-CFG edges. |
| 1513 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1514 | |
| 1515 | // Figure out which block is immediately after the current one. |
| 1516 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1517 | MachineFunction::iterator BBI = BrMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1518 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1519 | NextBlock = BBI; |
| 1520 | |
| 1521 | if (I.isUnconditional()) { |
| 1522 | // Update machine-CFG edges. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1523 | BrMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1524 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1525 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1526 | if (Succ0MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1527 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1528 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1529 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1530 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1531 | return; |
| 1532 | } |
| 1533 | |
| 1534 | // If this condition is one of the special cases we handle, do special stuff |
| 1535 | // now. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1536 | const Value *CondVal = I.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1537 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1538 | |
| 1539 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1540 | // 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] | 1541 | // As long as jumps are not expensive, this should improve performance. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1542 | // For example, instead of something like: |
| 1543 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1544 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1545 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1546 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1547 | // or C, F |
| 1548 | // jnz foo |
| 1549 | // Emit: |
| 1550 | // cmp A, B |
| 1551 | // je foo |
| 1552 | // cmp D, E |
| 1553 | // jle foo |
| 1554 | // |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1555 | if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1556 | if (!TM.getTargetLowering()->isJumpExpensive() && |
Chris Lattner | de189be | 2010-11-30 18:12:52 +0000 | [diff] [blame] | 1557 | BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1558 | (BOp->getOpcode() == Instruction::And || |
| 1559 | BOp->getOpcode() == Instruction::Or)) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1560 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, |
| 1561 | BOp->getOpcode()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1562 | // If the compares in later blocks need to use values not currently |
| 1563 | // exported from this block, export them now. This block should always |
| 1564 | // be the first entry. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1565 | assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1566 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1567 | // Allow some cases to be rejected. |
| 1568 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1569 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1570 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1571 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1572 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1573 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1574 | // Emit the branch for this block. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1575 | visitSwitchCase(SwitchCases[0], BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1576 | SwitchCases.erase(SwitchCases.begin()); |
| 1577 | return; |
| 1578 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1579 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1580 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1581 | // SwitchCases. |
| 1582 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1583 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1584 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1585 | SwitchCases.clear(); |
| 1586 | } |
| 1587 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1588 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1589 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1590 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1591 | NULL, Succ0MBB, Succ1MBB, BrMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1592 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1593 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1594 | // cond branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1595 | visitSwitchCase(CB, BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1599 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1600 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB, |
| 1601 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1602 | SDValue Cond; |
| 1603 | SDValue CondLHS = getValue(CB.CmpLHS); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1604 | SDLoc dl = getCurSDLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1605 | |
| 1606 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1607 | if (CB.CmpMHS == NULL) { |
| 1608 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1609 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1610 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1611 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1612 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1613 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1614 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1615 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1616 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1617 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1618 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1619 | } else { |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 1620 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1621 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1622 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1623 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1624 | |
| 1625 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1626 | EVT VT = CmpOp.getValueType(); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 1627 | |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 1628 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1629 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 1630 | ISD::SETLE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1631 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1632 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1633 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1634 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1635 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1636 | } |
| 1637 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1638 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1639 | // Update successor info |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 1640 | addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight); |
Jakob Stoklund Olesen | e7fdef4 | 2012-08-20 21:39:52 +0000 | [diff] [blame] | 1641 | // TrueBB and FalseBB are always different unless the incoming IR is |
| 1642 | // degenerate. This only happens when running llc on weird IR. |
| 1643 | if (CB.TrueBB != CB.FalseBB) |
| 1644 | addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1645 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1646 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1647 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1648 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1649 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1650 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1651 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1652 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1653 | // If the lhs block is the next block, invert the condition so that we can |
| 1654 | // fall through to the lhs instead of the rhs block. |
| 1655 | if (CB.TrueBB == NextBlock) { |
| 1656 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1657 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1658 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1659 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1660 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1661 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1662 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1663 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1664 | |
Evan Cheng | 266a99d | 2010-09-23 06:51:55 +0000 | [diff] [blame] | 1665 | // Insert the false branch. Do this even if it's a fall through branch, |
| 1666 | // this makes it easier to do DAG optimizations which require inverting |
| 1667 | // the branch condition. |
| 1668 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1669 | DAG.getBasicBlock(CB.FalseBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1670 | |
| 1671 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1675 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1676 | // Emit the code for the jump table |
| 1677 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1678 | EVT PTy = TM.getTargetLowering()->getPointerTy(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1679 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1680 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1681 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1682 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(), |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1683 | MVT::Other, Index.getValue(1), |
| 1684 | Table, Index); |
| 1685 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1689 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1690 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1691 | JumpTableHeader &JTH, |
| 1692 | MachineBasicBlock *SwitchBB) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1693 | // Subtract the lowest switch case value from the value being switched on and |
| 1694 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1695 | // difference between smallest and largest cases. |
| 1696 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1697 | EVT VT = SwitchOp.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1698 | SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1699 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1700 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1701 | // 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] | 1702 | // 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] | 1703 | // can be used as an index into the jump table in a subsequent basic block. |
| 1704 | // This value may be smaller or larger than the target's pointer type, and |
| 1705 | // therefore require extension or truncating. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1706 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1707 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI->getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1708 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1709 | unsigned JumpTableReg = FuncInfo.CreateReg(TLI->getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1710 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1711 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1712 | JT.Reg = JumpTableReg; |
| 1713 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1714 | // Emit the range check for the jump table, and branch to the default block |
| 1715 | // for the switch statement if the value being switched on exceeds the largest |
| 1716 | // case in the switch. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1717 | SDValue CMP = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1718 | TLI->getSetCCResultType(*DAG.getContext(), |
| 1719 | Sub.getValueType()), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1720 | Sub, |
| 1721 | DAG.getConstant(JTH.Last - JTH.First,VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1722 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1723 | |
| 1724 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1725 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1726 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1727 | MachineFunction::iterator BBI = SwitchBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1728 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1729 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1730 | NextBlock = BBI; |
| 1731 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1732 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1733 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1734 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1735 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1736 | if (JT.MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1737 | BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond, |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1738 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1739 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1740 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 1743 | /// Codegen a new tail for a stack protector check ParentMBB which has had its |
| 1744 | /// tail spliced into a stack protector check success bb. |
| 1745 | /// |
| 1746 | /// For a high level explanation of how this fits into the stack protector |
| 1747 | /// generation see the comment on the declaration of class |
| 1748 | /// StackProtectorDescriptor. |
| 1749 | void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD, |
| 1750 | MachineBasicBlock *ParentBB) { |
| 1751 | |
| 1752 | // First create the loads to the guard/stack slot for the comparison. |
| 1753 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1754 | EVT PtrTy = TLI->getPointerTy(); |
| 1755 | |
| 1756 | MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo(); |
| 1757 | int FI = MFI->getStackProtectorIndex(); |
| 1758 | |
| 1759 | const Value *IRGuard = SPD.getGuard(); |
| 1760 | SDValue GuardPtr = getValue(IRGuard); |
| 1761 | SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy); |
| 1762 | |
| 1763 | unsigned Align = |
| 1764 | TLI->getDataLayout()->getPrefTypeAlignment(IRGuard->getType()); |
| 1765 | SDValue Guard = DAG.getLoad(PtrTy, getCurSDLoc(), DAG.getEntryNode(), |
| 1766 | GuardPtr, MachinePointerInfo(IRGuard, 0), |
| 1767 | true, false, false, Align); |
| 1768 | |
| 1769 | SDValue StackSlot = DAG.getLoad(PtrTy, getCurSDLoc(), DAG.getEntryNode(), |
| 1770 | StackSlotPtr, |
| 1771 | MachinePointerInfo::getFixedStack(FI), |
| 1772 | true, false, false, Align); |
| 1773 | |
| 1774 | // Perform the comparison via a subtract/getsetcc. |
| 1775 | EVT VT = Guard.getValueType(); |
| 1776 | SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, Guard, StackSlot); |
| 1777 | |
| 1778 | SDValue Cmp = DAG.getSetCC(getCurSDLoc(), |
| 1779 | TLI->getSetCCResultType(*DAG.getContext(), |
| 1780 | Sub.getValueType()), |
| 1781 | Sub, DAG.getConstant(0, VT), |
| 1782 | ISD::SETNE); |
| 1783 | |
| 1784 | // If the sub is not 0, then we know the guard/stackslot do not equal, so |
| 1785 | // branch to failure MBB. |
| 1786 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
| 1787 | MVT::Other, StackSlot.getOperand(0), |
| 1788 | Cmp, DAG.getBasicBlock(SPD.getFailureMBB())); |
| 1789 | // Otherwise branch to success MBB. |
| 1790 | SDValue Br = DAG.getNode(ISD::BR, getCurSDLoc(), |
| 1791 | MVT::Other, BrCond, |
| 1792 | DAG.getBasicBlock(SPD.getSuccessMBB())); |
| 1793 | |
| 1794 | DAG.setRoot(Br); |
| 1795 | } |
| 1796 | |
| 1797 | /// Codegen the failure basic block for a stack protector check. |
| 1798 | /// |
| 1799 | /// A failure stack protector machine basic block consists simply of a call to |
| 1800 | /// __stack_chk_fail(). |
| 1801 | /// |
| 1802 | /// For a high level explanation of how this fits into the stack protector |
| 1803 | /// generation see the comment on the declaration of class |
| 1804 | /// StackProtectorDescriptor. |
| 1805 | void |
| 1806 | SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) { |
| 1807 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1808 | SDValue Chain = TLI->makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, |
| 1809 | MVT::isVoid, 0, 0, false, getCurSDLoc(), |
Michael Gottesman | 58a9b43 | 2013-08-22 23:45:24 +0000 | [diff] [blame] | 1810 | false, false).second; |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 1811 | DAG.setRoot(Chain); |
| 1812 | } |
| 1813 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1814 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1815 | /// suitable for "bit tests" |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1816 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B, |
| 1817 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1818 | // Subtract the minimum value |
| 1819 | SDValue SwitchOp = getValue(B.SValue); |
Patrik Hagglund | 34525f9 | 2012-12-11 11:14:33 +0000 | [diff] [blame] | 1820 | EVT VT = SwitchOp.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1821 | SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1822 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1823 | |
| 1824 | // Check range |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1825 | const TargetLowering *TLI = TM.getTargetLowering(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1826 | SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1827 | TLI->getSetCCResultType(*DAG.getContext(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1828 | Sub.getValueType()), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1829 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1830 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1831 | |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1832 | // Determine the type of the test operands. |
| 1833 | bool UsePtrType = false; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1834 | if (!TLI->isTypeLegal(VT)) |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1835 | UsePtrType = true; |
| 1836 | else { |
| 1837 | for (unsigned i = 0, e = B.Cases.size(); i != e; ++i) |
Eli Friedman | 5c75af6 | 2011-10-12 22:46:45 +0000 | [diff] [blame] | 1838 | if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) { |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1839 | // Switch table case range are encoded into series of masks. |
| 1840 | // Just use pointer type, it's guaranteed to fit. |
| 1841 | UsePtrType = true; |
| 1842 | break; |
| 1843 | } |
| 1844 | } |
| 1845 | if (UsePtrType) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1846 | VT = TLI->getPointerTy(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1847 | Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT); |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1848 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1849 | |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1850 | B.RegVT = VT.getSimpleVT(); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 1851 | B.Reg = FuncInfo.CreateReg(B.RegVT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1852 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1853 | B.Reg, Sub); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1854 | |
| 1855 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1856 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1857 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1858 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1859 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1860 | NextBlock = BBI; |
| 1861 | |
| 1862 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1863 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1864 | addSuccessorWithWeight(SwitchBB, B.Default); |
| 1865 | addSuccessorWithWeight(SwitchBB, MBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1866 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1867 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1868 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1869 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1870 | |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1871 | if (MBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1872 | BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo, |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1873 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1874 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1875 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | /// visitBitTestCase - this function produces one "bit test" |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1879 | void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB, |
| 1880 | MachineBasicBlock* NextMBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1881 | uint32_t BranchWeightToNext, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1882 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1883 | BitTestCase &B, |
| 1884 | MachineBasicBlock *SwitchBB) { |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 1885 | MVT VT = BB.RegVT; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1886 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1887 | Reg, VT); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1888 | SDValue Cmp; |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1889 | unsigned PopCount = CountPopulation_64(B.Mask); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1890 | const TargetLowering *TLI = TM.getTargetLowering(); |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1891 | if (PopCount == 1) { |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1892 | // Testing for a single bit; just compare the shift count with what it |
| 1893 | // would need to be to shift a 1 bit in that position. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1894 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1895 | TLI->getSetCCResultType(*DAG.getContext(), VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1896 | ShiftOp, |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1897 | DAG.getConstant(countTrailingZeros(B.Mask), VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1898 | ISD::SETEQ); |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1899 | } else if (PopCount == BB.Range) { |
| 1900 | // 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] | 1901 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1902 | TLI->getSetCCResultType(*DAG.getContext(), VT), |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1903 | ShiftOp, |
| 1904 | DAG.getConstant(CountTrailingOnes_64(B.Mask), VT), |
| 1905 | ISD::SETNE); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1906 | } else { |
| 1907 | // Make desired shift |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1908 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT, |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1909 | DAG.getConstant(1, VT), ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1910 | |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1911 | // Emit bit tests and jumps |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1912 | SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1913 | VT, SwitchVal, DAG.getConstant(B.Mask, VT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1914 | Cmp = DAG.getSetCC(getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1915 | TLI->getSetCCResultType(*DAG.getContext(), VT), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1916 | AndOp, DAG.getConstant(0, VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1917 | ISD::SETNE); |
| 1918 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1919 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1920 | // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight. |
| 1921 | addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight); |
| 1922 | // The branch weight from SwitchBB to NextMBB is BranchWeightToNext. |
| 1923 | addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1924 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1925 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1926 | MVT::Other, getControlRoot(), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1927 | Cmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1928 | |
| 1929 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1930 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1931 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1932 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1933 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1934 | NextBlock = BBI; |
| 1935 | |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1936 | if (NextMBB != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1937 | BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd, |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1938 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1939 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1940 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1943 | void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1944 | MachineBasicBlock *InvokeMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1945 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1946 | // Retrieve successors. |
| 1947 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1948 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1949 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1950 | const Value *Callee(I.getCalledValue()); |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1951 | const Function *Fn = dyn_cast<Function>(Callee); |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1952 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1953 | visitInlineAsm(&I); |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1954 | else if (Fn && Fn->isIntrinsic()) { |
| 1955 | assert(Fn->getIntrinsicID() == Intrinsic::donothing); |
Nuno Lopes | 4532bf6 | 2012-07-18 00:07:17 +0000 | [diff] [blame] | 1956 | // Ignore invokes to @llvm.donothing: jump directly to the next BB. |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1957 | } else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1958 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1959 | |
| 1960 | // If the value of the invoke is used outside of its defining block, make it |
| 1961 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1962 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1963 | |
| 1964 | // Update successor info |
Chandler Carruth | f264568 | 2011-11-22 11:37:46 +0000 | [diff] [blame] | 1965 | addSuccessorWithWeight(InvokeMBB, Return); |
| 1966 | addSuccessorWithWeight(InvokeMBB, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1967 | |
| 1968 | // Drop into normal successor. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1969 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1970 | MVT::Other, getControlRoot(), |
| 1971 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 1974 | void SelectionDAGBuilder::visitResume(const ResumeInst &RI) { |
| 1975 | llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!"); |
| 1976 | } |
| 1977 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1978 | void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) { |
| 1979 | assert(FuncInfo.MBB->isLandingPad() && |
| 1980 | "Call to landingpad not in landing pad!"); |
| 1981 | |
| 1982 | MachineBasicBlock *MBB = FuncInfo.MBB; |
| 1983 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 1984 | AddLandingPadInfo(LP, MMI, MBB); |
| 1985 | |
Bill Wendling | bdf9db6 | 2012-02-13 23:47:16 +0000 | [diff] [blame] | 1986 | // If there aren't registers to copy the values into (e.g., during SjLj |
| 1987 | // exceptions), then don't bother to create these DAG nodes. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1988 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1989 | if (TLI->getExceptionPointerRegister() == 0 && |
| 1990 | TLI->getExceptionSelectorRegister() == 0) |
Bill Wendling | bdf9db6 | 2012-02-13 23:47:16 +0000 | [diff] [blame] | 1991 | return; |
| 1992 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1993 | SmallVector<EVT, 2> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1994 | ComputeValueVTs(*TLI, LP.getType(), ValueVTs); |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 1995 | assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported"); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1996 | |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 1997 | // Get the two live-in registers as SDValues. The physregs have already been |
| 1998 | // copied into virtual registers. |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1999 | SDValue Ops[2]; |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 2000 | Ops[0] = DAG.getZExtOrTrunc( |
| 2001 | DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(), |
| 2002 | FuncInfo.ExceptionPointerVirtReg, TLI->getPointerTy()), |
| 2003 | getCurSDLoc(), ValueVTs[0]); |
| 2004 | Ops[1] = DAG.getZExtOrTrunc( |
| 2005 | DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(), |
| 2006 | FuncInfo.ExceptionSelectorVirtReg, TLI->getPointerTy()), |
| 2007 | getCurSDLoc(), ValueVTs[1]); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 2008 | |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 2009 | // Merge into one. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2010 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 2011 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 2012 | &Ops[0], 2); |
Jakob Stoklund Olesen | 918b7c8 | 2013-07-04 04:53:45 +0000 | [diff] [blame] | 2013 | setValue(&LP, Res); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2016 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 2017 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2018 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 2019 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2020 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2021 | MachineBasicBlock *Default, |
| 2022 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2023 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2024 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2025 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2026 | return false; |
| 2027 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2028 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2029 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2030 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Figure out which block is immediately after the current one. |
| 2033 | MachineBasicBlock *NextBlock = 0; |
| 2034 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2035 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2036 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2037 | NextBlock = BBI; |
| 2038 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2039 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 2040 | // 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] | 2041 | // is the same as the other, but has one bit unset that the other has set, |
| 2042 | // use bit manipulation to do two compares at once. For example: |
| 2043 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 2044 | // TODO: This could be extended to merge any 2 cases in switches with 3 cases. |
| 2045 | // TODO: Handle cases where CR.CaseBB != SwitchBB. |
| 2046 | if (Size == 2 && CR.CaseBB == SwitchBB) { |
| 2047 | Case &Small = *CR.Range.first; |
| 2048 | Case &Big = *(CR.Range.second-1); |
| 2049 | |
| 2050 | if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) { |
| 2051 | const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue(); |
| 2052 | const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue(); |
| 2053 | |
| 2054 | // Check that there is only one bit different. |
| 2055 | if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 && |
| 2056 | (SmallValue | BigValue) == BigValue) { |
| 2057 | // Isolate the common bit. |
| 2058 | APInt CommonBit = BigValue & ~SmallValue; |
| 2059 | assert((SmallValue | CommonBit) == BigValue && |
| 2060 | CommonBit.countPopulation() == 1 && "Not a common bit?"); |
| 2061 | |
| 2062 | SDValue CondLHS = getValue(SV); |
| 2063 | EVT VT = CondLHS.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2064 | SDLoc DL = getCurSDLoc(); |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 2065 | |
| 2066 | SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS, |
| 2067 | DAG.getConstant(CommonBit, VT)); |
| 2068 | SDValue Cond = DAG.getSetCC(DL, MVT::i1, |
| 2069 | Or, DAG.getConstant(BigValue, VT), |
| 2070 | ISD::SETEQ); |
| 2071 | |
| 2072 | // Update successor info. |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2073 | // Both Small and Big will jump to Small.BB, so we sum up the weights. |
| 2074 | addSuccessorWithWeight(SwitchBB, Small.BB, |
| 2075 | Small.ExtraWeight + Big.ExtraWeight); |
| 2076 | addSuccessorWithWeight(SwitchBB, Default, |
| 2077 | // The default destination is the first successor in IR. |
| 2078 | BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0); |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 2079 | |
| 2080 | // Insert the true branch. |
| 2081 | SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other, |
| 2082 | getControlRoot(), Cond, |
| 2083 | DAG.getBasicBlock(Small.BB)); |
| 2084 | |
| 2085 | // Insert the false branch. |
| 2086 | BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond, |
| 2087 | DAG.getBasicBlock(Default)); |
| 2088 | |
| 2089 | DAG.setRoot(BrCond); |
| 2090 | return true; |
| 2091 | } |
| 2092 | } |
| 2093 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2094 | |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2095 | // 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] | 2096 | uint32_t UnhandledWeights = 0; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2097 | if (BPI) { |
| 2098 | 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] | 2099 | uint32_t IWeight = I->ExtraWeight; |
| 2100 | UnhandledWeights += IWeight; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2101 | for (CaseItr J = CR.Range.first; J < I; ++J) { |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2102 | uint32_t JWeight = J->ExtraWeight; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2103 | if (IWeight > JWeight) |
| 2104 | std::swap(*I, *J); |
| 2105 | } |
| 2106 | } |
| 2107 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2108 | // 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] | 2109 | Case &BackCase = *(CR.Range.second-1); |
Benjamin Kramer | 5db954d | 2012-05-26 21:19:12 +0000 | [diff] [blame] | 2110 | if (Size > 1 && |
| 2111 | NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2112 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 2113 | // branches in this order. See if rearranging a case value would help. |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2114 | // 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] | 2115 | 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] | 2116 | if (I->BB == NextBlock) { |
| 2117 | std::swap(*I, BackCase); |
| 2118 | break; |
| 2119 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2120 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2121 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2122 | // Create a CaseBlock record representing a conditional branch to |
| 2123 | // the Case's target mbb if the value being switched on SV is equal |
| 2124 | // to C. |
| 2125 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 2126 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 2127 | MachineBasicBlock *FallThrough; |
| 2128 | if (I != E-1) { |
| 2129 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 2130 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2131 | |
| 2132 | // Put SV in a virtual register to make it available from the new blocks. |
| 2133 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2134 | } else { |
| 2135 | // If the last case doesn't match, go to the default block. |
| 2136 | FallThrough = Default; |
| 2137 | } |
| 2138 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2139 | const Value *RHS, *LHS, *MHS; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2140 | ISD::CondCode CC; |
| 2141 | if (I->High == I->Low) { |
| 2142 | // This is just small small case range :) containing exactly 1 case |
| 2143 | CC = ISD::SETEQ; |
| 2144 | LHS = SV; RHS = I->High; MHS = NULL; |
| 2145 | } else { |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2146 | CC = ISD::SETLE; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2147 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 2148 | } |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2149 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2150 | // The false weight should be sum of all un-handled cases. |
| 2151 | UnhandledWeights -= I->ExtraWeight; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2152 | CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough, |
| 2153 | /* me */ CurBlock, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2154 | /* trueweight */ I->ExtraWeight, |
| 2155 | /* falseweight */ UnhandledWeights); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2156 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2157 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 2158 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 2159 | // vector to be later processed by SDISel, and insert the node's MBB |
| 2160 | // before the next MBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2161 | if (CurBlock == SwitchBB) |
| 2162 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2163 | else |
| 2164 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2165 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2166 | CurBlock = FallThrough; |
| 2167 | } |
| 2168 | |
| 2169 | return true; |
| 2170 | } |
| 2171 | |
| 2172 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 2173 | return TLI.supportJumpTables() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2174 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 2175 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2176 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2177 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2178 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2179 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2180 | APInt LastExt = Last.sext(BitWidth), FirstExt = First.sext(BitWidth); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2181 | return (LastExt - FirstExt + 1ULL); |
| 2182 | } |
| 2183 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2184 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Chris Lattner | c3ab388 | 2011-09-09 22:06:59 +0000 | [diff] [blame] | 2185 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR, |
| 2186 | CaseRecVector &WorkList, |
| 2187 | const Value *SV, |
| 2188 | MachineBasicBlock *Default, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2189 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2190 | Case& FrontCase = *CR.Range.first; |
| 2191 | Case& BackCase = *(CR.Range.second-1); |
| 2192 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2193 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2194 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2195 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2196 | APInt TSize(First.getBitWidth(), 0); |
Chris Lattner | c3ab388 | 2011-09-09 22:06:59 +0000 | [diff] [blame] | 2197 | 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] | 2198 | TSize += I->size(); |
| 2199 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2200 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2201 | if (!areJTsAllowed(*TLI) || TSize.ult(TLI->getMinimumJumpTableEntries())) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2202 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2203 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2204 | APInt Range = ComputeRange(First, Last); |
Jakob Stoklund Olesen | 7944391 | 2011-10-26 01:47:48 +0000 | [diff] [blame] | 2205 | // The density is TSize / Range. Require at least 40%. |
| 2206 | // It should not be possible for IntTSize to saturate for sane code, but make |
| 2207 | // sure we handle Range saturation correctly. |
| 2208 | uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10); |
| 2209 | uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10); |
| 2210 | if (IntTSize * 10 < IntRange * 4) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2211 | return false; |
| 2212 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2213 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2214 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
Jakob Stoklund Olesen | 7944391 | 2011-10-26 01:47:48 +0000 | [diff] [blame] | 2215 | << "Range: " << Range << ". Size: " << TSize << ".\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2216 | |
| 2217 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2218 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2219 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2220 | |
| 2221 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2222 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 2223 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2224 | |
| 2225 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2226 | |
| 2227 | // Create a new basic block to hold the code for loading the address |
| 2228 | // of the jump table, and jumping to it. Update successor information; |
| 2229 | // we will either branch to the default case for the switch, or the jump |
| 2230 | // table. |
| 2231 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2232 | CurMF->insert(BBI, JumpTableBB); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 2233 | |
| 2234 | addSuccessorWithWeight(CR.CaseBB, Default); |
| 2235 | addSuccessorWithWeight(CR.CaseBB, JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2236 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2237 | // Build a vector of destination BBs, corresponding to each target |
| 2238 | // of the jump table. If the value of the jump table slot corresponds to |
| 2239 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 2240 | // the default BB. |
| 2241 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2242 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2243 | 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] | 2244 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 2245 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2246 | |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2247 | if (Low.sle(TEI) && TEI.sle(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2248 | DestBBs.push_back(I->BB); |
| 2249 | if (TEI==High) |
| 2250 | ++I; |
| 2251 | } else { |
| 2252 | DestBBs.push_back(Default); |
| 2253 | } |
| 2254 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2255 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2256 | // Calculate weight for each unique destination in CR. |
| 2257 | DenseMap<MachineBasicBlock*, uint32_t> DestWeights; |
| 2258 | if (FuncInfo.BPI) |
| 2259 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 2260 | DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr = |
| 2261 | DestWeights.find(I->BB); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2262 | if (Itr != DestWeights.end()) |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2263 | Itr->second += I->ExtraWeight; |
| 2264 | else |
| 2265 | DestWeights[I->BB] = I->ExtraWeight; |
| 2266 | } |
| 2267 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2268 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2269 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 2270 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2271 | E = DestBBs.end(); I != E; ++I) { |
| 2272 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 2273 | SuccsHandled[(*I)->getNumber()] = true; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2274 | DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr = |
| 2275 | DestWeights.find(*I); |
| 2276 | addSuccessorWithWeight(JumpTableBB, *I, |
| 2277 | Itr != DestWeights.end() ? Itr->second : 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2278 | } |
| 2279 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2280 | |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 2281 | // Create a jump table index for this jump table. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2282 | unsigned JTEncoding = TLI->getJumpTableEncoding(); |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 2283 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 2284 | ->createJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2285 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2286 | // Set the jump table information so that we can codegen it as a second |
| 2287 | // MachineBasicBlock |
| 2288 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2289 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB)); |
| 2290 | if (CR.CaseBB == SwitchBB) |
| 2291 | visitJumpTableHeader(JT, JTH, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2292 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2293 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2294 | return true; |
| 2295 | } |
| 2296 | |
| 2297 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 2298 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2299 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 2300 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2301 | const Value* SV, |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2302 | MachineBasicBlock* Default, |
| 2303 | MachineBasicBlock* SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2304 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2305 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2306 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2307 | |
| 2308 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2309 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 2310 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2311 | |
| 2312 | Case& FrontCase = *CR.Range.first; |
| 2313 | Case& BackCase = *(CR.Range.second-1); |
| 2314 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2315 | |
| 2316 | // Size is the number of Cases represented by this range. |
| 2317 | unsigned Size = CR.Range.second - CR.Range.first; |
| 2318 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2319 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2320 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2321 | double FMetric = 0; |
| 2322 | CaseItr Pivot = CR.Range.first + Size/2; |
| 2323 | |
| 2324 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 2325 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2326 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2327 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2328 | I!=E; ++I) |
| 2329 | TSize += I->size(); |
| 2330 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2331 | APInt LSize = FrontCase.size(); |
| 2332 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2333 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2334 | << "First: " << First << ", Last: " << Last <<'\n' |
| 2335 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2336 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 2337 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2338 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 2339 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2340 | APInt Range = ComputeRange(LEnd, RBegin); |
Stepan Dyatkovskiy | c2c52a6 | 2012-05-15 06:50:18 +0000 | [diff] [blame] | 2341 | assert((Range - 2ULL).isNonNegative() && |
| 2342 | "Invalid case distance"); |
Chris Lattner | c3e4e59 | 2011-04-09 06:57:13 +0000 | [diff] [blame] | 2343 | // Use volatile double here to avoid excess precision issues on some hosts, |
| 2344 | // e.g. that use 80-bit X87 registers. |
| 2345 | volatile double LDensity = |
| 2346 | (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2347 | (LEnd - First + 1ULL).roundToDouble(); |
Chris Lattner | c3e4e59 | 2011-04-09 06:57:13 +0000 | [diff] [blame] | 2348 | volatile double RDensity = |
| 2349 | (double)RSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2350 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2351 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2352 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2353 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2354 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 2355 | << "LDensity: " << LDensity |
| 2356 | << ", RDensity: " << RDensity << '\n' |
| 2357 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2358 | if (FMetric < Metric) { |
| 2359 | Pivot = J; |
| 2360 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2361 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | LSize += J->size(); |
| 2365 | RSize -= J->size(); |
| 2366 | } |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2367 | |
| 2368 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2369 | if (areJTsAllowed(*TLI)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2370 | // If our case is dense we *really* should handle it earlier! |
| 2371 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 2372 | } else { |
| 2373 | Pivot = CR.Range.first + Size/2; |
| 2374 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2375 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2376 | CaseRange LHSR(CR.Range.first, Pivot); |
| 2377 | CaseRange RHSR(Pivot, CR.Range.second); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 2378 | const Constant *C = Pivot->Low; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2379 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2380 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2381 | // 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] | 2382 | // 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] | 2383 | // 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] | 2384 | // 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] | 2385 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 2386 | // rather than creating a leaf node for it. |
| 2387 | if ((LHSR.second - LHSR.first) == 1 && |
| 2388 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2389 | cast<ConstantInt>(C)->getValue() == |
| 2390 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2391 | TrueBB = LHSR.first->BB; |
| 2392 | } else { |
| 2393 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2394 | CurMF->insert(BBI, TrueBB); |
| 2395 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2396 | |
| 2397 | // Put SV in a virtual register to make it available from the new blocks. |
| 2398 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2399 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2400 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2401 | // Similar to the optimization above, if the Value being switched on is |
| 2402 | // known to be less than the Constant CR.LT, and the current Case Value |
| 2403 | // is CR.LT - 1, then we can branch directly to the target block for |
| 2404 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 2405 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2406 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 2407 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2408 | FalseBB = RHSR.first->BB; |
| 2409 | } else { |
| 2410 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2411 | CurMF->insert(BBI, FalseBB); |
| 2412 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Put SV in a virtual register to make it available from the new blocks. |
| 2415 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2419 | // 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] | 2420 | // Otherwise, branch to LHS. |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2421 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2422 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2423 | if (CR.CaseBB == SwitchBB) |
| 2424 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2425 | else |
| 2426 | SwitchCases.push_back(CB); |
| 2427 | |
| 2428 | return true; |
| 2429 | } |
| 2430 | |
| 2431 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 2432 | /// range span less, than machine word bitwidth, encode case range into series |
| 2433 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2434 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 2435 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2436 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2437 | MachineBasicBlock* Default, |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2438 | MachineBasicBlock* SwitchBB) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2439 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2440 | EVT PTy = TLI->getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2441 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2442 | |
| 2443 | Case& FrontCase = *CR.Range.first; |
| 2444 | Case& BackCase = *(CR.Range.second-1); |
| 2445 | |
| 2446 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2447 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2448 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2449 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 2450 | // 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] | 2451 | if (!TLI->isOperationLegal(ISD::SHL, TLI->getPointerTy())) |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 2452 | return false; |
| 2453 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2454 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2455 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2456 | I!=E; ++I) { |
| 2457 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2458 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2459 | } |
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 | // Count unique destinations |
| 2462 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 2463 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2464 | Dests.insert(I->BB); |
| 2465 | if (Dests.size() > 3) |
| 2466 | // Don't bother the code below, if there are too much unique destinations |
| 2467 | return false; |
| 2468 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2469 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2470 | << Dests.size() << '\n' |
| 2471 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2472 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2473 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2474 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2475 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2476 | APInt cmpRange = maxValue - minValue; |
| 2477 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2478 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2479 | << "Low bound: " << minValue << '\n' |
| 2480 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2481 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 2482 | if (cmpRange.uge(IntPtrBits) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2483 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 2484 | !(Dests.size() == 2 && numCmps >= 5) && |
| 2485 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 2486 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2487 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2488 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2489 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 2490 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2491 | // Optimize the case where all the case values fit in a |
| 2492 | // word without having to subtract minValue. In this case, |
| 2493 | // we can optimize away the subtraction. |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2494 | if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2495 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2496 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2497 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2498 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2499 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2500 | CaseBitsVector CasesBits; |
| 2501 | unsigned i, count = 0; |
| 2502 | |
| 2503 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2504 | MachineBasicBlock* Dest = I->BB; |
| 2505 | for (i = 0; i < count; ++i) |
| 2506 | if (Dest == CasesBits[i].BB) |
| 2507 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2508 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2509 | if (i == count) { |
| 2510 | assert((count < 3) && "Too much destinations to test!"); |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2511 | CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2512 | count++; |
| 2513 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2514 | |
| 2515 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 2516 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 2517 | |
| 2518 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 2519 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2520 | CasesBits[i].ExtraWeight += I->ExtraWeight; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2521 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2522 | for (uint64_t j = lo; j <= hi; j++) { |
| 2523 | CasesBits[i].Mask |= 1ULL << j; |
| 2524 | CasesBits[i].Bits++; |
| 2525 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2526 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2527 | } |
| 2528 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2529 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2530 | BitTestInfo BTC; |
| 2531 | |
| 2532 | // Figure out which block is immediately after the current one. |
| 2533 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2534 | ++BBI; |
| 2535 | |
| 2536 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2537 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2538 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2539 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2540 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2541 | << ", Bits: " << CasesBits[i].Bits |
| 2542 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2543 | |
| 2544 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2545 | CurMF->insert(BBI, CaseBB); |
| 2546 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2547 | CaseBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2548 | CasesBits[i].BB, CasesBits[i].ExtraWeight)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2549 | |
| 2550 | // Put SV in a virtual register to make it available from the new blocks. |
| 2551 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2552 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2553 | |
| 2554 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 2555 | -1U, MVT::Other, (CR.CaseBB == SwitchBB), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2556 | CR.CaseBB, Default, BTC); |
| 2557 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2558 | if (CR.CaseBB == SwitchBB) |
| 2559 | visitBitTestHeader(BTB, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2560 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2561 | BitTestCases.push_back(BTB); |
| 2562 | |
| 2563 | return true; |
| 2564 | } |
| 2565 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2566 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2567 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 2568 | const SwitchInst& SI) { |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2569 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2570 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2571 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2572 | // Start with "simple" cases |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 2573 | for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 2574 | i != e; ++i) { |
| 2575 | const BasicBlock *SuccBB = i.getCaseSuccessor(); |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2576 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB]; |
| 2577 | |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2578 | uint32_t ExtraWeight = |
| 2579 | BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0; |
| 2580 | |
| 2581 | Cases.push_back(Case(i.getCaseValue(), i.getCaseValue(), |
| 2582 | SMBB, ExtraWeight)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2583 | } |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2584 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2585 | |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2586 | // Merge case into clusters |
| 2587 | if (Cases.size() >= 2) |
| 2588 | // Must recompute end() each iteration because it may be |
| 2589 | // invalidated by erase if we hold on to it |
| 2590 | for (CaseItr I = Cases.begin(), J = llvm::next(Cases.begin()); |
| 2591 | J != Cases.end(); ) { |
| 2592 | const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue(); |
| 2593 | const APInt& currentValue = cast<ConstantInt>(I->High)->getValue(); |
| 2594 | MachineBasicBlock* nextBB = J->BB; |
| 2595 | MachineBasicBlock* currentBB = I->BB; |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2596 | |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2597 | // If the two neighboring cases go to the same destination, merge them |
| 2598 | // into a single case. |
| 2599 | if ((nextValue - currentValue == 1) && (currentBB == nextBB)) { |
| 2600 | I->High = J->High; |
| 2601 | I->ExtraWeight += J->ExtraWeight; |
| 2602 | J = Cases.erase(J); |
| 2603 | } else { |
| 2604 | I = J++; |
| 2605 | } |
| 2606 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2607 | |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame^] | 2608 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 2609 | if (I->Low != I->High) |
| 2610 | // A range counts double, since it requires two compares. |
| 2611 | ++numCmps; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | return numCmps; |
| 2615 | } |
| 2616 | |
Jakob Stoklund Olesen | 2622f46 | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 2617 | void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First, |
| 2618 | MachineBasicBlock *Last) { |
| 2619 | // Update JTCases. |
| 2620 | for (unsigned i = 0, e = JTCases.size(); i != e; ++i) |
| 2621 | if (JTCases[i].first.HeaderBB == First) |
| 2622 | JTCases[i].first.HeaderBB = Last; |
| 2623 | |
| 2624 | // Update BitTestCases. |
| 2625 | for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i) |
| 2626 | if (BitTestCases[i].Parent == First) |
| 2627 | BitTestCases[i].Parent = Last; |
| 2628 | } |
| 2629 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2630 | void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 2631 | MachineBasicBlock *SwitchMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2632 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2633 | // Figure out which block is immediately after the current one. |
| 2634 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2635 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2636 | |
| 2637 | // If there is only the default destination, branch to it if it is not the |
| 2638 | // next basic block. Otherwise, just fall through. |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 2639 | if (!SI.getNumCases()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2640 | // Update machine-CFG edges. |
| 2641 | |
| 2642 | // If this is not a fall-through branch, emit the branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2643 | SwitchMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2644 | if (Default != NextBlock) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2645 | DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2646 | MVT::Other, getControlRoot(), |
| 2647 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2648 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2649 | return; |
| 2650 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2651 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2652 | // If there are any non-default case statements, create a vector of Cases |
| 2653 | // representing each one, and sort the vector so that we can efficiently |
| 2654 | // create a binary search tree from them. |
| 2655 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2656 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2657 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2658 | << ". Total compares: " << numCmps << '\n'); |
Duncan Sands | 17001ce | 2011-10-18 12:44:00 +0000 | [diff] [blame] | 2659 | (void)numCmps; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2660 | |
| 2661 | // Get the Value to be switched on and default basic blocks, which will be |
| 2662 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2663 | // search tree. |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 2664 | const Value *SV = SI.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2665 | |
| 2666 | // Push the initial CaseRec onto the worklist |
| 2667 | CaseRecVector WorkList; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2668 | WorkList.push_back(CaseRec(SwitchMBB,0,0, |
| 2669 | CaseRange(Cases.begin(),Cases.end()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2670 | |
| 2671 | while (!WorkList.empty()) { |
| 2672 | // Grab a record representing a case range to process off the worklist |
| 2673 | CaseRec CR = WorkList.back(); |
| 2674 | WorkList.pop_back(); |
| 2675 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2676 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2677 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2678 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2679 | // If the range has few cases (two or less) emit a series of specific |
| 2680 | // tests. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2681 | if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2682 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2683 | |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2684 | // 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] | 2685 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2686 | // lowering the switch to a binary tree of conditional branches. |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2687 | // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries(). |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2688 | if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2689 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2690 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2691 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2692 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2693 | handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2697 | void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 2698 | MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2699 | |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2700 | // Update machine-CFG edges with unique successors. |
Nadav Rotem | ee0ce15 | 2012-10-23 21:05:33 +0000 | [diff] [blame] | 2701 | SmallSet<BasicBlock*, 32> Done; |
| 2702 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) { |
| 2703 | BasicBlock *BB = I.getSuccessor(i); |
| 2704 | bool Inserted = Done.insert(BB); |
| 2705 | if (!Inserted) |
| 2706 | continue; |
| 2707 | |
| 2708 | MachineBasicBlock *Succ = FuncInfo.MBBMap[BB]; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 2709 | addSuccessorWithWeight(IndirectBrMBB, Succ); |
| 2710 | } |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2711 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2712 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2713 | MVT::Other, getControlRoot(), |
| 2714 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2715 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2716 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2717 | void SelectionDAGBuilder::visitFSub(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2718 | // -0.0 - X --> fneg |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2719 | Type *Ty = I.getType(); |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2720 | if (isa<Constant>(I.getOperand(0)) && |
| 2721 | I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) { |
| 2722 | SDValue Op2 = getValue(I.getOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2723 | setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(), |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2724 | Op2.getValueType(), Op2)); |
| 2725 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2726 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2727 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2728 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2731 | void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2732 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2733 | SDValue Op2 = getValue(I.getOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2734 | setValue(&I, DAG.getNode(OpCode, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2735 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2738 | void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2739 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2740 | SDValue Op2 = getValue(I.getOperand(1)); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2741 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2742 | EVT ShiftTy = TM.getTargetLowering()->getShiftAmountTy(Op2.getValueType()); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2743 | |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2744 | // Coerce the shift amount to the right type if we can. |
| 2745 | if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) { |
Chris Lattner | 915eeb4 | 2011-02-13 09:10:56 +0000 | [diff] [blame] | 2746 | unsigned ShiftSize = ShiftTy.getSizeInBits(); |
| 2747 | unsigned Op2Size = Op2.getValueType().getSizeInBits(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2748 | SDLoc DL = getCurSDLoc(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2749 | |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2750 | // If the operand is smaller than the shift count type, promote it. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2751 | if (ShiftSize > Op2Size) |
| 2752 | Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2753 | |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2754 | // If the operand is larger than the shift count type but the shift |
| 2755 | // count type has enough bits to represent any shift value, truncate |
| 2756 | // it now. This is a common case and it exposes the truncate to |
| 2757 | // optimization early. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2758 | else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2759 | Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2); |
| 2760 | // Otherwise we'll need to temporarily settle for some other convenient |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 2761 | // type. Type legalization will make adjustments once the shiftee is split. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2762 | else |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 2763 | Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2764 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2765 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2766 | setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2767 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2770 | void SelectionDAGBuilder::visitSDiv(const User &I) { |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2771 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2772 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2773 | |
| 2774 | // Turn exact SDivs into multiplications. |
| 2775 | // FIXME: This should be in DAGCombiner, but it doesn't have access to the |
| 2776 | // exact bit. |
Benjamin Kramer | 3492a4a | 2011-07-08 12:08:24 +0000 | [diff] [blame] | 2777 | if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() && |
| 2778 | !isa<ConstantSDNode>(Op1) && |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2779 | isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue()) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2780 | setValue(&I, TM.getTargetLowering()->BuildExactSDIV(Op1, Op2, |
| 2781 | getCurSDLoc(), DAG)); |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2782 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2783 | setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2784 | Op1, Op2)); |
| 2785 | } |
| 2786 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2787 | void SelectionDAGBuilder::visitICmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2788 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2789 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2790 | predicate = IC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2791 | else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2792 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2793 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2794 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2795 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2796 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2797 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2798 | setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2801 | void SelectionDAGBuilder::visitFCmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2802 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2803 | if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2804 | predicate = FC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2805 | else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2806 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2807 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2808 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2809 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2810 | if (TM.Options.NoNaNsFPMath) |
| 2811 | Condition = getFCmpCodeWithoutNaN(Condition); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2812 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2813 | setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2816 | void SelectionDAGBuilder::visitSelect(const User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2817 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2818 | ComputeValueVTs(*TM.getTargetLowering(), I.getType(), ValueVTs); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2819 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2820 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2821 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2822 | SmallVector<SDValue, 4> Values(NumValues); |
| 2823 | SDValue Cond = getValue(I.getOperand(0)); |
| 2824 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2825 | SDValue FalseVal = getValue(I.getOperand(2)); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 2826 | ISD::NodeType OpCode = Cond.getValueType().isVector() ? |
| 2827 | ISD::VSELECT : ISD::SELECT; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2828 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2829 | for (unsigned i = 0; i != NumValues; ++i) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2830 | Values[i] = DAG.getNode(OpCode, getCurSDLoc(), |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 2831 | TrueVal.getNode()->getValueType(TrueVal.getResNo()+i), |
Chris Lattner | b3e87b2 | 2010-03-12 07:15:36 +0000 | [diff] [blame] | 2832 | Cond, |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2833 | SDValue(TrueVal.getNode(), |
| 2834 | TrueVal.getResNo() + i), |
| 2835 | SDValue(FalseVal.getNode(), |
| 2836 | FalseVal.getResNo() + i)); |
| 2837 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2838 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2839 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2840 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2841 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2842 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2843 | void SelectionDAGBuilder::visitTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2844 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 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.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N)); |
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::visitZExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2851 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2852 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2853 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2854 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2855 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2858 | void SelectionDAGBuilder::visitSExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2859 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2860 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2861 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2862 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2863 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2866 | void SelectionDAGBuilder::visitFPTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2867 | // FPTrunc is never a no-op cast, no need to check |
| 2868 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2869 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2870 | EVT DestVT = TLI->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2871 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(), |
Pete Cooper | f57e1c2 | 2012-01-17 01:54:07 +0000 | [diff] [blame] | 2872 | DestVT, N, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2873 | DAG.getTargetConstant(0, TLI->getPointerTy()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2876 | void SelectionDAGBuilder::visitFPExt(const User &I) { |
Hal Finkel | 46bb70c | 2011-10-18 03:51:57 +0000 | [diff] [blame] | 2877 | // FPExt is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2878 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2879 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2880 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2883 | void SelectionDAGBuilder::visitFPToUI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2884 | // FPToUI is never a no-op cast, no need to check |
| 2885 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2886 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2887 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2890 | void SelectionDAGBuilder::visitFPToSI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2891 | // FPToSI is never a no-op cast, no need to check |
| 2892 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2893 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2894 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2897 | void SelectionDAGBuilder::visitUIToFP(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2898 | // UIToFP is never a no-op cast, no need to check |
| 2899 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2900 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2901 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 2904 | void SelectionDAGBuilder::visitSIToFP(const User &I) { |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2905 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2906 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2907 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2908 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2911 | void SelectionDAGBuilder::visitPtrToInt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2912 | // What to do depends on the size of the integer and the size of the pointer. |
| 2913 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2914 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2915 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2916 | setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2919 | void SelectionDAGBuilder::visitIntToPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2920 | // What to do depends on the size of the integer and the size of the pointer. |
| 2921 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2922 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2923 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2924 | setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2927 | void SelectionDAGBuilder::visitBitCast(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2928 | SDValue N = getValue(I.getOperand(0)); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2929 | EVT DestVT = TM.getTargetLowering()->getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2930 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2931 | // 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] | 2932 | // either a BITCAST or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2933 | if (DestVT != N.getValueType()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2934 | setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2935 | DestVT, N)); // convert types. |
| 2936 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2937 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2940 | void SelectionDAGBuilder::visitInsertElement(const User &I) { |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 2941 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2942 | SDValue InVec = getValue(I.getOperand(0)); |
| 2943 | SDValue InVal = getValue(I.getOperand(1)); |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 2944 | SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)), |
| 2945 | getCurSDLoc(), TLI.getVectorIdxTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2946 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2947 | TM.getTargetLowering()->getValueType(I.getType()), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2948 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2951 | void SelectionDAGBuilder::visitExtractElement(const User &I) { |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 2952 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2953 | SDValue InVec = getValue(I.getOperand(0)); |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 2954 | SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), |
| 2955 | getCurSDLoc(), TLI.getVectorIdxTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2956 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2957 | TM.getTargetLowering()->getValueType(I.getType()), |
| 2958 | InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2961 | // Utility for visitShuffleVector - Return true if every element in Mask, |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 2962 | // beginning from position Pos and ending in Pos+Size, falls within the |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2963 | // specified sequential range [L, L+Pos). or is undef. |
| 2964 | static bool isSequentialInRange(const SmallVectorImpl<int> &Mask, |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 2965 | unsigned Pos, unsigned Size, int Low) { |
| 2966 | for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low) |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2967 | if (Mask[i] >= 0 && Mask[i] != Low) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2968 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2969 | return true; |
| 2970 | } |
| 2971 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2972 | void SelectionDAGBuilder::visitShuffleVector(const User &I) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2973 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2974 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2975 | |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 2976 | SmallVector<int, 8> Mask; |
| 2977 | ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask); |
| 2978 | unsigned MaskNumElts = Mask.size(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2979 | |
| 2980 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 2981 | EVT VT = TLI->getValueType(I.getType()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2982 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2983 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2984 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2985 | if (SrcNumElts == MaskNumElts) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2986 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2987 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2988 | return; |
| 2989 | } |
| 2990 | |
| 2991 | // 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] | 2992 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2993 | // Mask is longer than the source vectors and is a multiple of the source |
| 2994 | // 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] | 2995 | // lengths match. |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2996 | if (SrcNumElts*2 == MaskNumElts) { |
| 2997 | // First check for Src1 in low and Src2 in high |
| 2998 | if (isSequentialInRange(Mask, 0, SrcNumElts, 0) && |
| 2999 | isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) { |
| 3000 | // The shuffle is concatenating two vectors together. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3001 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(), |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 3002 | VT, Src1, Src2)); |
| 3003 | return; |
| 3004 | } |
| 3005 | // Then check for Src2 in low and Src1 in high |
| 3006 | if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) && |
| 3007 | isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) { |
| 3008 | // The shuffle is concatenating two vectors together. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3009 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(), |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 3010 | VT, Src2, Src1)); |
| 3011 | return; |
| 3012 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3015 | // Pad both vectors with undefs to make them the same length as the mask. |
| 3016 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3017 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 3018 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3019 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3020 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3021 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 3022 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 3023 | MOps1[0] = Src1; |
| 3024 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3025 | |
| 3026 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3027 | getCurSDLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3028 | &MOps1[0], NumConcat); |
| 3029 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3030 | getCurSDLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3031 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 3032 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3033 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3034 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3035 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3036 | int Idx = Mask[i]; |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3037 | if (Idx >= (int)SrcNumElts) |
| 3038 | Idx -= SrcNumElts - MaskNumElts; |
| 3039 | MappedOps.push_back(Idx); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3040 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3041 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3042 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3043 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3044 | return; |
| 3045 | } |
| 3046 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3047 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3048 | // Analyze the access pattern of the vector to see if we can extract |
| 3049 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 3050 | // the range of elements the mask access on both vectors. |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3051 | int MinRange[2] = { static_cast<int>(SrcNumElts), |
| 3052 | static_cast<int>(SrcNumElts)}; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3053 | int MaxRange[2] = {-1, -1}; |
| 3054 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3055 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3056 | int Idx = Mask[i]; |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3057 | unsigned Input = 0; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3058 | if (Idx < 0) |
| 3059 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3060 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3061 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3062 | Input = 1; |
| 3063 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3064 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3065 | if (Idx > MaxRange[Input]) |
| 3066 | MaxRange[Input] = Idx; |
| 3067 | if (Idx < MinRange[Input]) |
| 3068 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3069 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3070 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3071 | // Check if the access is smaller than the vector size and can we find |
| 3072 | // a reasonable extract index. |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3073 | int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not |
| 3074 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3075 | int StartIdx[2]; // StartIdx to extract from |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3076 | for (unsigned Input = 0; Input < 2; ++Input) { |
| 3077 | if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3078 | RangeUse[Input] = 0; // Unused |
| 3079 | StartIdx[Input] = 0; |
Craig Topper | f873dde | 2012-04-08 17:53:33 +0000 | [diff] [blame] | 3080 | continue; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 3081 | } |
Craig Topper | f873dde | 2012-04-08 17:53:33 +0000 | [diff] [blame] | 3082 | |
| 3083 | // Find a good start index that is a multiple of the mask length. Then |
| 3084 | // see if the rest of the elements are in range. |
| 3085 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
| 3086 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
| 3087 | StartIdx[Input] + MaskNumElts <= SrcNumElts) |
| 3088 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 3091 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3092 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3093 | return; |
| 3094 | } |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3095 | if (RangeUse[0] >= 0 && RangeUse[1] >= 0) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3096 | // Extract appropriate subvector and generate a vector shuffle |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3097 | for (unsigned Input = 0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 3098 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3099 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3100 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3101 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3102 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 3103 | Src, DAG.getConstant(StartIdx[Input], |
| 3104 | TLI->getVectorIdxTy())); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3105 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3106 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3107 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3108 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3109 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3110 | int Idx = Mask[i]; |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3111 | if (Idx >= 0) { |
| 3112 | if (Idx < (int)SrcNumElts) |
| 3113 | Idx -= StartIdx[0]; |
| 3114 | else |
| 3115 | Idx -= SrcNumElts + StartIdx[1] - MaskNumElts; |
| 3116 | } |
| 3117 | MappedOps.push_back(Idx); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3118 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3119 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3120 | setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3121 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3122 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3126 | // We can't use either concat vectors or extract subvectors so fall back to |
| 3127 | // replacing the shuffle with extract and build vector. |
| 3128 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3129 | EVT EltVT = VT.getVectorElementType(); |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 3130 | EVT IdxVT = TLI->getVectorIdxTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3131 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3132 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3133 | int Idx = Mask[i]; |
| 3134 | SDValue Res; |
| 3135 | |
| 3136 | if (Idx < 0) { |
| 3137 | Res = DAG.getUNDEF(EltVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3138 | } else { |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3139 | SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2; |
| 3140 | if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3141 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3142 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(), |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 3143 | EltVT, Src, DAG.getConstant(Idx, IdxVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3144 | } |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3145 | |
| 3146 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3147 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3148 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3149 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3150 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3153 | void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3154 | const Value *Op0 = I.getOperand(0); |
| 3155 | const Value *Op1 = I.getOperand(1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3156 | Type *AggTy = I.getType(); |
| 3157 | Type *ValTy = Op1->getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3158 | bool IntoUndef = isa<UndefValue>(Op0); |
| 3159 | bool FromUndef = isa<UndefValue>(Op1); |
| 3160 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3161 | unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3162 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3163 | const TargetLowering *TLI = TM.getTargetLowering(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3164 | SmallVector<EVT, 4> AggValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3165 | ComputeValueVTs(*TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3166 | SmallVector<EVT, 4> ValValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3167 | ComputeValueVTs(*TLI, ValTy, ValValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3168 | |
| 3169 | unsigned NumAggValues = AggValueVTs.size(); |
| 3170 | unsigned NumValValues = ValValueVTs.size(); |
| 3171 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 3172 | |
| 3173 | SDValue Agg = getValue(Op0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3174 | unsigned i = 0; |
| 3175 | // Copy the beginning value(s) from the original aggregate. |
| 3176 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3177 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3178 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 3179 | // Copy values from the inserted value(s). |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 3180 | if (NumValValues) { |
| 3181 | SDValue Val = getValue(Op1); |
| 3182 | for (; i != LinearIndex + NumValValues; ++i) |
| 3183 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
| 3184 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 3185 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3186 | // Copy remaining value(s) from the original aggregate. |
| 3187 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3188 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3189 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 3190 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3191 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3192 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 3193 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3196 | void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3197 | const Value *Op0 = I.getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3198 | Type *AggTy = Op0->getType(); |
| 3199 | Type *ValTy = I.getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3200 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 3201 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3202 | unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3203 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3204 | const TargetLowering *TLI = TM.getTargetLowering(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3205 | SmallVector<EVT, 4> ValValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3206 | ComputeValueVTs(*TLI, ValTy, ValValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3207 | |
| 3208 | unsigned NumValValues = ValValueVTs.size(); |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 3209 | |
| 3210 | // Ignore a extractvalue that produces an empty object |
| 3211 | if (!NumValValues) { |
| 3212 | setValue(&I, DAG.getUNDEF(MVT(MVT::Other))); |
| 3213 | return; |
| 3214 | } |
| 3215 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3216 | SmallVector<SDValue, 4> Values(NumValValues); |
| 3217 | |
| 3218 | SDValue Agg = getValue(Op0); |
| 3219 | // Copy out the selected value(s). |
| 3220 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 3221 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 3222 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3223 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 3224 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3225 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3226 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3227 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 3228 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3231 | void SelectionDAGBuilder::visitGetElementPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3232 | SDValue N = getValue(I.getOperand(0)); |
Nadav Rotem | 1c23920 | 2012-02-28 14:13:19 +0000 | [diff] [blame] | 3233 | // Note that the pointer operand may be a vector of pointers. Take the scalar |
| 3234 | // element which holds a pointer. |
| 3235 | Type *Ty = I.getOperand(0)->getType()->getScalarType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3236 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3237 | 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] | 3238 | OI != E; ++OI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3239 | const Value *Idx = *OI; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3240 | if (StructType *StTy = dyn_cast<StructType>(Ty)) { |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3241 | unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3242 | if (Field) { |
| 3243 | // N = N + Offset |
| 3244 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3245 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3246 | DAG.getConstant(Offset, N.getValueType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3247 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3248 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3249 | Ty = StTy->getElementType(Field); |
| 3250 | } else { |
Tom Stellard | da25cd3 | 2013-08-26 15:05:36 +0000 | [diff] [blame] | 3251 | uint32_t AS = 0; |
| 3252 | if (PointerType *PtrType = dyn_cast<PointerType>(Ty)) { |
| 3253 | AS = PtrType->getAddressSpace(); |
| 3254 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3255 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 3256 | |
| 3257 | // If this is a constant subscript, handle it quickly. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3258 | const TargetLowering *TLI = TM.getTargetLowering(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3259 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 3260 | if (CI->isZero()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3261 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 3262 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 3263 | SDValue OffsVal; |
Tom Stellard | da25cd3 | 2013-08-26 15:05:36 +0000 | [diff] [blame] | 3264 | EVT PTy = TLI->getPointerTy(AS); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 3265 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3266 | if (PtrBits < 64) |
Tom Stellard | da25cd3 | 2013-08-26 15:05:36 +0000 | [diff] [blame] | 3267 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), PTy, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3268 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3269 | else |
Tom Stellard | da25cd3 | 2013-08-26 15:05:36 +0000 | [diff] [blame] | 3270 | OffsVal = DAG.getConstant(Offs, PTy); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3271 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3272 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 3273 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3274 | continue; |
| 3275 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3276 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3277 | // N = N + Idx * ElementSize; |
Tom Stellard | da25cd3 | 2013-08-26 15:05:36 +0000 | [diff] [blame] | 3278 | APInt ElementSize = APInt(TLI->getPointerSizeInBits(AS), |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 3279 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3280 | SDValue IdxN = getValue(Idx); |
| 3281 | |
| 3282 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 3283 | // it. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3284 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3285 | |
| 3286 | // If this is a multiply by a power of two, turn it into a shl |
| 3287 | // immediately. This is a very common case. |
| 3288 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 3289 | if (ElementSize.isPowerOf2()) { |
| 3290 | unsigned Amt = ElementSize.logBase2(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3291 | IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3292 | N.getValueType(), IdxN, |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3293 | DAG.getConstant(Amt, IdxN.getValueType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3294 | } else { |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3295 | SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3296 | IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3297 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3298 | } |
| 3299 | } |
| 3300 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3301 | N = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3302 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3303 | } |
| 3304 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3305 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3306 | setValue(&I, N); |
| 3307 | } |
| 3308 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3309 | void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3310 | // If this is a fixed sized alloca in the entry block of the function, |
| 3311 | // allocate it statically on the stack. |
| 3312 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 3313 | return; // getValue will auto-populate this. |
| 3314 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3315 | Type *Ty = I.getAllocatedType(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3316 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3317 | uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3318 | unsigned Align = |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3319 | std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3320 | I.getAlignment()); |
| 3321 | |
| 3322 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3323 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3324 | EVT IntPtr = TLI->getPointerTy(); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3325 | if (AllocSize.getValueType() != IntPtr) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3326 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3327 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3328 | AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr, |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3329 | AllocSize, |
| 3330 | DAG.getConstant(TySize, IntPtr)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3331 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3332 | // Handle alignment. If the requested alignment is less than or equal to |
| 3333 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 3334 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 3335 | unsigned StackAlign = TM.getFrameLowering()->getStackAlignment(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3336 | if (Align <= StackAlign) |
| 3337 | Align = 0; |
| 3338 | |
| 3339 | // Round the size of the allocation up to the stack alignment size |
| 3340 | // by add SA-1 to the size. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3341 | AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3342 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3343 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3344 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3345 | // Mask out the low bits for alignment purposes. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3346 | AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3347 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3348 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 3349 | |
| 3350 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3351 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3352 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3353 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3354 | setValue(&I, DSA); |
| 3355 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3356 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3357 | // Inform the Frame Information that we have just allocated a variable-sized |
| 3358 | // object. |
Bob Wilson | 8f637ad | 2013-02-08 20:35:15 +0000 | [diff] [blame] | 3359 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3362 | void SelectionDAGBuilder::visitLoad(const LoadInst &I) { |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3363 | if (I.isAtomic()) |
| 3364 | return visitAtomicLoad(I); |
| 3365 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3366 | const Value *SV = I.getOperand(0); |
| 3367 | SDValue Ptr = getValue(SV); |
| 3368 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3369 | Type *Ty = I.getType(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3370 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3371 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3372 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 3373 | bool isInvariant = I.getMetadata("invariant.load") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3374 | unsigned Alignment = I.getAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3375 | const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa); |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 3376 | const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3377 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3378 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3379 | SmallVector<uint64_t, 4> Offsets; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3380 | ComputeValueVTs(*TM.getTargetLowering(), Ty, ValueVTs, &Offsets); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3381 | unsigned NumValues = ValueVTs.size(); |
| 3382 | if (NumValues == 0) |
| 3383 | return; |
| 3384 | |
| 3385 | SDValue Root; |
| 3386 | bool ConstantMemory = false; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3387 | if (I.isVolatile() || NumValues > MaxParallelChains) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3388 | // Serialize volatile loads with other side effects. |
| 3389 | Root = getRoot(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3390 | else if (AA->pointsToConstantMemory( |
| 3391 | AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3392 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 3393 | Root = DAG.getEntryNode(); |
| 3394 | ConstantMemory = true; |
| 3395 | } else { |
| 3396 | // Do not serialize non-volatile loads against each other. |
| 3397 | Root = DAG.getRoot(); |
| 3398 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3399 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3400 | SmallVector<SDValue, 4> Values(NumValues); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3401 | SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains), |
| 3402 | NumValues)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3403 | EVT PtrVT = Ptr.getValueType(); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3404 | unsigned ChainI = 0; |
| 3405 | for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { |
| 3406 | // Serializing loads here may result in excessive register pressure, and |
| 3407 | // TokenFactor places arbitrary choke points on the scheduler. SD scheduling |
| 3408 | // could recover a bit by hoisting nodes upward in the chain by recognizing |
| 3409 | // they are side-effect free or do not alias. The optimizer should really |
| 3410 | // avoid this case by converting large object/array copies to llvm.memcpy |
| 3411 | // (MaxParallelChains should always remain as failsafe). |
| 3412 | if (ChainI == MaxParallelChains) { |
| 3413 | assert(PendingLoads.empty() && "PendingLoads must be serialized first"); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3414 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3415 | MVT::Other, &Chains[0], ChainI); |
| 3416 | Root = Chain; |
| 3417 | ChainI = 0; |
| 3418 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3419 | SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(), |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3420 | PtrVT, Ptr, |
| 3421 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3422 | SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root, |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 3423 | A, MachinePointerInfo(SV, Offsets[i]), isVolatile, |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 3424 | isNonTemporal, isInvariant, Alignment, TBAAInfo, |
| 3425 | Ranges); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3426 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3427 | Values[i] = L; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3428 | Chains[ChainI] = L.getValue(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3429 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3430 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3431 | if (!ConstantMemory) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3432 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3433 | MVT::Other, &Chains[0], ChainI); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3434 | if (isVolatile) |
| 3435 | DAG.setRoot(Chain); |
| 3436 | else |
| 3437 | PendingLoads.push_back(Chain); |
| 3438 | } |
| 3439 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3440 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3441 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 3442 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3443 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3444 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3445 | void SelectionDAGBuilder::visitStore(const StoreInst &I) { |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3446 | if (I.isAtomic()) |
| 3447 | return visitAtomicStore(I); |
| 3448 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3449 | const Value *SrcV = I.getOperand(0); |
| 3450 | const Value *PtrV = I.getOperand(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3451 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3452 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3453 | SmallVector<uint64_t, 4> Offsets; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3454 | ComputeValueVTs(*TM.getTargetLowering(), SrcV->getType(), ValueVTs, &Offsets); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3455 | unsigned NumValues = ValueVTs.size(); |
| 3456 | if (NumValues == 0) |
| 3457 | return; |
| 3458 | |
| 3459 | // Get the lowered operands. Note that we do this after |
| 3460 | // checking if NumResults is zero, because with zero results |
| 3461 | // the operands won't have values in the map. |
| 3462 | SDValue Src = getValue(SrcV); |
| 3463 | SDValue Ptr = getValue(PtrV); |
| 3464 | |
| 3465 | SDValue Root = getRoot(); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3466 | SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains), |
| 3467 | NumValues)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3468 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3469 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3470 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3471 | unsigned Alignment = I.getAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3472 | const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3473 | |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3474 | unsigned ChainI = 0; |
| 3475 | for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { |
| 3476 | // See visitLoad comments. |
| 3477 | if (ChainI == MaxParallelChains) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3478 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3479 | MVT::Other, &Chains[0], ChainI); |
| 3480 | Root = Chain; |
| 3481 | ChainI = 0; |
| 3482 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3483 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr, |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3484 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3485 | SDValue St = DAG.getStore(Root, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3486 | SDValue(Src.getNode(), Src.getResNo() + i), |
| 3487 | Add, MachinePointerInfo(PtrV, Offsets[i]), |
| 3488 | isVolatile, isNonTemporal, Alignment, TBAAInfo); |
| 3489 | Chains[ChainI] = St; |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3492 | SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3493 | MVT::Other, &Chains[0], ChainI); |
Devang Patel | 7e13efa | 2010-10-26 22:14:52 +0000 | [diff] [blame] | 3494 | DAG.setRoot(StoreNode); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3497 | static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3498 | SynchronizationScope Scope, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3499 | bool Before, SDLoc dl, |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3500 | SelectionDAG &DAG, |
| 3501 | const TargetLowering &TLI) { |
| 3502 | // Fence, if necessary |
| 3503 | if (Before) { |
Eli Friedman | 069e2ed | 2011-08-26 02:59:24 +0000 | [diff] [blame] | 3504 | if (Order == AcquireRelease || Order == SequentiallyConsistent) |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3505 | Order = Release; |
| 3506 | else if (Order == Acquire || Order == Monotonic) |
| 3507 | return Chain; |
| 3508 | } else { |
| 3509 | if (Order == AcquireRelease) |
| 3510 | Order = Acquire; |
| 3511 | else if (Order == Release || Order == Monotonic) |
| 3512 | return Chain; |
| 3513 | } |
| 3514 | SDValue Ops[3]; |
| 3515 | Ops[0] = Chain; |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3516 | Ops[1] = DAG.getConstant(Order, TLI.getPointerTy()); |
| 3517 | Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy()); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3518 | return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3); |
| 3519 | } |
| 3520 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3521 | void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3522 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3523 | AtomicOrdering Order = I.getOrdering(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3524 | SynchronizationScope Scope = I.getSynchScope(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3525 | |
| 3526 | SDValue InChain = getRoot(); |
| 3527 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3528 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3529 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3530 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3531 | DAG, *TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3532 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3533 | SDValue L = |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3534 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 3535 | getValue(I.getCompareOperand()).getSimpleValueType(), |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3536 | InChain, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3537 | getValue(I.getPointerOperand()), |
| 3538 | getValue(I.getCompareOperand()), |
| 3539 | getValue(I.getNewValOperand()), |
| 3540 | MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3541 | TLI->getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3542 | Scope); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3543 | |
| 3544 | SDValue OutChain = L.getValue(1); |
| 3545 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3546 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3547 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3548 | DAG, *TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3549 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3550 | setValue(&I, L); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3551 | DAG.setRoot(OutChain); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3555 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3556 | ISD::NodeType NT; |
| 3557 | switch (I.getOperation()) { |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 3558 | default: llvm_unreachable("Unknown atomicrmw operation"); |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3559 | case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break; |
| 3560 | case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break; |
| 3561 | case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break; |
| 3562 | case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break; |
| 3563 | case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break; |
| 3564 | case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break; |
| 3565 | case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break; |
| 3566 | case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break; |
| 3567 | case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break; |
| 3568 | case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break; |
| 3569 | case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break; |
| 3570 | } |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3571 | AtomicOrdering Order = I.getOrdering(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3572 | SynchronizationScope Scope = I.getSynchScope(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3573 | |
| 3574 | SDValue InChain = getRoot(); |
| 3575 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3576 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3577 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3578 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3579 | DAG, *TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3580 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3581 | SDValue L = |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3582 | DAG.getAtomic(NT, dl, |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 3583 | getValue(I.getValOperand()).getSimpleValueType(), |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3584 | InChain, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3585 | getValue(I.getPointerOperand()), |
| 3586 | getValue(I.getValOperand()), |
| 3587 | I.getPointerOperand(), 0 /* Alignment */, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3588 | TLI->getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3589 | Scope); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3590 | |
| 3591 | SDValue OutChain = L.getValue(1); |
| 3592 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3593 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3594 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3595 | DAG, *TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3596 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3597 | setValue(&I, L); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3598 | DAG.setRoot(OutChain); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3601 | void SelectionDAGBuilder::visitFence(const FenceInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3602 | SDLoc dl = getCurSDLoc(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3603 | const TargetLowering *TLI = TM.getTargetLowering(); |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 3604 | SDValue Ops[3]; |
| 3605 | Ops[0] = getRoot(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3606 | Ops[1] = DAG.getConstant(I.getOrdering(), TLI->getPointerTy()); |
| 3607 | Ops[2] = DAG.getConstant(I.getSynchScope(), TLI->getPointerTy()); |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 3608 | DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3)); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3611 | void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3612 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3613 | AtomicOrdering Order = I.getOrdering(); |
| 3614 | SynchronizationScope Scope = I.getSynchScope(); |
| 3615 | |
| 3616 | SDValue InChain = getRoot(); |
| 3617 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3618 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3619 | EVT VT = TLI->getValueType(I.getType()); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3620 | |
Evan Cheng | 607acd6 | 2013-02-06 02:06:33 +0000 | [diff] [blame] | 3621 | if (I.getAlignment() < VT.getSizeInBits() / 8) |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3622 | report_fatal_error("Cannot generate unaligned atomic load"); |
| 3623 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3624 | SDValue L = |
| 3625 | DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain, |
| 3626 | getValue(I.getPointerOperand()), |
| 3627 | I.getPointerOperand(), I.getAlignment(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3628 | TLI->getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3629 | Scope); |
| 3630 | |
| 3631 | SDValue OutChain = L.getValue(1); |
| 3632 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3633 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3634 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3635 | DAG, *TLI); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3636 | |
| 3637 | setValue(&I, L); |
| 3638 | DAG.setRoot(OutChain); |
| 3639 | } |
| 3640 | |
| 3641 | void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3642 | SDLoc dl = getCurSDLoc(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3643 | |
| 3644 | AtomicOrdering Order = I.getOrdering(); |
| 3645 | SynchronizationScope Scope = I.getSynchScope(); |
| 3646 | |
| 3647 | SDValue InChain = getRoot(); |
| 3648 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3649 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3650 | EVT VT = TLI->getValueType(I.getValueOperand()->getType()); |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3651 | |
Evan Cheng | 607acd6 | 2013-02-06 02:06:33 +0000 | [diff] [blame] | 3652 | if (I.getAlignment() < VT.getSizeInBits() / 8) |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3653 | report_fatal_error("Cannot generate unaligned atomic store"); |
| 3654 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3655 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3656 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3657 | DAG, *TLI); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3658 | |
| 3659 | SDValue OutChain = |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3660 | DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3661 | InChain, |
| 3662 | getValue(I.getPointerOperand()), |
| 3663 | getValue(I.getValueOperand()), |
| 3664 | I.getPointerOperand(), I.getAlignment(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3665 | TLI->getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3666 | Scope); |
| 3667 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3668 | if (TLI->getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3669 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3670 | DAG, *TLI); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3671 | |
| 3672 | DAG.setRoot(OutChain); |
| 3673 | } |
| 3674 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3675 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 3676 | /// node. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3677 | void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3678 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3679 | bool HasChain = !I.doesNotAccessMemory(); |
| 3680 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 3681 | |
| 3682 | // Build the operand list. |
| 3683 | SmallVector<SDValue, 8> Ops; |
| 3684 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 3685 | if (OnlyLoad) { |
| 3686 | // We don't need to serialize loads against other loads. |
| 3687 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3688 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3689 | Ops.push_back(getRoot()); |
| 3690 | } |
| 3691 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3692 | |
| 3693 | // Info is set by getTgtMemInstrinsic |
| 3694 | TargetLowering::IntrinsicInfo Info; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3695 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 3696 | bool IsTgtIntrinsic = TLI->getTgtMemIntrinsic(Info, I, Intrinsic); |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3697 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3698 | // 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] | 3699 | if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID || |
| 3700 | Info.opc == ISD::INTRINSIC_W_CHAIN) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3701 | Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI->getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3702 | |
| 3703 | // Add all operands of the call to the operand list. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3704 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 3705 | SDValue Op = getValue(I.getArgOperand(i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3706 | Ops.push_back(Op); |
| 3707 | } |
| 3708 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3709 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3710 | ComputeValueVTs(*TLI, I.getType(), ValueVTs); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3711 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3712 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3713 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3714 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 3715 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3716 | |
| 3717 | // Create the node. |
| 3718 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3719 | if (IsTgtIntrinsic) { |
| 3720 | // This is target intrinsic that touches memory |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3721 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3722 | VTs, &Ops[0], Ops.size(), |
Chris Lattner | e9ba5dd | 2010-09-21 04:57:15 +0000 | [diff] [blame] | 3723 | Info.memVT, |
| 3724 | MachinePointerInfo(Info.ptrVal, Info.offset), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3725 | Info.align, Info.vol, |
| 3726 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3727 | } else if (!HasChain) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3728 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3729 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3730 | } else if (!I.getType()->isVoidTy()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3731 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3732 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3733 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3734 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3735 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3738 | if (HasChain) { |
| 3739 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 3740 | if (OnlyLoad) |
| 3741 | PendingLoads.push_back(Chain); |
| 3742 | else |
| 3743 | DAG.setRoot(Chain); |
| 3744 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3745 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3746 | if (!I.getType()->isVoidTy()) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3747 | if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 3748 | EVT VT = TLI->getValueType(PTy); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3749 | Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3750 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3751 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3752 | setValue(&I, Result); |
| 3753 | } |
| 3754 | } |
| 3755 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3756 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3757 | /// number with exponent of 1: |
| 3758 | /// |
| 3759 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3760 | /// |
Matt Beaumont-Gay | 50e75bf | 2013-02-25 18:11:18 +0000 | [diff] [blame] | 3761 | /// where Op is the hexadecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3762 | static SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3763 | GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3764 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3765 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 3766 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 3767 | DAG.getConstant(0x3f800000, MVT::i32)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3768 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3771 | /// GetExponent - Get the exponent: |
| 3772 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3773 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3774 | /// |
Matt Beaumont-Gay | 50e75bf | 2013-02-25 18:11:18 +0000 | [diff] [blame] | 3775 | /// where Op is the hexadecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3776 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3777 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3778 | SDLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3779 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3780 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 3781 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3782 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3783 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 3784 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3785 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3788 | /// getF32Constant - Get 32-bit floating point constant. |
| 3789 | static SDValue |
| 3790 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3791 | return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)), |
| 3792 | MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3795 | /// expandExp - Lower an exp intrinsic. Handles the special sequences for |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3796 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3797 | static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3798 | const TargetLowering &TLI) { |
| 3799 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3800 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3801 | |
| 3802 | // Put the exponent in the right bit position for later addition to the |
| 3803 | // final result: |
| 3804 | // |
| 3805 | // #define LOG2OFe 1.4426950f |
| 3806 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3807 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3808 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3809 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3810 | |
| 3811 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3812 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3813 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3814 | |
| 3815 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3816 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3817 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3818 | |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3819 | SDValue TwoToFracPartOfX; |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3820 | if (LimitFloatPrecision <= 6) { |
| 3821 | // For floating-point precision of 6: |
| 3822 | // |
| 3823 | // TwoToFractionalPartOfX = |
| 3824 | // 0.997535578f + |
| 3825 | // (0.735607626f + 0.252464424f * x) * x; |
| 3826 | // |
| 3827 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3828 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3829 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3830 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3831 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3832 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3833 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3834 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3835 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3836 | // For floating-point precision of 12: |
| 3837 | // |
| 3838 | // TwoToFractionalPartOfX = |
| 3839 | // 0.999892986f + |
| 3840 | // (0.696457318f + |
| 3841 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3842 | // |
| 3843 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3844 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3845 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3846 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3847 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3848 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3849 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3850 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3851 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3852 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3853 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3854 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3855 | // For floating-point precision of 18: |
| 3856 | // |
| 3857 | // TwoToFractionalPartOfX = |
| 3858 | // 0.999999982f + |
| 3859 | // (0.693148872f + |
| 3860 | // (0.240227044f + |
| 3861 | // (0.554906021e-1f + |
| 3862 | // (0.961591928e-2f + |
| 3863 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3864 | // |
| 3865 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3866 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3867 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3868 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3869 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3870 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3871 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3872 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3873 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3874 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3875 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3876 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3877 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3878 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3879 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3880 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3881 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3882 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3883 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 3884 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3885 | } |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3886 | |
| 3887 | // Add the exponent into the result in integer domain. |
| 3888 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX); |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3889 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 3890 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 3891 | t13, IntegerPartOfX)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3894 | // No special expansion. |
| 3895 | return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3898 | /// expandLog - Lower a log intrinsic. Handles the special sequences for |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3899 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3900 | static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3901 | const TargetLowering &TLI) { |
| 3902 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3903 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3904 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3905 | |
| 3906 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3907 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3908 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3909 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3910 | |
| 3911 | // Get the significand and build it into a floating-point number with |
| 3912 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3913 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3914 | |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3915 | SDValue LogOfMantissa; |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3916 | if (LimitFloatPrecision <= 6) { |
| 3917 | // For floating-point precision of 6: |
| 3918 | // |
| 3919 | // LogofMantissa = |
| 3920 | // -1.1609546f + |
| 3921 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3922 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3923 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3924 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3925 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3926 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3927 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3928 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3929 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3930 | getF32Constant(DAG, 0x3f949a29)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3931 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3932 | // For floating-point precision of 12: |
| 3933 | // |
| 3934 | // LogOfMantissa = |
| 3935 | // -1.7417939f + |
| 3936 | // (2.8212026f + |
| 3937 | // (-1.4699568f + |
| 3938 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3939 | // |
| 3940 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3941 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3942 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3943 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3944 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3945 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3946 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3947 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3948 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3949 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3950 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3951 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3952 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3953 | getF32Constant(DAG, 0x3fdef31a)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3954 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3955 | // For floating-point precision of 18: |
| 3956 | // |
| 3957 | // LogOfMantissa = |
| 3958 | // -2.1072184f + |
| 3959 | // (4.2372794f + |
| 3960 | // (-3.7029485f + |
| 3961 | // (2.2781945f + |
| 3962 | // (-0.87823314f + |
| 3963 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3964 | // |
| 3965 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3966 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3967 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3968 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3969 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3970 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3971 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3972 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3973 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3974 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3975 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3976 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3977 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3978 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3979 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3980 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3981 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3982 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3983 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3984 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3985 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3986 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3987 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3990 | // No special expansion. |
| 3991 | return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3994 | /// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3995 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3996 | static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3997 | const TargetLowering &TLI) { |
| 3998 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3999 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4000 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4001 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 4002 | // Get the exponent. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4003 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 4004 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4005 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 4006 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4007 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4008 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4009 | // Different possible minimax approximations of significand in |
| 4010 | // floating-point for various degrees of accuracy over [1,2]. |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4011 | SDValue Log2ofMantissa; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4012 | if (LimitFloatPrecision <= 6) { |
| 4013 | // For floating-point precision of 6: |
| 4014 | // |
| 4015 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 4016 | // |
| 4017 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4018 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4019 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4020 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4021 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4022 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4023 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 4024 | getF32Constant(DAG, 0x3fd6633d)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4025 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4026 | // For floating-point precision of 12: |
| 4027 | // |
| 4028 | // Log2ofMantissa = |
| 4029 | // -2.51285454f + |
| 4030 | // (4.07009056f + |
| 4031 | // (-2.12067489f + |
| 4032 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4033 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4034 | // error 0.0000876136000, which is better than 13 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, 0xbda7262e)); |
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, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4039 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4040 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4041 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4042 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4043 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4044 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4045 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4046 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 4047 | getF32Constant(DAG, 0x4020d29c)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4048 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4049 | // For floating-point precision of 18: |
| 4050 | // |
| 4051 | // Log2ofMantissa = |
| 4052 | // -3.0400495f + |
| 4053 | // (6.1129976f + |
| 4054 | // (-5.3420409f + |
| 4055 | // (3.2865683f + |
| 4056 | // (-1.2669343f + |
| 4057 | // (0.27515199f - |
| 4058 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 4059 | // |
| 4060 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4061 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4062 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4063 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4064 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4065 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4066 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4067 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4068 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4069 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4070 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4071 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4072 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4073 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4074 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4075 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4076 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4077 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4078 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 4079 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4080 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4081 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4082 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa); |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 4083 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4084 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4085 | // No special expansion. |
| 4086 | return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4089 | /// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4090 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4091 | static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4092 | const TargetLowering &TLI) { |
| 4093 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4094 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4095 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4096 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 4097 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4098 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4099 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4100 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4101 | |
| 4102 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 4103 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4104 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4105 | |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4106 | SDValue Log10ofMantissa; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4107 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4108 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4109 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4110 | // Log10ofMantissa = |
| 4111 | // -0.50419619f + |
| 4112 | // (0.60948995f - 0.10380950f * x) * x; |
| 4113 | // |
| 4114 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4115 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4116 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4117 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4118 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4119 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4120 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 4121 | getF32Constant(DAG, 0x3f011300)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4122 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4123 | // For floating-point precision of 12: |
| 4124 | // |
| 4125 | // Log10ofMantissa = |
| 4126 | // -0.64831180f + |
| 4127 | // (0.91751397f + |
| 4128 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 4129 | // |
| 4130 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4131 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4132 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4133 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4134 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4135 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4136 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4137 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4138 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4139 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
| 4140 | getF32Constant(DAG, 0x3f25f7c3)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4141 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4142 | // For floating-point precision of 18: |
| 4143 | // |
| 4144 | // Log10ofMantissa = |
| 4145 | // -0.84299375f + |
| 4146 | // (1.5327582f + |
| 4147 | // (-1.0688956f + |
| 4148 | // (0.49102474f + |
| 4149 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 4150 | // |
| 4151 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4152 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4153 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4154 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4155 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4156 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4157 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4158 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4159 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4160 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4161 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4162 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4163 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4164 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4165 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4166 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
| 4167 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4168 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4169 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4170 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa); |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 4171 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4172 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4173 | // No special expansion. |
| 4174 | return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4177 | /// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4178 | /// limited-precision mode. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4179 | static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG, |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4180 | const TargetLowering &TLI) { |
| 4181 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4182 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4183 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4184 | |
| 4185 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4186 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 4187 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4188 | |
| 4189 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4190 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 4191 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4192 | |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4193 | SDValue TwoToFractionalPartOfX; |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4194 | if (LimitFloatPrecision <= 6) { |
| 4195 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4196 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4197 | // TwoToFractionalPartOfX = |
| 4198 | // 0.997535578f + |
| 4199 | // (0.735607626f + 0.252464424f * x) * x; |
| 4200 | // |
| 4201 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4202 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4203 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4204 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4205 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4206 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4207 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 4208 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4209 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4210 | // For floating-point precision of 12: |
| 4211 | // |
| 4212 | // TwoToFractionalPartOfX = |
| 4213 | // 0.999892986f + |
| 4214 | // (0.696457318f + |
| 4215 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 4216 | // |
| 4217 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4218 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4219 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4220 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4221 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4222 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4223 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4224 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4225 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4226 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 4227 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4228 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4229 | // For floating-point precision of 18: |
| 4230 | // |
| 4231 | // TwoToFractionalPartOfX = |
| 4232 | // 0.999999982f + |
| 4233 | // (0.693148872f + |
| 4234 | // (0.240227044f + |
| 4235 | // (0.554906021e-1f + |
| 4236 | // (0.961591928e-2f + |
| 4237 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 4238 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4239 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4240 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4241 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4242 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4243 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4244 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4245 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4246 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4247 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4248 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4249 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4250 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4251 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4252 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 4253 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4254 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4255 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4256 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 4257 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4258 | } |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4259 | |
| 4260 | // Add the exponent into the result in integer domain. |
| 4261 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, |
| 4262 | TwoToFractionalPartOfX); |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4263 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 4264 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 4265 | t13, IntegerPartOfX)); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4266 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4267 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4268 | // No special expansion. |
| 4269 | return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4272 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 4273 | /// limited-precision mode with x == 10.0f. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4274 | static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS, |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4275 | SelectionDAG &DAG, const TargetLowering &TLI) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4276 | bool IsExp10 = false; |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4277 | if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4278 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4279 | if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) { |
| 4280 | APFloat Ten(10.0f); |
| 4281 | IsExp10 = LHSC->isExactlyValue(Ten); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4282 | } |
| 4283 | } |
| 4284 | |
Craig Topper | c1aa638 | 2012-11-25 00:48:58 +0000 | [diff] [blame] | 4285 | if (IsExp10) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4286 | // Put the exponent in the right bit position for later addition to the |
| 4287 | // final result: |
| 4288 | // |
| 4289 | // #define LOG2OF10 3.3219281f |
| 4290 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4291 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4292 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4293 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4294 | |
| 4295 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4296 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 4297 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4298 | |
| 4299 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4300 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 4301 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4302 | |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4303 | SDValue TwoToFractionalPartOfX; |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4304 | if (LimitFloatPrecision <= 6) { |
| 4305 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4306 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4307 | // twoToFractionalPartOfX = |
| 4308 | // 0.997535578f + |
| 4309 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4310 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4311 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4312 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4313 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4314 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4315 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4316 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4317 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 4318 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4319 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4320 | // For floating-point precision of 12: |
| 4321 | // |
| 4322 | // TwoToFractionalPartOfX = |
| 4323 | // 0.999892986f + |
| 4324 | // (0.696457318f + |
| 4325 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 4326 | // |
| 4327 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4328 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4329 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4330 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4331 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4332 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4333 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4334 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4335 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4336 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 4337 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4338 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4339 | // For floating-point precision of 18: |
| 4340 | // |
| 4341 | // TwoToFractionalPartOfX = |
| 4342 | // 0.999999982f + |
| 4343 | // (0.693148872f + |
| 4344 | // (0.240227044f + |
| 4345 | // (0.554906021e-1f + |
| 4346 | // (0.961591928e-2f + |
| 4347 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 4348 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4349 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4350 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4351 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4352 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4353 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4354 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4355 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4356 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4357 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4358 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4359 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4360 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4361 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4362 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 4363 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4364 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4365 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4366 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 4367 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4368 | } |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4369 | |
| 4370 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX); |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4371 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 4372 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 4373 | t13, IntegerPartOfX)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4376 | // No special expansion. |
| 4377 | return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4380 | |
| 4381 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4382 | static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4383 | SelectionDAG &DAG) { |
| 4384 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 4385 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 4386 | // optimizing for size, we only want to do this if the expansion would produce |
| 4387 | // a small number of multiplies, otherwise we do the full expansion. |
| 4388 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 4389 | // Get the exponent as a positive value. |
| 4390 | unsigned Val = RHSC->getSExtValue(); |
| 4391 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4392 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4393 | // powi(x, 0) -> 1.0 |
| 4394 | if (Val == 0) |
| 4395 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 4396 | |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 4397 | const Function *F = DAG.getMachineFunction().getFunction(); |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 4398 | if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 4399 | Attribute::OptimizeForSize) || |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4400 | // If optimizing for size, don't insert too many multiplies. This |
| 4401 | // inserts up to 5 multiplies. |
| 4402 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 4403 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4404 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4405 | // powi(x,15) generates one more multiply than it should), but this has |
| 4406 | // the benefit of being both really simple and much better than a libcall. |
| 4407 | SDValue Res; // Logically starts equal to 1.0 |
| 4408 | SDValue CurSquare = LHS; |
| 4409 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 4410 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4411 | if (Res.getNode()) |
| 4412 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 4413 | else |
| 4414 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 4415 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4416 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4417 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 4418 | CurSquare, CurSquare); |
| 4419 | Val >>= 1; |
| 4420 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4421 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4422 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 4423 | if (RHSC->getSExtValue() < 0) |
| 4424 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 4425 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 4426 | return Res; |
| 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | // Otherwise, expand to a libcall. |
| 4431 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 4432 | } |
| 4433 | |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4434 | // getTruncatedArgReg - Find underlying register used for an truncated |
| 4435 | // argument. |
| 4436 | static unsigned getTruncatedArgReg(const SDValue &N) { |
| 4437 | if (N.getOpcode() != ISD::TRUNCATE) |
| 4438 | return 0; |
| 4439 | |
| 4440 | const SDValue &Ext = N.getOperand(0); |
Stephen Lin | 09f8ca3 | 2013-07-06 21:44:25 +0000 | [diff] [blame] | 4441 | if (Ext.getOpcode() == ISD::AssertZext || |
| 4442 | Ext.getOpcode() == ISD::AssertSext) { |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4443 | const SDValue &CFR = Ext.getOperand(0); |
| 4444 | if (CFR.getOpcode() == ISD::CopyFromReg) |
| 4445 | return cast<RegisterSDNode>(CFR.getOperand(1))->getReg(); |
Craig Topper | 7eb46d8 | 2012-04-11 04:55:51 +0000 | [diff] [blame] | 4446 | if (CFR.getOpcode() == ISD::TRUNCATE) |
| 4447 | return getTruncatedArgReg(CFR); |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4448 | } |
| 4449 | return 0; |
| 4450 | } |
| 4451 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4452 | /// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function |
| 4453 | /// argument, create the corresponding DBG_VALUE machine instruction for it now. |
| 4454 | /// 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] | 4455 | bool |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 4456 | SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable, |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4457 | int64_t Offset, |
Dan Gohman | 5d11ea3 | 2010-05-01 00:33:16 +0000 | [diff] [blame] | 4458 | const SDValue &N) { |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4459 | const Argument *Arg = dyn_cast<Argument>(V); |
| 4460 | if (!Arg) |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4461 | return false; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4462 | |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 4463 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4464 | const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo(); |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4465 | |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 4466 | // Ignore inlined function arguments here. |
| 4467 | DIVariable DV(Variable); |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 4468 | if (DV.isInlinedFnArgument(MF.getFunction())) |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 4469 | return false; |
| 4470 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4471 | Optional<MachineOperand> Op; |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 4472 | // Some arguments' frame index is recorded during argument lowering. |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4473 | if (int FI = FuncInfo.getArgumentFrameIndex(Arg)) |
| 4474 | Op = MachineOperand::CreateFI(FI); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4475 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4476 | if (!Op && N.getNode()) { |
| 4477 | unsigned Reg; |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4478 | if (N.getOpcode() == ISD::CopyFromReg) |
| 4479 | Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
| 4480 | else |
| 4481 | Reg = getTruncatedArgReg(N); |
| 4482 | if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4483 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 4484 | unsigned PR = RegInfo.getLiveInPhysReg(Reg); |
| 4485 | if (PR) |
| 4486 | Reg = PR; |
| 4487 | } |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4488 | if (Reg) |
| 4489 | Op = MachineOperand::CreateReg(Reg, false); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4492 | if (!Op) { |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4493 | // Check if ValueMap has reg number. |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4494 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4495 | if (VMI != FuncInfo.ValueMap.end()) |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4496 | Op = MachineOperand::CreateReg(VMI->second, false); |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4497 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4498 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4499 | if (!Op && N.getNode()) |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4500 | // Check if frame index is available. |
| 4501 | if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode())) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4502 | if (FrameIndexSDNode *FINode = |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4503 | dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) |
| 4504 | Op = MachineOperand::CreateFI(FINode->getIndex()); |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4505 | |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4506 | if (!Op) |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4507 | return false; |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4508 | |
Adrian Prantl | 893ae83 | 2013-07-10 01:53:30 +0000 | [diff] [blame] | 4509 | // FIXME: This does not handle register-indirect values at offset 0. |
| 4510 | bool IsIndirect = Offset != 0; |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4511 | if (Op->isReg()) |
Adrian Prantl | 3517640 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 4512 | FuncInfo.ArgDbgValues.push_back(BuildMI(MF, getCurDebugLoc(), |
| 4513 | TII->get(TargetOpcode::DBG_VALUE), |
Adrian Prantl | 893ae83 | 2013-07-10 01:53:30 +0000 | [diff] [blame] | 4514 | IsIndirect, |
Adrian Prantl | 3517640 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 4515 | Op->getReg(), Offset, Variable)); |
| 4516 | else |
| 4517 | FuncInfo.ArgDbgValues.push_back( |
David Blaikie | 6d9dbd5 | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 4518 | BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE)) |
| 4519 | .addOperand(*Op).addImm(Offset).addMetadata(Variable)); |
Adrian Prantl | 3517640 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 4520 | |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4521 | return true; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4522 | } |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4523 | |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 4524 | // VisualStudio defines setjmp as _setjmp |
Michael J. Spencer | 1f40960 | 2010-09-24 19:48:47 +0000 | [diff] [blame] | 4525 | #if defined(_MSC_VER) && defined(setjmp) && \ |
| 4526 | !defined(setjmp_undefined_for_msvc) |
| 4527 | # pragma push_macro("setjmp") |
| 4528 | # undef setjmp |
| 4529 | # define setjmp_undefined_for_msvc |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 4530 | #endif |
| 4531 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4532 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 4533 | /// we want to emit this as a call to a named external function, return the name |
| 4534 | /// otherwise lower it and return null. |
| 4535 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4536 | SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4537 | const TargetLowering *TLI = TM.getTargetLowering(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4538 | SDLoc sdl = getCurSDLoc(); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4539 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4540 | SDValue Res; |
| 4541 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4542 | switch (Intrinsic) { |
| 4543 | default: |
| 4544 | // By default, turn this into a target intrinsic node. |
| 4545 | visitTargetIntrinsic(I, Intrinsic); |
| 4546 | return 0; |
| 4547 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 4548 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 4549 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 4550 | case Intrinsic::returnaddress: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4551 | setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI->getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4552 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4553 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 4554 | case Intrinsic::frameaddress: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4555 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI->getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4556 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4557 | return 0; |
| 4558 | case Intrinsic::setjmp: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4559 | return &"_setjmp"[!TLI->usesUnderscoreSetJmp()]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4560 | case Intrinsic::longjmp: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4561 | return &"_longjmp"[!TLI->usesUnderscoreLongJmp()]; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4562 | case Intrinsic::memcpy: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4563 | // Assert for address < 256 since we support only user defined address |
| 4564 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4565 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4566 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4567 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4568 | < 256 && |
| 4569 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4570 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4571 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4572 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4573 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4574 | if (!Align) |
| 4575 | 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] | 4576 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4577 | DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4578 | MachinePointerInfo(I.getArgOperand(0)), |
| 4579 | MachinePointerInfo(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4580 | return 0; |
| 4581 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4582 | case Intrinsic::memset: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4583 | // Assert for address < 256 since we support only user defined address |
| 4584 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4585 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4586 | < 256 && |
| 4587 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4588 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4589 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4590 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4591 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4592 | if (!Align) |
| 4593 | 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] | 4594 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4595 | DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4596 | MachinePointerInfo(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4597 | return 0; |
| 4598 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4599 | case Intrinsic::memmove: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4600 | // Assert for address < 256 since we support only user defined address |
| 4601 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4602 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4603 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4604 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4605 | < 256 && |
| 4606 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4607 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4608 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4609 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4610 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4611 | if (!Align) |
| 4612 | 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] | 4613 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4614 | DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4615 | MachinePointerInfo(I.getArgOperand(0)), |
| 4616 | MachinePointerInfo(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4617 | return 0; |
| 4618 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4619 | case Intrinsic::dbg_declare: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4620 | const DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 4621 | MDNode *Variable = DI.getVariable(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4622 | const Value *Address = DI.getAddress(); |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 4623 | DIVariable DIVar(Variable); |
| 4624 | assert((!DIVar || DIVar.isVariable()) && |
| 4625 | "Variable in DbgDeclareInst should be either null or a DIVariable."); |
| 4626 | if (!Address || !DIVar) { |
Eric Christopher | 8f2a88d | 2012-03-15 21:33:41 +0000 | [diff] [blame] | 4627 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Dale Johannesen | 8ac38f2 | 2010-02-08 21:53:27 +0000 | [diff] [blame] | 4628 | return 0; |
Eric Christopher | 8f2a88d | 2012-03-15 21:33:41 +0000 | [diff] [blame] | 4629 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4630 | |
Devang Patel | 3f74a11 | 2010-09-02 21:29:42 +0000 | [diff] [blame] | 4631 | // Check if address has undef value. |
| 4632 | if (isa<UndefValue>(Address) || |
| 4633 | (Address->use_empty() && !isa<Argument>(Address))) { |
Eric Christopher | 2441367 | 2012-02-23 03:39:39 +0000 | [diff] [blame] | 4634 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Devang Patel | 3f74a11 | 2010-09-02 21:29:42 +0000 | [diff] [blame] | 4635 | return 0; |
| 4636 | } |
| 4637 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4638 | SDValue &N = NodeMap[Address]; |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4639 | if (!N.getNode() && isa<Argument>(Address)) |
| 4640 | // Check unused arguments map. |
| 4641 | N = UnusedArgNodeMap[Address]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4642 | SDDbgValue *SDV; |
| 4643 | if (N.getNode()) { |
Devang Patel | 8e741ed | 2010-09-02 21:02:27 +0000 | [diff] [blame] | 4644 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 4645 | Address = BCI->getOperand(0); |
Eric Christopher | 178606d | 2012-02-24 01:59:08 +0000 | [diff] [blame] | 4646 | // Parameters are handled specially. |
| 4647 | bool isParameter = |
| 4648 | (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable || |
| 4649 | isa<Argument>(Address)); |
| 4650 | |
Devang Patel | 8e741ed | 2010-09-02 21:02:27 +0000 | [diff] [blame] | 4651 | const AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 4652 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4653 | if (isParameter && !AI) { |
| 4654 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode()); |
| 4655 | if (FINode) |
| 4656 | // Byval parameter. We have a frame index at this point. |
| 4657 | SDV = DAG.getDbgValue(Variable, FINode->getIndex(), |
| 4658 | 0, dl, SDNodeOrder); |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4659 | else { |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4660 | // Address is an argument, so try to emit its dbg value using |
| 4661 | // virtual register info from the FuncInfo.ValueMap. |
| 4662 | EmitFuncArgumentDbgValue(Address, Variable, 0, N); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4663 | return 0; |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4664 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4665 | } else if (AI) |
| 4666 | SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(), |
| 4667 | 0, dl, SDNodeOrder); |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4668 | else { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4669 | // Can't do anything with other non-AI cases yet. |
Eric Christopher | 2441367 | 2012-02-23 03:39:39 +0000 | [diff] [blame] | 4670 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Eric Christopher | 178606d | 2012-02-24 01:59:08 +0000 | [diff] [blame] | 4671 | DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t"); |
| 4672 | DEBUG(Address->dump()); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4673 | return 0; |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4674 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4675 | DAG.AddDbgValue(SDV, N.getNode(), isParameter); |
| 4676 | } else { |
Gabor Greif | fb4032f | 2010-10-01 10:32:19 +0000 | [diff] [blame] | 4677 | // 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] | 4678 | // virtual register info from the FuncInfo.ValueMap. |
Devang Patel | 6cd467b | 2010-08-26 22:53:27 +0000 | [diff] [blame] | 4679 | if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) { |
Devang Patel | 1397fdc | 2010-09-15 14:48:53 +0000 | [diff] [blame] | 4680 | // If variable is pinned by a alloca in dominating bb then |
| 4681 | // use StaticAllocaMap. |
| 4682 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
Devang Patel | 27ede1b | 2010-09-15 18:13:55 +0000 | [diff] [blame] | 4683 | if (AI->getParent() != DI.getParent()) { |
| 4684 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4685 | FuncInfo.StaticAllocaMap.find(AI); |
| 4686 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 4687 | SDV = DAG.getDbgValue(Variable, SI->second, |
| 4688 | 0, dl, SDNodeOrder); |
| 4689 | DAG.AddDbgValue(SDV, 0, false); |
| 4690 | return 0; |
| 4691 | } |
Devang Patel | 1397fdc | 2010-09-15 14:48:53 +0000 | [diff] [blame] | 4692 | } |
| 4693 | } |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 4694 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Devang Patel | 6cd467b | 2010-08-26 22:53:27 +0000 | [diff] [blame] | 4695 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4696 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4697 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4698 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4699 | case Intrinsic::dbg_value: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4700 | const DbgValueInst &DI = cast<DbgValueInst>(I); |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 4701 | DIVariable DIVar(DI.getVariable()); |
| 4702 | assert((!DIVar || DIVar.isVariable()) && |
| 4703 | "Variable in DbgValueInst should be either null or a DIVariable."); |
| 4704 | if (!DIVar) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4705 | return 0; |
| 4706 | |
| 4707 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4708 | uint64_t Offset = DI.getOffset(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4709 | const Value *V = DI.getValue(); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4710 | if (!V) |
| 4711 | return 0; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4712 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4713 | SDDbgValue *SDV; |
Devang Patel | 5787124 | 2011-08-03 23:13:55 +0000 | [diff] [blame] | 4714 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4715 | SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder); |
| 4716 | DAG.AddDbgValue(SDV, 0, false); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4717 | } else { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 4718 | // Do not use getValue() in here; we don't want to generate code at |
| 4719 | // this point if it hasn't been done yet. |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 4720 | SDValue N = NodeMap[V]; |
| 4721 | if (!N.getNode() && isa<Argument>(V)) |
| 4722 | // Check unused arguments map. |
| 4723 | N = UnusedArgNodeMap[V]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4724 | if (N.getNode()) { |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 4725 | if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) { |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4726 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 4727 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 4728 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 4729 | } |
Devang Patel | a778f5c | 2011-02-18 22:43:42 +0000 | [diff] [blame] | 4730 | } else if (!V->use_empty() ) { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 4731 | // Do not call getValue(V) yet, as we don't want to generate code. |
| 4732 | // Remember it for later. |
| 4733 | DanglingDebugInfo DDI(&DI, dl, SDNodeOrder); |
| 4734 | DanglingDebugInfoMap[V] = DDI; |
Devang Patel | 0991dfb | 2010-08-27 22:25:51 +0000 | [diff] [blame] | 4735 | } else { |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4736 | // 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] | 4737 | // data available is an unreferenced parameter. |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 4738 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4739 | } |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | // Build a debug info table entry. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4743 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4744 | V = BCI->getOperand(0); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4745 | const AllocaInst *AI = dyn_cast<AllocaInst>(V); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4746 | // Don't handle byval struct arguments or VLAs, for example. |
Eric Christopher | 7e1e18f | 2012-03-26 06:10:32 +0000 | [diff] [blame] | 4747 | if (!AI) { |
Eric Christopher | 9fc5c83 | 2012-03-28 07:34:36 +0000 | [diff] [blame] | 4748 | DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n"); |
| 4749 | DEBUG(dbgs() << " Last seen at:\n " << *V << "\n"); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4750 | return 0; |
Eric Christopher | 7e1e18f | 2012-03-26 06:10:32 +0000 | [diff] [blame] | 4751 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4752 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4753 | FuncInfo.StaticAllocaMap.find(AI); |
| 4754 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 4755 | return 0; // VLAs. |
| 4756 | int FI = SI->second; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4757 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4758 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 4759 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 4760 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4761 | return 0; |
| 4762 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4763 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4764 | case Intrinsic::eh_typeid_for: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4765 | // Find the type id for the given typeinfo. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4766 | GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0)); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4767 | unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV); |
| 4768 | Res = DAG.getConstant(TypeID, MVT::i32); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4769 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4770 | return 0; |
| 4771 | } |
| 4772 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4773 | case Intrinsic::eh_return_i32: |
| 4774 | case Intrinsic::eh_return_i64: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4775 | DAG.getMachineFunction().getMMI().setCallsEHReturn(true); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4776 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl, |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4777 | MVT::Other, |
| 4778 | getControlRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4779 | getValue(I.getArgOperand(0)), |
| 4780 | getValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4781 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4782 | case Intrinsic::eh_unwind_init: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4783 | DAG.getMachineFunction().getMMI().setCallsUnwindInit(true); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4784 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4785 | case Intrinsic::eh_dwarf_cfa: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4786 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4787 | TLI->getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4788 | SDValue Offset = DAG.getNode(ISD::ADD, sdl, |
Tom Stellard | edd08f7 | 2013-08-26 15:06:10 +0000 | [diff] [blame] | 4789 | CfaArg.getValueType(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4790 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl, |
Tom Stellard | edd08f7 | 2013-08-26 15:06:10 +0000 | [diff] [blame] | 4791 | CfaArg.getValueType()), |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4792 | CfaArg); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4793 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4794 | TLI->getPointerTy(), |
| 4795 | DAG.getConstant(0, TLI->getPointerTy())); |
Tom Stellard | edd08f7 | 2013-08-26 15:06:10 +0000 | [diff] [blame] | 4796 | setValue(&I, DAG.getNode(ISD::ADD, sdl, FA.getValueType(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4797 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4798 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4799 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4800 | case Intrinsic::eh_sjlj_callsite: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4801 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4802 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0)); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4803 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4804 | assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4805 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4806 | MMI.setCurrentCallSite(CI->getZExtValue()); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4807 | return 0; |
| 4808 | } |
Bill Wendling | 6ef9417 | 2011-09-28 03:36:43 +0000 | [diff] [blame] | 4809 | case Intrinsic::eh_sjlj_functioncontext: { |
| 4810 | // Get and store the index of the function context. |
| 4811 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
Bill Wendling | adbf7b2 | 2011-09-28 03:52:41 +0000 | [diff] [blame] | 4812 | AllocaInst *FnCtx = |
| 4813 | cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts()); |
Bill Wendling | 6ef9417 | 2011-09-28 03:36:43 +0000 | [diff] [blame] | 4814 | int FI = FuncInfo.StaticAllocaMap[FnCtx]; |
| 4815 | MFI->setFunctionContextIndex(FI); |
| 4816 | return 0; |
| 4817 | } |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4818 | case Intrinsic::eh_sjlj_setjmp: { |
Bill Wendling | ce370cf | 2011-10-07 21:25:38 +0000 | [diff] [blame] | 4819 | SDValue Ops[2]; |
| 4820 | Ops[0] = getRoot(); |
| 4821 | Ops[1] = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4822 | SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl, |
Bill Wendling | ce370cf | 2011-10-07 21:25:38 +0000 | [diff] [blame] | 4823 | DAG.getVTList(MVT::i32, MVT::Other), |
| 4824 | Ops, 2); |
| 4825 | setValue(&I, Op.getValue(0)); |
| 4826 | DAG.setRoot(Op.getValue(1)); |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4827 | return 0; |
| 4828 | } |
Jim Grosbach | 5eb1951 | 2010-05-22 01:06:18 +0000 | [diff] [blame] | 4829 | case Intrinsic::eh_sjlj_longjmp: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4830 | DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other, |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 4831 | getRoot(), getValue(I.getArgOperand(0)))); |
| 4832 | return 0; |
| 4833 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4834 | |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4835 | case Intrinsic::x86_mmx_pslli_w: |
| 4836 | case Intrinsic::x86_mmx_pslli_d: |
| 4837 | case Intrinsic::x86_mmx_pslli_q: |
| 4838 | case Intrinsic::x86_mmx_psrli_w: |
| 4839 | case Intrinsic::x86_mmx_psrli_d: |
| 4840 | case Intrinsic::x86_mmx_psrli_q: |
| 4841 | case Intrinsic::x86_mmx_psrai_w: |
| 4842 | case Intrinsic::x86_mmx_psrai_d: { |
| 4843 | SDValue ShAmt = getValue(I.getArgOperand(1)); |
| 4844 | if (isa<ConstantSDNode>(ShAmt)) { |
| 4845 | visitTargetIntrinsic(I, Intrinsic); |
| 4846 | return 0; |
| 4847 | } |
| 4848 | unsigned NewIntrinsic = 0; |
| 4849 | EVT ShAmtVT = MVT::v2i32; |
| 4850 | switch (Intrinsic) { |
| 4851 | case Intrinsic::x86_mmx_pslli_w: |
| 4852 | NewIntrinsic = Intrinsic::x86_mmx_psll_w; |
| 4853 | break; |
| 4854 | case Intrinsic::x86_mmx_pslli_d: |
| 4855 | NewIntrinsic = Intrinsic::x86_mmx_psll_d; |
| 4856 | break; |
| 4857 | case Intrinsic::x86_mmx_pslli_q: |
| 4858 | NewIntrinsic = Intrinsic::x86_mmx_psll_q; |
| 4859 | break; |
| 4860 | case Intrinsic::x86_mmx_psrli_w: |
| 4861 | NewIntrinsic = Intrinsic::x86_mmx_psrl_w; |
| 4862 | break; |
| 4863 | case Intrinsic::x86_mmx_psrli_d: |
| 4864 | NewIntrinsic = Intrinsic::x86_mmx_psrl_d; |
| 4865 | break; |
| 4866 | case Intrinsic::x86_mmx_psrli_q: |
| 4867 | NewIntrinsic = Intrinsic::x86_mmx_psrl_q; |
| 4868 | break; |
| 4869 | case Intrinsic::x86_mmx_psrai_w: |
| 4870 | NewIntrinsic = Intrinsic::x86_mmx_psra_w; |
| 4871 | break; |
| 4872 | case Intrinsic::x86_mmx_psrai_d: |
| 4873 | NewIntrinsic = Intrinsic::x86_mmx_psra_d; |
| 4874 | break; |
| 4875 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 4876 | } |
| 4877 | |
| 4878 | // The vector shift intrinsics with scalars uses 32b shift amounts but |
| 4879 | // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits |
| 4880 | // to be zero. |
| 4881 | // We must do this early because v2i32 is not a legal type. |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4882 | SDValue ShOps[2]; |
| 4883 | ShOps[0] = ShAmt; |
| 4884 | ShOps[1] = DAG.getConstant(0, MVT::i32); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4885 | ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4886 | EVT DestVT = TLI->getValueType(I.getType()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4887 | ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt); |
| 4888 | Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT, |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4889 | DAG.getConstant(NewIntrinsic, MVT::i32), |
| 4890 | getValue(I.getArgOperand(0)), ShAmt); |
| 4891 | setValue(&I, Res); |
| 4892 | return 0; |
| 4893 | } |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4894 | case Intrinsic::x86_avx_vinsertf128_pd_256: |
| 4895 | case Intrinsic::x86_avx_vinsertf128_ps_256: |
Craig Topper | b45c969 | 2012-04-07 22:32:29 +0000 | [diff] [blame] | 4896 | case Intrinsic::x86_avx_vinsertf128_si_256: |
| 4897 | case Intrinsic::x86_avx2_vinserti128: { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4898 | EVT DestVT = TLI->getValueType(I.getType()); |
| 4899 | EVT ElVT = TLI->getValueType(I.getArgOperand(1)->getType()); |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4900 | uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) * |
| 4901 | ElVT.getVectorNumElements(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4902 | Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT, |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4903 | getValue(I.getArgOperand(0)), |
| 4904 | getValue(I.getArgOperand(1)), |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 4905 | DAG.getConstant(Idx, TLI->getVectorIdxTy())); |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4906 | setValue(&I, Res); |
| 4907 | return 0; |
| 4908 | } |
| 4909 | case Intrinsic::x86_avx_vextractf128_pd_256: |
| 4910 | case Intrinsic::x86_avx_vextractf128_ps_256: |
| 4911 | case Intrinsic::x86_avx_vextractf128_si_256: |
| 4912 | case Intrinsic::x86_avx2_vextracti128: { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4913 | EVT DestVT = TLI->getValueType(I.getType()); |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4914 | uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) * |
| 4915 | DestVT.getVectorNumElements(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4916 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT, |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4917 | getValue(I.getArgOperand(0)), |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 4918 | DAG.getConstant(Idx, TLI->getVectorIdxTy())); |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4919 | setValue(&I, Res); |
| 4920 | return 0; |
| 4921 | } |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4922 | case Intrinsic::convertff: |
| 4923 | case Intrinsic::convertfsi: |
| 4924 | case Intrinsic::convertfui: |
| 4925 | case Intrinsic::convertsif: |
| 4926 | case Intrinsic::convertuif: |
| 4927 | case Intrinsic::convertss: |
| 4928 | case Intrinsic::convertsu: |
| 4929 | case Intrinsic::convertus: |
| 4930 | case Intrinsic::convertuu: { |
| 4931 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4932 | switch (Intrinsic) { |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 4933 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4934 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4935 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4936 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4937 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4938 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4939 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4940 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4941 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4942 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4943 | } |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4944 | EVT DestVT = TLI->getValueType(I.getType()); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4945 | const Value *Op1 = I.getArgOperand(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4946 | Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4947 | DAG.getValueType(DestVT), |
| 4948 | DAG.getValueType(getValue(Op1).getValueType()), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4949 | getValue(I.getArgOperand(1)), |
| 4950 | getValue(I.getArgOperand(2)), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4951 | Code); |
| 4952 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4953 | return 0; |
| 4954 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4955 | case Intrinsic::powi: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4956 | setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4957 | getValue(I.getArgOperand(1)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4958 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4959 | case Intrinsic::log: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4960 | setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4961 | return 0; |
| 4962 | case Intrinsic::log2: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4963 | setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4964 | return 0; |
| 4965 | case Intrinsic::log10: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4966 | setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4967 | return 0; |
| 4968 | case Intrinsic::exp: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4969 | setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4970 | return 0; |
| 4971 | case Intrinsic::exp2: |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4972 | setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4973 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4974 | case Intrinsic::pow: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4975 | setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4976 | getValue(I.getArgOperand(1)), DAG, *TLI)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4977 | return 0; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4978 | case Intrinsic::sqrt: |
Peter Collingbourne | b34d3aa | 2012-05-28 21:48:37 +0000 | [diff] [blame] | 4979 | case Intrinsic::fabs: |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4980 | case Intrinsic::sin: |
| 4981 | case Intrinsic::cos: |
Dan Gohman | 27db99f | 2012-07-26 17:43:27 +0000 | [diff] [blame] | 4982 | case Intrinsic::floor: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4983 | case Intrinsic::ceil: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4984 | case Intrinsic::trunc: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4985 | case Intrinsic::rint: |
Hal Finkel | 41418d1 | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 4986 | case Intrinsic::nearbyint: |
| 4987 | case Intrinsic::round: { |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4988 | unsigned Opcode; |
| 4989 | switch (Intrinsic) { |
| 4990 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 4991 | case Intrinsic::sqrt: Opcode = ISD::FSQRT; break; |
| 4992 | case Intrinsic::fabs: Opcode = ISD::FABS; break; |
| 4993 | case Intrinsic::sin: Opcode = ISD::FSIN; break; |
| 4994 | case Intrinsic::cos: Opcode = ISD::FCOS; break; |
| 4995 | case Intrinsic::floor: Opcode = ISD::FFLOOR; break; |
| 4996 | case Intrinsic::ceil: Opcode = ISD::FCEIL; break; |
| 4997 | case Intrinsic::trunc: Opcode = ISD::FTRUNC; break; |
| 4998 | case Intrinsic::rint: Opcode = ISD::FRINT; break; |
| 4999 | case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break; |
Hal Finkel | 41418d1 | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 5000 | case Intrinsic::round: Opcode = ISD::FROUND; break; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5003 | setValue(&I, DAG.getNode(Opcode, sdl, |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 5004 | getValue(I.getArgOperand(0)).getValueType(), |
| 5005 | getValue(I.getArgOperand(0)))); |
| 5006 | return 0; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 5007 | } |
Hal Finkel | 66d1fa6 | 2013-08-19 23:35:46 +0000 | [diff] [blame] | 5008 | case Intrinsic::copysign: |
| 5009 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl, |
| 5010 | getValue(I.getArgOperand(0)).getValueType(), |
| 5011 | getValue(I.getArgOperand(0)), |
| 5012 | getValue(I.getArgOperand(1)))); |
| 5013 | return 0; |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 5014 | case Intrinsic::fma: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5015 | setValue(&I, DAG.getNode(ISD::FMA, sdl, |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 5016 | getValue(I.getArgOperand(0)).getValueType(), |
| 5017 | getValue(I.getArgOperand(0)), |
| 5018 | getValue(I.getArgOperand(1)), |
| 5019 | getValue(I.getArgOperand(2)))); |
| 5020 | return 0; |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 5021 | case Intrinsic::fmuladd: { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5022 | EVT VT = TLI->getValueType(I.getType()); |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 5023 | if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict && |
Stephen Lin | e54885a | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 5024 | TLI->isFMAFasterThanFMulAndFAdd(VT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5025 | setValue(&I, DAG.getNode(ISD::FMA, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 5026 | getValue(I.getArgOperand(0)).getValueType(), |
| 5027 | getValue(I.getArgOperand(0)), |
| 5028 | getValue(I.getArgOperand(1)), |
| 5029 | getValue(I.getArgOperand(2)))); |
| 5030 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5031 | SDValue Mul = DAG.getNode(ISD::FMUL, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 5032 | getValue(I.getArgOperand(0)).getValueType(), |
| 5033 | getValue(I.getArgOperand(0)), |
| 5034 | getValue(I.getArgOperand(1))); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5035 | SDValue Add = DAG.getNode(ISD::FADD, sdl, |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 5036 | getValue(I.getArgOperand(0)).getValueType(), |
| 5037 | Mul, |
| 5038 | getValue(I.getArgOperand(2))); |
| 5039 | setValue(&I, Add); |
| 5040 | } |
| 5041 | return 0; |
| 5042 | } |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 5043 | case Intrinsic::convert_to_fp16: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5044 | setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5045 | MVT::i16, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 5046 | return 0; |
| 5047 | case Intrinsic::convert_from_fp16: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5048 | setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5049 | MVT::f32, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 5050 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5051 | case Intrinsic::pcmarker: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5052 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5053 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5054 | return 0; |
| 5055 | } |
| 5056 | case Intrinsic::readcyclecounter: { |
| 5057 | SDValue Op = getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5058 | Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl, |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5059 | DAG.getVTList(MVT::i64, MVT::Other), |
| 5060 | &Op, 1); |
| 5061 | setValue(&I, Res); |
| 5062 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5063 | return 0; |
| 5064 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5065 | case Intrinsic::bswap: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5066 | setValue(&I, DAG.getNode(ISD::BSWAP, sdl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5067 | getValue(I.getArgOperand(0)).getValueType(), |
| 5068 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5069 | return 0; |
| 5070 | case Intrinsic::cttz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5071 | SDValue Arg = getValue(I.getArgOperand(0)); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 5072 | ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5073 | EVT Ty = Arg.getValueType(); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 5074 | setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5075 | sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5076 | return 0; |
| 5077 | } |
| 5078 | case Intrinsic::ctlz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5079 | SDValue Arg = getValue(I.getArgOperand(0)); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 5080 | ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5081 | EVT Ty = Arg.getValueType(); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 5082 | setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5083 | sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5084 | return 0; |
| 5085 | } |
| 5086 | case Intrinsic::ctpop: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5087 | SDValue Arg = getValue(I.getArgOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5088 | EVT Ty = Arg.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5089 | setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5090 | return 0; |
| 5091 | } |
| 5092 | case Intrinsic::stacksave: { |
| 5093 | SDValue Op = getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5094 | Res = DAG.getNode(ISD::STACKSAVE, sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5095 | DAG.getVTList(TLI->getPointerTy(), MVT::Other), &Op, 1); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5096 | setValue(&I, Res); |
| 5097 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5098 | return 0; |
| 5099 | } |
| 5100 | case Intrinsic::stackrestore: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5101 | Res = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5102 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5103 | return 0; |
| 5104 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 5105 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5106 | // Emit code into the DAG to store the stack guard onto the stack. |
| 5107 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5108 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5109 | EVT PtrTy = TLI->getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5110 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5111 | SDValue Src = getValue(I.getArgOperand(0)); // The guard's value. |
| 5112 | AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5113 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 5114 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5115 | MFI->setStackProtectorIndex(FI); |
| 5116 | |
| 5117 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 5118 | |
| 5119 | // Store the stack protector onto the stack. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5120 | Res = DAG.getStore(getRoot(), sdl, Src, FIN, |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 5121 | MachinePointerInfo::getFixedStack(FI), |
| 5122 | true, false, 0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5123 | setValue(&I, Res); |
| 5124 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5125 | return 0; |
| 5126 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5127 | case Intrinsic::objectsize: { |
| 5128 | // 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] | 5129 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5130 | |
| 5131 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 5132 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5133 | SDValue Arg = getValue(I.getCalledValue()); |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 5134 | EVT Ty = Arg.getValueType(); |
| 5135 | |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 5136 | if (CI->isZero()) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5137 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5138 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5139 | Res = DAG.getConstant(0, Ty); |
| 5140 | |
| 5141 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5142 | return 0; |
| 5143 | } |
Justin Holewinski | c2b7f5f | 2013-05-21 14:37:16 +0000 | [diff] [blame] | 5144 | case Intrinsic::annotation: |
| 5145 | case Intrinsic::ptr_annotation: |
| 5146 | // Drop the intrinsic, but forward the value |
| 5147 | setValue(&I, getValue(I.getOperand(0))); |
| 5148 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5149 | case Intrinsic::var_annotation: |
| 5150 | // Discard annotate attributes |
| 5151 | return 0; |
| 5152 | |
| 5153 | case Intrinsic::init_trampoline: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5154 | const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5155 | |
| 5156 | SDValue Ops[6]; |
| 5157 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5158 | Ops[1] = getValue(I.getArgOperand(0)); |
| 5159 | Ops[2] = getValue(I.getArgOperand(1)); |
| 5160 | Ops[3] = getValue(I.getArgOperand(2)); |
| 5161 | Ops[4] = DAG.getSrcValue(I.getArgOperand(0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5162 | Ops[5] = DAG.getSrcValue(F); |
| 5163 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5164 | Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5165 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5166 | DAG.setRoot(Res); |
| 5167 | return 0; |
| 5168 | } |
| 5169 | case Intrinsic::adjust_trampoline: { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5170 | setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5171 | TLI->getPointerTy(), |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5172 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5173 | return 0; |
| 5174 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5175 | case Intrinsic::gcroot: |
| 5176 | if (GFI) { |
Bill Wendling | 95dd442 | 2012-05-01 22:50:45 +0000 | [diff] [blame] | 5177 | const Value *Alloca = I.getArgOperand(0)->stripPointerCasts(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5178 | const Constant *TypeMap = cast<Constant>(I.getArgOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5179 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5180 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 5181 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 5182 | } |
| 5183 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5184 | case Intrinsic::gcread: |
| 5185 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5186 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5187 | case Intrinsic::flt_rounds: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5188 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5189 | return 0; |
Jakub Staszak | 9da9934 | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 5190 | |
| 5191 | case Intrinsic::expect: { |
| 5192 | // Just replace __builtin_expect(exp, c) with EXP. |
| 5193 | setValue(&I, getValue(I.getArgOperand(0))); |
| 5194 | return 0; |
| 5195 | } |
| 5196 | |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5197 | case Intrinsic::debugtrap: |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5198 | case Intrinsic::trap: { |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5199 | StringRef TrapFuncName = TM.Options.getTrapFunctionName(); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5200 | if (TrapFuncName.empty()) { |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 5201 | ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ? |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5202 | ISD::TRAP : ISD::DEBUGTRAP; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5203 | DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot())); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5204 | return 0; |
| 5205 | } |
| 5206 | TargetLowering::ArgListTy Args; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5207 | TargetLowering:: |
| 5208 | CallLoweringInfo CLI(getRoot(), I.getType(), |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5209 | false, false, false, false, 0, CallingConv::C, |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 5210 | /*isTailCall=*/false, |
| 5211 | /*doesNotRet=*/false, /*isReturnValueUsed=*/true, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5212 | DAG.getExternalSymbol(TrapFuncName.data(), |
| 5213 | TLI->getPointerTy()), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5214 | Args, DAG, sdl); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5215 | std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5216 | DAG.setRoot(Result.second); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5217 | return 0; |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5218 | } |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5219 | |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 5220 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5221 | case Intrinsic::sadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5222 | case Intrinsic::usub_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5223 | case Intrinsic::ssub_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5224 | case Intrinsic::umul_with_overflow: |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5225 | case Intrinsic::smul_with_overflow: { |
| 5226 | ISD::NodeType Op; |
| 5227 | switch (Intrinsic) { |
| 5228 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 5229 | case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break; |
| 5230 | case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break; |
| 5231 | case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break; |
| 5232 | case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break; |
| 5233 | case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break; |
| 5234 | case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break; |
| 5235 | } |
| 5236 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 5237 | SDValue Op2 = getValue(I.getArgOperand(1)); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 5238 | |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5239 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5240 | setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2)); |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5241 | return 0; |
| 5242 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5243 | case Intrinsic::prefetch: { |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5244 | SDValue Ops[5]; |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5245 | unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5246 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5247 | Ops[1] = getValue(I.getArgOperand(0)); |
| 5248 | Ops[2] = getValue(I.getArgOperand(1)); |
| 5249 | Ops[3] = getValue(I.getArgOperand(2)); |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5250 | Ops[4] = getValue(I.getArgOperand(3)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5251 | DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl, |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5252 | DAG.getVTList(MVT::Other), |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5253 | &Ops[0], 5, |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5254 | EVT::getIntegerVT(*Context, 8), |
| 5255 | MachinePointerInfo(I.getArgOperand(0)), |
| 5256 | 0, /* align */ |
| 5257 | false, /* volatile */ |
| 5258 | rw==0, /* read */ |
| 5259 | rw==1)); /* write */ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5260 | return 0; |
| 5261 | } |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5262 | case Intrinsic::lifetime_start: |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5263 | case Intrinsic::lifetime_end: { |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5264 | bool IsStart = (Intrinsic == Intrinsic::lifetime_start); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5265 | // Stack coloring is not enabled in O0, discard region information. |
| 5266 | if (TM.getOptLevel() == CodeGenOpt::None) |
| 5267 | return 0; |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5268 | |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5269 | SmallVector<Value *, 4> Allocas; |
| 5270 | GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD); |
| 5271 | |
Craig Topper | f22fd3f | 2013-07-03 05:11:49 +0000 | [diff] [blame] | 5272 | for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(), |
| 5273 | E = Allocas.end(); Object != E; ++Object) { |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5274 | AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object); |
| 5275 | |
| 5276 | // Could not find an Alloca. |
| 5277 | if (!LifetimeObject) |
| 5278 | continue; |
| 5279 | |
| 5280 | int FI = FuncInfo.StaticAllocaMap[LifetimeObject]; |
| 5281 | |
| 5282 | SDValue Ops[2]; |
| 5283 | Ops[0] = getRoot(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5284 | Ops[1] = DAG.getFrameIndex(FI, TLI->getPointerTy(), true); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5285 | unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END); |
| 5286 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5287 | Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5288 | DAG.setRoot(Res); |
| 5289 | } |
Nadav Rotem | 5882e56 | 2013-02-01 19:25:23 +0000 | [diff] [blame] | 5290 | return 0; |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5291 | } |
| 5292 | case Intrinsic::invariant_start: |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5293 | // Discard region information. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5294 | setValue(&I, DAG.getUNDEF(TLI->getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5295 | return 0; |
| 5296 | case Intrinsic::invariant_end: |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5297 | // Discard region information. |
| 5298 | return 0; |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 5299 | case Intrinsic::stackprotectorcheck: { |
| 5300 | // Do not actually emit anything for this basic block. Instead we initialize |
| 5301 | // the stack protector descriptor and export the guard variable so we can |
| 5302 | // access it in FinishBasicBlock. |
| 5303 | const BasicBlock *BB = I.getParent(); |
| 5304 | SPDescriptor.initialize(BB, FuncInfo.MBBMap[BB], I); |
| 5305 | ExportFromCurrentBlock(SPDescriptor.getGuard()); |
| 5306 | |
| 5307 | // Flush our exports since we are going to process a terminator. |
| 5308 | (void)getControlRoot(); |
| 5309 | return 0; |
| 5310 | } |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 5311 | case Intrinsic::donothing: |
| 5312 | // ignore |
| 5313 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5314 | } |
| 5315 | } |
| 5316 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5317 | void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5318 | bool isTailCall, |
| 5319 | MachineBasicBlock *LandingPad) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5320 | PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 5321 | FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 5322 | Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5323 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 5324 | MCSymbol *BeginLabel = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5325 | |
| 5326 | TargetLowering::ArgListTy Args; |
| 5327 | TargetLowering::ArgListEntry Entry; |
| 5328 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5329 | |
| 5330 | // Check whether the function can return without sret-demotion. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 5331 | SmallVector<ISD::OutputArg, 4> Outs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5332 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 5333 | GetReturnInfo(RetTy, CS.getAttributes(), Outs, *TLI); |
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 | bool CanLowerReturn = TLI->CanLowerReturn(CS.getCallingConv(), |
| 5336 | DAG.getMachineFunction(), |
| 5337 | FTy->isVarArg(), Outs, |
| 5338 | FTy->getContext()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5339 | |
| 5340 | SDValue DemoteStackSlot; |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5341 | int DemoteStackIdx = -100; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5342 | |
| 5343 | if (!CanLowerReturn) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5344 | uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize( |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5345 | FTy->getReturnType()); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5346 | unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment( |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5347 | FTy->getReturnType()); |
| 5348 | MachineFunction &MF = DAG.getMachineFunction(); |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5349 | DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5350 | Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5351 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5352 | DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI->getPointerTy()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5353 | Entry.Node = DemoteStackSlot; |
| 5354 | Entry.Ty = StackSlotPtrType; |
| 5355 | Entry.isSExt = false; |
| 5356 | Entry.isZExt = false; |
| 5357 | Entry.isInReg = false; |
| 5358 | Entry.isSRet = true; |
| 5359 | Entry.isNest = false; |
| 5360 | Entry.isByVal = false; |
Stephen Lin | 456ca04 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 5361 | Entry.isReturned = false; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5362 | Entry.Alignment = Align; |
| 5363 | Args.push_back(Entry); |
| 5364 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 5365 | } |
| 5366 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5367 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5368 | i != e; ++i) { |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 5369 | const Value *V = *i; |
| 5370 | |
| 5371 | // Skip empty types |
| 5372 | if (V->getType()->isEmptyTy()) |
| 5373 | continue; |
| 5374 | |
| 5375 | SDValue ArgNode = getValue(V); |
| 5376 | Entry.Node = ArgNode; Entry.Ty = V->getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5377 | |
| 5378 | unsigned attrInd = i - CS.arg_begin() + 1; |
Stephen Lin | 456ca04 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 5379 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 5380 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 5381 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 5382 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 5383 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 5384 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
| 5385 | Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned); |
| 5386 | Entry.Alignment = CS.getParamAlignment(attrInd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5387 | Args.push_back(Entry); |
| 5388 | } |
| 5389 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5390 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5391 | // Insert a label before the invoke call to mark the try range. This can be |
| 5392 | // used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5393 | BeginLabel = MMI.getContext().CreateTempSymbol(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 5394 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5395 | // For SjLj, keep track of which landing pads go with which invokes |
| 5396 | // so as to maintain the ordering of pads in the LSDA. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5397 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5398 | if (CallSiteIndex) { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5399 | MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
Bill Wendling | 30e6740 | 2011-10-05 22:24:35 +0000 | [diff] [blame] | 5400 | LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex); |
Bill Wendling | a8512ed | 2011-10-04 22:00:35 +0000 | [diff] [blame] | 5401 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5402 | // Now that the call site is handled, stop tracking it. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5403 | MMI.setCurrentCallSite(0); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5406 | // Both PendingLoads and PendingExports must be flushed here; |
| 5407 | // this call might not return. |
| 5408 | (void)getRoot(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5409 | DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5412 | // Check if target-independent constraints permit a tail call here. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5413 | // Target-dependent constraints are checked within TLI->LowerCallTo. |
| 5414 | if (isTailCall && !isInTailCallPosition(CS, *TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5415 | isTailCall = false; |
| 5416 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5417 | TargetLowering:: |
| 5418 | CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5419 | getCurSDLoc(), CS); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5420 | std::pair<SDValue,SDValue> Result = TLI->LowerCallTo(CLI); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5421 | assert((isTailCall || Result.second.getNode()) && |
| 5422 | "Non-null chain expected with non-tail call!"); |
| 5423 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 5424 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5425 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5426 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5427 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5428 | // The instruction result is the result of loading from the |
| 5429 | // hidden sret parameter. |
| 5430 | SmallVector<EVT, 1> PVTs; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5431 | Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5432 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5433 | ComputeValueVTs(*TLI, PtrRetTy, PVTs); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5434 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 5435 | EVT PtrVT = PVTs[0]; |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5436 | |
| 5437 | SmallVector<EVT, 4> RetTys; |
| 5438 | SmallVector<uint64_t, 4> Offsets; |
| 5439 | RetTy = FTy->getReturnType(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5440 | ComputeValueVTs(*TLI, RetTy, RetTys, &Offsets); |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5441 | |
| 5442 | unsigned NumValues = RetTys.size(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5443 | SmallVector<SDValue, 4> Values(NumValues); |
| 5444 | SmallVector<SDValue, 4> Chains(NumValues); |
| 5445 | |
| 5446 | for (unsigned i = 0; i < NumValues; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5447 | SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5448 | DemoteStackSlot, |
| 5449 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5450 | SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5451 | MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5452 | false, false, false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5453 | Values[i] = L; |
| 5454 | Chains[i] = L.getValue(1); |
| 5455 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5456 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5457 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5458 | MVT::Other, &Chains[0], NumValues); |
| 5459 | PendingLoads.push_back(Chain); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5460 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5461 | setValue(CS.getInstruction(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5462 | DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5463 | DAG.getVTList(&RetTys[0], RetTys.size()), |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5464 | &Values[0], Values.size())); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5465 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5466 | |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5467 | if (!Result.second.getNode()) { |
Evan Cheng | c249e48 | 2011-04-01 19:57:01 +0000 | [diff] [blame] | 5468 | // As a special case, a null chain means that a tail call has been emitted and |
| 5469 | // the DAG root is already updated. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5470 | HasTailCall = true; |
Tim Northover | e5a81a1 | 2013-07-06 12:58:45 +0000 | [diff] [blame] | 5471 | |
| 5472 | // Since there's no actual continuation from this block, nothing can be |
| 5473 | // relying on us setting vregs for them. |
| 5474 | PendingExports.clear(); |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5475 | } else { |
| 5476 | DAG.setRoot(Result.second); |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5477 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5478 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5479 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5480 | // Insert a label at the end of the invoke call to mark the try range. This |
| 5481 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5482 | MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5483 | DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5484 | |
| 5485 | // Inform MachineModuleInfo of range. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5486 | MMI.addInvoke(LandingPad, BeginLabel, EndLabel); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5487 | } |
| 5488 | } |
| 5489 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5490 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 5491 | /// value is equal or not-equal to zero. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5492 | static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) { |
| 5493 | 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] | 5494 | UI != E; ++UI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5495 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5496 | if (IC->isEquality()) |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5497 | if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5498 | if (C->isNullValue()) |
| 5499 | continue; |
| 5500 | // Unknown instruction. |
| 5501 | return false; |
| 5502 | } |
| 5503 | return true; |
| 5504 | } |
| 5505 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5506 | static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5507 | Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5508 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5509 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5510 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 5511 | // input is from a string literal. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5512 | if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5513 | // Cast pointer to the type we really want to load. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5514 | LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput), |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5515 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5516 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5517 | if (const Constant *LoadCst = |
| 5518 | ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput), |
| 5519 | Builder.TD)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5520 | return Builder.getValue(LoadCst); |
| 5521 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5522 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5523 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 5524 | // still constant memory, the input chain can be the entry node. |
| 5525 | SDValue Root; |
| 5526 | bool ConstantMemory = false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5527 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5528 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 5529 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 5530 | Root = Builder.DAG.getEntryNode(); |
| 5531 | ConstantMemory = true; |
| 5532 | } else { |
| 5533 | // Do not serialize non-volatile loads against each other. |
| 5534 | Root = Builder.DAG.getRoot(); |
| 5535 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5536 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5537 | SDValue Ptr = Builder.getValue(PtrVal); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5538 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5539 | Ptr, MachinePointerInfo(PtrVal), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5540 | false /*volatile*/, |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 5541 | false /*nontemporal*/, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5542 | false /*isinvariant*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5543 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5544 | if (!ConstantMemory) |
| 5545 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 5546 | return LoadVal; |
| 5547 | } |
| 5548 | |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5549 | /// processIntegerCallValue - Record the value for an instruction that |
| 5550 | /// produces an integer result, converting the type where necessary. |
| 5551 | void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I, |
| 5552 | SDValue Value, |
| 5553 | bool IsSigned) { |
| 5554 | EVT VT = TM.getTargetLowering()->getValueType(I.getType(), true); |
| 5555 | if (IsSigned) |
| 5556 | Value = DAG.getSExtOrTrunc(Value, getCurSDLoc(), VT); |
| 5557 | else |
| 5558 | Value = DAG.getZExtOrTrunc(Value, getCurSDLoc(), VT); |
| 5559 | setValue(&I, Value); |
| 5560 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5561 | |
| 5562 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 5563 | /// If so, return true and lower it, otherwise return false and it will be |
| 5564 | /// lowered like a normal call. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5565 | bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5566 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 5567 | if (I.getNumArgOperands() != 3) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5568 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5569 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5570 | const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 5571 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5572 | !I.getArgOperand(2)->getType()->isIntegerTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 5573 | !I.getType()->isIntegerTy()) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5574 | return false; |
| 5575 | |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5576 | const Value *Size = I.getArgOperand(2); |
| 5577 | const ConstantInt *CSize = dyn_cast<ConstantInt>(Size); |
| 5578 | if (CSize && CSize->getZExtValue() == 0) { |
Richard Sandiford | ac168b8 | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5579 | EVT CallVT = TM.getTargetLowering()->getValueType(I.getType(), true); |
| 5580 | setValue(&I, DAG.getConstant(0, CallVT)); |
| 5581 | return true; |
| 5582 | } |
| 5583 | |
Richard Sandiford | ac168b8 | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5584 | const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo(); |
| 5585 | std::pair<SDValue, SDValue> Res = |
| 5586 | TSI.EmitTargetCodeForMemcmp(DAG, getCurSDLoc(), DAG.getRoot(), |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5587 | getValue(LHS), getValue(RHS), getValue(Size), |
| 5588 | MachinePointerInfo(LHS), |
| 5589 | MachinePointerInfo(RHS)); |
Richard Sandiford | ac168b8 | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5590 | if (Res.first.getNode()) { |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5591 | processIntegerCallValue(I, Res.first, true); |
| 5592 | PendingLoads.push_back(Res.second); |
Richard Sandiford | ac168b8 | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5593 | return true; |
| 5594 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5595 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5596 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 5597 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5598 | if (CSize && IsOnlyUsedInZeroEqualityComparison(&I)) { |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5599 | bool ActuallyDoIt = true; |
| 5600 | MVT LoadVT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5601 | Type *LoadTy; |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5602 | switch (CSize->getZExtValue()) { |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5603 | default: |
| 5604 | LoadVT = MVT::Other; |
| 5605 | LoadTy = 0; |
| 5606 | ActuallyDoIt = false; |
| 5607 | break; |
| 5608 | case 2: |
| 5609 | LoadVT = MVT::i16; |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5610 | LoadTy = Type::getInt16Ty(CSize->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5611 | break; |
| 5612 | case 4: |
| 5613 | LoadVT = MVT::i32; |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5614 | LoadTy = Type::getInt32Ty(CSize->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5615 | break; |
| 5616 | case 8: |
| 5617 | LoadVT = MVT::i64; |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5618 | LoadTy = Type::getInt64Ty(CSize->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5619 | break; |
| 5620 | /* |
| 5621 | case 16: |
| 5622 | LoadVT = MVT::v4i32; |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5623 | LoadTy = Type::getInt32Ty(CSize->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5624 | LoadTy = VectorType::get(LoadTy, 4); |
| 5625 | break; |
| 5626 | */ |
| 5627 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5628 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5629 | // This turns into unaligned loads. We only do this if the target natively |
| 5630 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 5631 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5632 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5633 | // Require that we can find a legal MVT, and only do this if the target |
| 5634 | // supports unaligned loads of that type. Expanding into byte loads would |
| 5635 | // bloat the code. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5636 | const TargetLowering *TLI = TM.getTargetLowering(); |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5637 | if (ActuallyDoIt && CSize->getZExtValue() > 4) { |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5638 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 5639 | // 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] | 5640 | if (!TLI->isTypeLegal(LoadVT) ||!TLI->allowsUnalignedMemoryAccesses(LoadVT)) |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5641 | ActuallyDoIt = false; |
| 5642 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5643 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5644 | if (ActuallyDoIt) { |
| 5645 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 5646 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5647 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5648 | SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal, |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5649 | ISD::SETNE); |
Richard Sandiford | 6a079fe | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 5650 | processIntegerCallValue(I, Res, false); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5651 | return true; |
| 5652 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5653 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5654 | |
| 5655 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5656 | return false; |
| 5657 | } |
| 5658 | |
Richard Sandiford | 8c20158 | 2013-08-20 09:38:48 +0000 | [diff] [blame] | 5659 | /// visitMemChrCall -- See if we can lower a memchr call into an optimized |
| 5660 | /// form. If so, return true and lower it, otherwise return false and it |
| 5661 | /// will be lowered like a normal call. |
| 5662 | bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) { |
| 5663 | // Verify that the prototype makes sense. void *memchr(void *, int, size_t) |
| 5664 | if (I.getNumArgOperands() != 3) |
| 5665 | return false; |
| 5666 | |
| 5667 | const Value *Src = I.getArgOperand(0); |
| 5668 | const Value *Char = I.getArgOperand(1); |
| 5669 | const Value *Length = I.getArgOperand(2); |
| 5670 | if (!Src->getType()->isPointerTy() || |
| 5671 | !Char->getType()->isIntegerTy() || |
| 5672 | !Length->getType()->isIntegerTy() || |
| 5673 | !I.getType()->isPointerTy()) |
| 5674 | return false; |
| 5675 | |
| 5676 | const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo(); |
| 5677 | std::pair<SDValue, SDValue> Res = |
| 5678 | TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(), |
| 5679 | getValue(Src), getValue(Char), getValue(Length), |
| 5680 | MachinePointerInfo(Src)); |
| 5681 | if (Res.first.getNode()) { |
| 5682 | setValue(&I, Res.first); |
| 5683 | PendingLoads.push_back(Res.second); |
| 5684 | return true; |
| 5685 | } |
| 5686 | |
| 5687 | return false; |
| 5688 | } |
| 5689 | |
Richard Sandiford | 4fc7355 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 5690 | /// visitStrCpyCall -- See if we can lower a strcpy or stpcpy call into an |
| 5691 | /// optimized form. If so, return true and lower it, otherwise return false |
| 5692 | /// and it will be lowered like a normal call. |
| 5693 | bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) { |
| 5694 | // Verify that the prototype makes sense. char *strcpy(char *, char *) |
| 5695 | if (I.getNumArgOperands() != 2) |
| 5696 | return false; |
| 5697 | |
| 5698 | const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1); |
| 5699 | if (!Arg0->getType()->isPointerTy() || |
| 5700 | !Arg1->getType()->isPointerTy() || |
| 5701 | !I.getType()->isPointerTy()) |
| 5702 | return false; |
| 5703 | |
| 5704 | const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo(); |
| 5705 | std::pair<SDValue, SDValue> Res = |
| 5706 | TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(), |
| 5707 | getValue(Arg0), getValue(Arg1), |
| 5708 | MachinePointerInfo(Arg0), |
| 5709 | MachinePointerInfo(Arg1), isStpcpy); |
| 5710 | if (Res.first.getNode()) { |
| 5711 | setValue(&I, Res.first); |
| 5712 | DAG.setRoot(Res.second); |
| 5713 | return true; |
| 5714 | } |
| 5715 | |
| 5716 | return false; |
| 5717 | } |
| 5718 | |
Richard Sandiford | e1b2af7 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5719 | /// visitStrCmpCall - See if we can lower a call to strcmp in an optimized form. |
| 5720 | /// If so, return true and lower it, otherwise return false and it will be |
| 5721 | /// lowered like a normal call. |
| 5722 | bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) { |
| 5723 | // Verify that the prototype makes sense. int strcmp(void*,void*) |
| 5724 | if (I.getNumArgOperands() != 2) |
| 5725 | return false; |
| 5726 | |
| 5727 | const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1); |
| 5728 | if (!Arg0->getType()->isPointerTy() || |
| 5729 | !Arg1->getType()->isPointerTy() || |
| 5730 | !I.getType()->isIntegerTy()) |
| 5731 | return false; |
| 5732 | |
| 5733 | const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo(); |
| 5734 | std::pair<SDValue, SDValue> Res = |
| 5735 | TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(), |
| 5736 | getValue(Arg0), getValue(Arg1), |
| 5737 | MachinePointerInfo(Arg0), |
| 5738 | MachinePointerInfo(Arg1)); |
| 5739 | if (Res.first.getNode()) { |
| 5740 | processIntegerCallValue(I, Res.first, true); |
| 5741 | PendingLoads.push_back(Res.second); |
| 5742 | return true; |
| 5743 | } |
| 5744 | |
| 5745 | return false; |
| 5746 | } |
| 5747 | |
Richard Sandiford | 19262ee | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 5748 | /// visitStrLenCall -- See if we can lower a strlen call into an optimized |
| 5749 | /// form. If so, return true and lower it, otherwise return false and it |
| 5750 | /// will be lowered like a normal call. |
| 5751 | bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) { |
| 5752 | // Verify that the prototype makes sense. size_t strlen(char *) |
| 5753 | if (I.getNumArgOperands() != 1) |
| 5754 | return false; |
| 5755 | |
| 5756 | const Value *Arg0 = I.getArgOperand(0); |
| 5757 | if (!Arg0->getType()->isPointerTy() || !I.getType()->isIntegerTy()) |
| 5758 | return false; |
| 5759 | |
| 5760 | const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo(); |
| 5761 | std::pair<SDValue, SDValue> Res = |
| 5762 | TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(), |
| 5763 | getValue(Arg0), MachinePointerInfo(Arg0)); |
| 5764 | if (Res.first.getNode()) { |
| 5765 | processIntegerCallValue(I, Res.first, false); |
| 5766 | PendingLoads.push_back(Res.second); |
| 5767 | return true; |
| 5768 | } |
| 5769 | |
| 5770 | return false; |
| 5771 | } |
| 5772 | |
| 5773 | /// visitStrNLenCall -- See if we can lower a strnlen call into an optimized |
| 5774 | /// form. If so, return true and lower it, otherwise return false and it |
| 5775 | /// will be lowered like a normal call. |
| 5776 | bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) { |
| 5777 | // Verify that the prototype makes sense. size_t strnlen(char *, size_t) |
| 5778 | if (I.getNumArgOperands() != 2) |
| 5779 | return false; |
| 5780 | |
| 5781 | const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1); |
| 5782 | if (!Arg0->getType()->isPointerTy() || |
| 5783 | !Arg1->getType()->isIntegerTy() || |
| 5784 | !I.getType()->isIntegerTy()) |
| 5785 | return false; |
| 5786 | |
| 5787 | const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo(); |
| 5788 | std::pair<SDValue, SDValue> Res = |
| 5789 | TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(), |
| 5790 | getValue(Arg0), getValue(Arg1), |
| 5791 | MachinePointerInfo(Arg0)); |
| 5792 | if (Res.first.getNode()) { |
| 5793 | processIntegerCallValue(I, Res.first, false); |
| 5794 | PendingLoads.push_back(Res.second); |
| 5795 | return true; |
| 5796 | } |
| 5797 | |
| 5798 | return false; |
| 5799 | } |
| 5800 | |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5801 | /// visitUnaryFloatCall - If a call instruction is a unary floating-point |
| 5802 | /// operation (as expected), translate it to an SDNode with the specified opcode |
| 5803 | /// and return true. |
| 5804 | bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I, |
| 5805 | unsigned Opcode) { |
| 5806 | // Sanity check that it really is a unary floating-point call. |
| 5807 | if (I.getNumArgOperands() != 1 || |
| 5808 | !I.getArgOperand(0)->getType()->isFloatingPointTy() || |
| 5809 | I.getType() != I.getArgOperand(0)->getType() || |
| 5810 | !I.onlyReadsMemory()) |
| 5811 | return false; |
| 5812 | |
| 5813 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5814 | setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp)); |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5815 | return true; |
| 5816 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5817 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5818 | void SelectionDAGBuilder::visitCall(const CallInst &I) { |
Chris Lattner | 598751e | 2010-07-05 05:36:21 +0000 | [diff] [blame] | 5819 | // Handle inline assembly differently. |
| 5820 | if (isa<InlineAsm>(I.getCalledValue())) { |
| 5821 | visitInlineAsm(&I); |
| 5822 | return; |
| 5823 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5824 | |
Michael J. Spencer | 391b43b | 2010-10-21 20:49:23 +0000 | [diff] [blame] | 5825 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Michael J. Spencer | c9c137b | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 5826 | ComputeUsesVAFloatArgument(I, &MMI); |
Michael J. Spencer | 391b43b | 2010-10-21 20:49:23 +0000 | [diff] [blame] | 5827 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5828 | const char *RenameFn = 0; |
| 5829 | if (Function *F = I.getCalledFunction()) { |
| 5830 | if (F->isDeclaration()) { |
Chris Lattner | 598751e | 2010-07-05 05:36:21 +0000 | [diff] [blame] | 5831 | if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 5832 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 5833 | RenameFn = visitIntrinsicCall(I, IID); |
| 5834 | if (!RenameFn) |
| 5835 | return; |
| 5836 | } |
| 5837 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5838 | if (unsigned IID = F->getIntrinsicID()) { |
| 5839 | RenameFn = visitIntrinsicCall(I, IID); |
| 5840 | if (!RenameFn) |
| 5841 | return; |
| 5842 | } |
| 5843 | } |
| 5844 | |
| 5845 | // Check for well-known libc/libm calls. If the function is internal, it |
| 5846 | // can't be a library call. |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5847 | LibFunc::Func Func; |
| 5848 | if (!F->hasLocalLinkage() && F->hasName() && |
| 5849 | LibInfo->getLibFunc(F->getName(), Func) && |
| 5850 | LibInfo->hasOptimizedCodeGen(Func)) { |
| 5851 | switch (Func) { |
| 5852 | default: break; |
| 5853 | case LibFunc::copysign: |
| 5854 | case LibFunc::copysignf: |
| 5855 | case LibFunc::copysignl: |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 5856 | if (I.getNumArgOperands() == 2 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5857 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 5858 | I.getType() == I.getArgOperand(0)->getType() && |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5859 | I.getType() == I.getArgOperand(1)->getType() && |
| 5860 | I.onlyReadsMemory()) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5861 | SDValue LHS = getValue(I.getArgOperand(0)); |
| 5862 | SDValue RHS = getValue(I.getArgOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5863 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(), |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 5864 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5865 | return; |
| 5866 | } |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5867 | break; |
| 5868 | case LibFunc::fabs: |
| 5869 | case LibFunc::fabsf: |
| 5870 | case LibFunc::fabsl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5871 | if (visitUnaryFloatCall(I, ISD::FABS)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5872 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5873 | break; |
| 5874 | case LibFunc::sin: |
| 5875 | case LibFunc::sinf: |
| 5876 | case LibFunc::sinl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5877 | if (visitUnaryFloatCall(I, ISD::FSIN)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5878 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5879 | break; |
| 5880 | case LibFunc::cos: |
| 5881 | case LibFunc::cosf: |
| 5882 | case LibFunc::cosl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5883 | if (visitUnaryFloatCall(I, ISD::FCOS)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5884 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5885 | break; |
| 5886 | case LibFunc::sqrt: |
| 5887 | case LibFunc::sqrtf: |
| 5888 | case LibFunc::sqrtl: |
Preston Gurd | b704d23 | 2013-05-27 15:44:35 +0000 | [diff] [blame] | 5889 | case LibFunc::sqrt_finite: |
| 5890 | case LibFunc::sqrtf_finite: |
| 5891 | case LibFunc::sqrtl_finite: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5892 | if (visitUnaryFloatCall(I, ISD::FSQRT)) |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 5893 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5894 | break; |
| 5895 | case LibFunc::floor: |
| 5896 | case LibFunc::floorf: |
| 5897 | case LibFunc::floorl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5898 | if (visitUnaryFloatCall(I, ISD::FFLOOR)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5899 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5900 | break; |
| 5901 | case LibFunc::nearbyint: |
| 5902 | case LibFunc::nearbyintf: |
| 5903 | case LibFunc::nearbyintl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5904 | if (visitUnaryFloatCall(I, ISD::FNEARBYINT)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5905 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5906 | break; |
| 5907 | case LibFunc::ceil: |
| 5908 | case LibFunc::ceilf: |
| 5909 | case LibFunc::ceill: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5910 | if (visitUnaryFloatCall(I, ISD::FCEIL)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5911 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5912 | break; |
| 5913 | case LibFunc::rint: |
| 5914 | case LibFunc::rintf: |
| 5915 | case LibFunc::rintl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5916 | if (visitUnaryFloatCall(I, ISD::FRINT)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5917 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5918 | break; |
Hal Finkel | 41418d1 | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 5919 | case LibFunc::round: |
| 5920 | case LibFunc::roundf: |
| 5921 | case LibFunc::roundl: |
| 5922 | if (visitUnaryFloatCall(I, ISD::FROUND)) |
| 5923 | return; |
| 5924 | break; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5925 | case LibFunc::trunc: |
| 5926 | case LibFunc::truncf: |
| 5927 | case LibFunc::truncl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5928 | if (visitUnaryFloatCall(I, ISD::FTRUNC)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5929 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5930 | break; |
| 5931 | case LibFunc::log2: |
| 5932 | case LibFunc::log2f: |
| 5933 | case LibFunc::log2l: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5934 | if (visitUnaryFloatCall(I, ISD::FLOG2)) |
Owen Anderson | 4e0adfa | 2011-12-15 00:54:12 +0000 | [diff] [blame] | 5935 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5936 | break; |
| 5937 | case LibFunc::exp2: |
| 5938 | case LibFunc::exp2f: |
| 5939 | case LibFunc::exp2l: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5940 | if (visitUnaryFloatCall(I, ISD::FEXP2)) |
Owen Anderson | 4e0adfa | 2011-12-15 00:54:12 +0000 | [diff] [blame] | 5941 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5942 | break; |
| 5943 | case LibFunc::memcmp: |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5944 | if (visitMemCmpCall(I)) |
| 5945 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5946 | break; |
Richard Sandiford | 8c20158 | 2013-08-20 09:38:48 +0000 | [diff] [blame] | 5947 | case LibFunc::memchr: |
| 5948 | if (visitMemChrCall(I)) |
| 5949 | return; |
| 5950 | break; |
Richard Sandiford | 4fc7355 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 5951 | case LibFunc::strcpy: |
| 5952 | if (visitStrCpyCall(I, false)) |
| 5953 | return; |
| 5954 | break; |
| 5955 | case LibFunc::stpcpy: |
| 5956 | if (visitStrCpyCall(I, true)) |
| 5957 | return; |
| 5958 | break; |
Richard Sandiford | e1b2af7 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5959 | case LibFunc::strcmp: |
| 5960 | if (visitStrCmpCall(I)) |
| 5961 | return; |
| 5962 | break; |
Richard Sandiford | 19262ee | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 5963 | case LibFunc::strlen: |
| 5964 | if (visitStrLenCall(I)) |
| 5965 | return; |
| 5966 | break; |
| 5967 | case LibFunc::strnlen: |
| 5968 | if (visitStrNLenCall(I)) |
| 5969 | return; |
| 5970 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5971 | } |
| 5972 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5973 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5974 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5975 | SDValue Callee; |
| 5976 | if (!RenameFn) |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5977 | Callee = getValue(I.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5978 | else |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 5979 | Callee = DAG.getExternalSymbol(RenameFn, |
| 5980 | TM.getTargetLowering()->getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5981 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 5982 | // Check if we can potentially perform a tail call. More detailed checking is |
| 5983 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 5984 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5985 | } |
| 5986 | |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5987 | namespace { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5988 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5989 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 5990 | /// lowering. |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5991 | class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 5992 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5993 | /// CallOperand - If this is the result output operand or a clobber |
| 5994 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 5995 | /// This gets modified as the asm is processed. |
| 5996 | SDValue CallOperand; |
| 5997 | |
| 5998 | /// AssignedRegs - If this is a register or register class operand, this |
| 5999 | /// contains the set of register corresponding to the operand. |
| 6000 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6001 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 6002 | explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6003 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 6004 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6005 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6006 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 6007 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6008 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6009 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 6010 | const TargetLowering &TLI, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6011 | const DataLayout *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6012 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6013 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 6014 | if (isa<BasicBlock>(CallOperandVal)) |
| 6015 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6016 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6017 | llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6018 | |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 6019 | // FIXME: code duplicated from TargetLowering::ParseConstraints(). |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 6020 | // If this is an indirect operand, the operand is a pointer to the |
| 6021 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 6022 | if (isIndirect) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6023 | llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 6024 | if (!PtrTy) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 6025 | report_fatal_error("Indirect operand for inline asm not a pointer!"); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 6026 | OpTy = PtrTy->getElementType(); |
| 6027 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6028 | |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 6029 | // Look for vector wrapped in a struct. e.g. { <16 x i8> }. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6030 | if (StructType *STy = dyn_cast<StructType>(OpTy)) |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 6031 | if (STy->getNumElements() == 1) |
| 6032 | OpTy = STy->getElementType(0); |
| 6033 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 6034 | // If OpTy is not a single value, it may be a struct/union that we |
| 6035 | // can tile with integers. |
| 6036 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 6037 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 6038 | switch (BitSize) { |
| 6039 | default: break; |
| 6040 | case 1: |
| 6041 | case 8: |
| 6042 | case 16: |
| 6043 | case 32: |
| 6044 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 6045 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 6046 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 6047 | break; |
| 6048 | } |
| 6049 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6050 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 6051 | return TLI.getValueType(OpTy, true); |
| 6052 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6053 | }; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 6054 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 6055 | typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector; |
| 6056 | |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 6057 | } // end anonymous namespace |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6058 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6059 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 6060 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 6061 | /// register allocator to handle the assignment process. However, if the asm |
| 6062 | /// 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] | 6063 | /// allocation. This produces generally horrible, but correct, code. |
| 6064 | /// |
| 6065 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6066 | /// |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 6067 | static void GetRegistersForValue(SelectionDAG &DAG, |
| 6068 | const TargetLowering &TLI, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6069 | SDLoc DL, |
Benjamin Kramer | 8b93ff2 | 2012-02-24 14:01:17 +0000 | [diff] [blame] | 6070 | SDISelAsmOperandInfo &OpInfo) { |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 6071 | LLVMContext &Context = *DAG.getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6072 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6073 | MachineFunction &MF = DAG.getMachineFunction(); |
| 6074 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6075 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6076 | // If this is a constraint for a single physreg, or a constraint for a |
| 6077 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6078 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6079 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 6080 | OpInfo.ConstraintVT); |
| 6081 | |
| 6082 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6083 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 6084 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 6085 | // cast of the input value. More generally, handle any case where the input |
| 6086 | // value disagrees with the register class we plan to stick this in. |
| 6087 | if (OpInfo.Type == InlineAsm::isInput && |
| 6088 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6089 | // 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] | 6090 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 6091 | // vector types). |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 6092 | MVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 6093 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 6094 | OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 6095 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 6096 | OpInfo.ConstraintVT = RegVT; |
| 6097 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 6098 | // If the input is a FP value and we want it in FP registers, do a |
| 6099 | // bitcast to the corresponding integer type. This turns an f64 value |
| 6100 | // into i64, which can be passed with two i32 values on a 32-bit |
| 6101 | // machine. |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 6102 | RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits()); |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 6103 | OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 6104 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 6105 | OpInfo.ConstraintVT = RegVT; |
| 6106 | } |
| 6107 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6108 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6109 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 6110 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6111 | |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 6112 | MVT RegVT; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6113 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6114 | |
| 6115 | // If this is a constraint for a specific physical register, like {r17}, |
| 6116 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 6117 | if (unsigned AssignedReg = PhysReg.first) { |
| 6118 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6119 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 6120 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6121 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6122 | // Get the actual register value type. This is important, because the user |
| 6123 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 6124 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 6125 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6126 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6127 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 6128 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6129 | |
| 6130 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 6131 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 6132 | TargetRegisterClass::iterator I = RC->begin(); |
| 6133 | for (; *I != AssignedReg; ++I) |
| 6134 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6135 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6136 | // Already added the first reg. |
| 6137 | --NumRegs; ++I; |
| 6138 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 6139 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6140 | Regs.push_back(*I); |
| 6141 | } |
| 6142 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6143 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6144 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6145 | return; |
| 6146 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6147 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6148 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 6149 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 6150 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 6151 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6152 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6153 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6154 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6155 | // Create the appropriate number of virtual registers. |
| 6156 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 6157 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 6158 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6159 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6160 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6161 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6162 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6163 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6164 | // Otherwise, we couldn't allocate enough registers for this. |
| 6165 | } |
| 6166 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6167 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 6168 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6169 | void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) { |
| 6170 | const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6171 | |
| 6172 | /// ConstraintOperands - Information about all of the constraints. |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 6173 | SDISelAsmOperandInfoVector ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6174 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6175 | const TargetLowering *TLI = TM.getTargetLowering(); |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 6176 | TargetLowering::AsmOperandInfoVector |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6177 | TargetConstraints = TLI->ParseConstraints(CS); |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 6178 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 6179 | bool hasMemory = false; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6180 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6181 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 6182 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 6183 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 6184 | ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i])); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6185 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6186 | |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 6187 | MVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6188 | |
| 6189 | // Compute the value type for each operand. |
| 6190 | switch (OpInfo.Type) { |
| 6191 | case InlineAsm::isOutput: |
| 6192 | // Indirect outputs just consume an argument. |
| 6193 | if (OpInfo.isIndirect) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6194 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6195 | break; |
| 6196 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6197 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6198 | // The return value of the call is this value. As such, there is no |
| 6199 | // corresponding argument. |
Nick Lewycky | 8de3400 | 2011-09-30 22:19:53 +0000 | [diff] [blame] | 6200 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6201 | if (StructType *STy = dyn_cast<StructType>(CS.getType())) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6202 | OpVT = TLI->getSimpleValueType(STy->getElementType(ResNo)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6203 | } else { |
| 6204 | assert(ResNo == 0 && "Asm only has one result!"); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6205 | OpVT = TLI->getSimpleValueType(CS.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6206 | } |
| 6207 | ++ResNo; |
| 6208 | break; |
| 6209 | case InlineAsm::isInput: |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6210 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6211 | break; |
| 6212 | case InlineAsm::isClobber: |
| 6213 | // Nothing to do. |
| 6214 | break; |
| 6215 | } |
| 6216 | |
| 6217 | // If this is an input or an indirect output, process the call argument. |
| 6218 | // BasicBlocks are labels, currently appearing only in asm's. |
| 6219 | if (OpInfo.CallOperandVal) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6220 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6221 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 6222 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6223 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6224 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6225 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6226 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), *TLI, TD). |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 6227 | getSimpleVT(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6228 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6229 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6230 | OpInfo.ConstraintVT = OpVT; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6231 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 6232 | // Indirect operand accesses access memory. |
| 6233 | if (OpInfo.isIndirect) |
| 6234 | hasMemory = true; |
| 6235 | else { |
| 6236 | for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) { |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 6237 | TargetLowering::ConstraintType |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6238 | CType = TLI->getConstraintType(OpInfo.Codes[j]); |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 6239 | if (CType == TargetLowering::C_Memory) { |
| 6240 | hasMemory = true; |
| 6241 | break; |
| 6242 | } |
| 6243 | } |
| 6244 | } |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6245 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6246 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 6247 | SDValue Chain, Flag; |
| 6248 | |
| 6249 | // We won't need to flush pending loads if this asm doesn't touch |
| 6250 | // memory and is nonvolatile. |
| 6251 | if (hasMemory || IA->hasSideEffects()) |
| 6252 | Chain = getRoot(); |
| 6253 | else |
| 6254 | Chain = DAG.getRoot(); |
| 6255 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6256 | // Second pass over the constraints: compute which constraint option to use |
| 6257 | // and assign registers to constraints that want a specific physreg. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 6258 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6259 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6260 | |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 6261 | // If this is an output operand with a matching input operand, look up the |
| 6262 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 6263 | // other is floating point, or their sizes are different, flag it as an |
| 6264 | // error. |
| 6265 | if (OpInfo.hasMatchingInput()) { |
| 6266 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6267 | |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 6268 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 6269 | std::pair<unsigned, const TargetRegisterClass*> MatchRC = |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6270 | TLI->getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 6271 | OpInfo.ConstraintVT); |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 6272 | std::pair<unsigned, const TargetRegisterClass*> InputRC = |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6273 | TLI->getRegForInlineAsmConstraint(Input.ConstraintCode, |
| 6274 | Input.ConstraintVT); |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 6275 | if ((OpInfo.ConstraintVT.isInteger() != |
| 6276 | Input.ConstraintVT.isInteger()) || |
Eric Christopher | 5427ede | 2011-07-14 20:13:52 +0000 | [diff] [blame] | 6277 | (MatchRC.second != InputRC.second)) { |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 6278 | report_fatal_error("Unsupported asm: input constraint" |
| 6279 | " with a matching output constraint of" |
| 6280 | " incompatible type!"); |
| 6281 | } |
| 6282 | Input.ConstraintVT = OpInfo.ConstraintVT; |
| 6283 | } |
| 6284 | } |
| 6285 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6286 | // Compute the constraint code and ConstraintType to use. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6287 | TLI->ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6288 | |
Eric Christopher | fffe363 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 6289 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 6290 | OpInfo.Type == InlineAsm::isClobber) |
| 6291 | continue; |
| 6292 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6293 | // If this is a memory input, and if the operand is not indirect, do what we |
| 6294 | // need to to provide an address for the memory input. |
| 6295 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 6296 | !OpInfo.isIndirect) { |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 6297 | assert((OpInfo.isMultipleAlternative || |
| 6298 | (OpInfo.Type == InlineAsm::isInput)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6299 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6300 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6301 | // Memory operands really want the address of the value. If we don't have |
| 6302 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 6303 | // it to a stack slot. |
Eric Christopher | e0b42c0 | 2011-06-03 17:21:23 +0000 | [diff] [blame] | 6304 | // TODO: This isn't quite right. We need to handle these according to |
| 6305 | // the addressing mode that the constraint wants. Also, this may take |
| 6306 | // an additional register for the computation and we don't want that |
| 6307 | // either. |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 6308 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6309 | // If the operand is a float, integer, or vector constant, spill to a |
| 6310 | // constant pool entry to get its address. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6311 | const Value *OpVal = OpInfo.CallOperandVal; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6312 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 6313 | isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6314 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6315 | TLI->getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6316 | } else { |
| 6317 | // Otherwise, create a stack slot and emit a store to it before the |
| 6318 | // asm. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6319 | Type *Ty = OpVal->getType(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6320 | uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty); |
| 6321 | unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6322 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 6323 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6324 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI->getPointerTy()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6325 | Chain = DAG.getStore(Chain, getCurSDLoc(), |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 6326 | OpInfo.CallOperand, StackSlot, |
| 6327 | MachinePointerInfo::getFixedStack(SSFI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6328 | false, false, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6329 | OpInfo.CallOperand = StackSlot; |
| 6330 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6331 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6332 | // There is no longer a Value* corresponding to this operand. |
| 6333 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6334 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6335 | // It is now an indirect operand. |
| 6336 | OpInfo.isIndirect = true; |
| 6337 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6338 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6339 | // If this constraint is for a specific register, allocate it before |
| 6340 | // anything else. |
| 6341 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6342 | GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6343 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6344 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6345 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 6346 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6347 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 6348 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6349 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6350 | // C_Register operands have already been allocated, Other/Memory don't need |
| 6351 | // to be. |
| 6352 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6353 | GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6356 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 6357 | std::vector<SDValue> AsmNodeOperands; |
| 6358 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 6359 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 6360 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6361 | TLI->getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6362 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6363 | // If we have a !srcloc metadata node associated with it, we want to attach |
| 6364 | // this to the ultimately generated inline asm machineinstr. To do this, we |
| 6365 | // pass in the third operand as this (potentially null) inline asm MDNode. |
| 6366 | const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc"); |
| 6367 | AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6368 | |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6369 | // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore |
| 6370 | // bits as operand 3. |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 6371 | unsigned ExtraInfo = 0; |
| 6372 | if (IA->hasSideEffects()) |
| 6373 | ExtraInfo |= InlineAsm::Extra_HasSideEffects; |
| 6374 | if (IA->isAlignStack()) |
| 6375 | ExtraInfo |= InlineAsm::Extra_IsAlignStack; |
Chad Rosier | 77fffa6 | 2012-09-05 22:17:43 +0000 | [diff] [blame] | 6376 | // Set the asm dialect. |
Chad Rosier | 2f1d815 | 2012-09-05 22:40:13 +0000 | [diff] [blame] | 6377 | ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect; |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6378 | |
| 6379 | // Determine if this InlineAsm MayLoad or MayStore based on the constraints. |
| 6380 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 6381 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
| 6382 | |
| 6383 | // Compute the constraint code and ConstraintType to use. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6384 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6385 | |
Chad Rosier | dfa4cec | 2012-10-30 20:01:12 +0000 | [diff] [blame] | 6386 | // Ideally, we would only check against memory constraints. However, the |
| 6387 | // meaning of an other constraint can be target-specific and we can't easily |
| 6388 | // reason about it. Therefore, be conservative and set MayLoad/MayStore |
| 6389 | // for other constriants as well. |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6390 | if (OpInfo.ConstraintType == TargetLowering::C_Memory || |
| 6391 | OpInfo.ConstraintType == TargetLowering::C_Other) { |
| 6392 | if (OpInfo.Type == InlineAsm::isInput) |
| 6393 | ExtraInfo |= InlineAsm::Extra_MayLoad; |
| 6394 | else if (OpInfo.Type == InlineAsm::isOutput) |
| 6395 | ExtraInfo |= InlineAsm::Extra_MayStore; |
Eric Christopher | fffe363 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 6396 | else if (OpInfo.Type == InlineAsm::isClobber) |
| 6397 | ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore); |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6398 | } |
| 6399 | } |
| 6400 | |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 6401 | AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6402 | TLI->getPointerTy())); |
Dale Johannesen | f1e309e | 2010-07-02 20:16:09 +0000 | [diff] [blame] | 6403 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6404 | // Loop over all of the inputs, copying the operand values into the |
| 6405 | // appropriate registers and processing the output regs. |
| 6406 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6407 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6408 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 6409 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6410 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6411 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 6412 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 6413 | |
| 6414 | switch (OpInfo.Type) { |
| 6415 | case InlineAsm::isOutput: { |
| 6416 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 6417 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 6418 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 6419 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 6420 | |
| 6421 | // Add information to the INLINEASM node to know about this output. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6422 | unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
| 6423 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6424 | TLI->getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6425 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 6426 | break; |
| 6427 | } |
| 6428 | |
| 6429 | // Otherwise, this is a register or register class output. |
| 6430 | |
| 6431 | // Copy the output from the appropriate register. Find a register that |
| 6432 | // we can use. |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6433 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 6434 | LLVMContext &Ctx = *DAG.getContext(); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6435 | Ctx.emitError(CS.getInstruction(), |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6436 | "couldn't allocate output register for constraint '" + |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6437 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6438 | return; |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6439 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6440 | |
| 6441 | // If this is an indirect operand, store through the pointer after the |
| 6442 | // asm. |
| 6443 | if (OpInfo.isIndirect) { |
| 6444 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 6445 | OpInfo.CallOperandVal)); |
| 6446 | } else { |
| 6447 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 6448 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6449 | // Concatenate this output onto the outputs list. |
| 6450 | RetValRegs.append(OpInfo.AssignedRegs); |
| 6451 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6452 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6453 | // Add information to the INLINEASM node to know that this register is |
| 6454 | // set. |
Eric Christopher | b0bee81 | 2013-07-30 22:50:44 +0000 | [diff] [blame] | 6455 | OpInfo.AssignedRegs |
| 6456 | .AddInlineAsmOperands(OpInfo.isEarlyClobber |
| 6457 | ? InlineAsm::Kind_RegDefEarlyClobber |
| 6458 | : InlineAsm::Kind_RegDef, |
| 6459 | false, 0, DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6460 | break; |
| 6461 | } |
| 6462 | case InlineAsm::isInput: { |
| 6463 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6464 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 6465 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6466 | // If this is required to match an output register we have already set, |
| 6467 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 6468 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6469 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6470 | // Scan until we find the definition we already emitted of this operand. |
| 6471 | // When we find it, create a RegsForValue operand. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6472 | unsigned CurOp = InlineAsm::Op_FirstOperand; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6473 | for (; OperandNo; --OperandNo) { |
| 6474 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6475 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6476 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6477 | assert((InlineAsm::isRegDefKind(OpFlag) || |
| 6478 | InlineAsm::isRegDefEarlyClobberKind(OpFlag) || |
| 6479 | InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6480 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6481 | } |
| 6482 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6483 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6484 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6485 | if (InlineAsm::isRegDefKind(OpFlag) || |
| 6486 | InlineAsm::isRegDefEarlyClobberKind(OpFlag)) { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6487 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6488 | if (OpInfo.isIndirect) { |
| 6489 | // This happens on gcc/testsuite/gcc.dg/pr8788-1.c |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 6490 | LLVMContext &Ctx = *DAG.getContext(); |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6491 | Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:" |
| 6492 | " don't know how to handle tied " |
| 6493 | "indirect register inputs"); |
| 6494 | return; |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6495 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6496 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6497 | RegsForValue MatchedRegs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6498 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 6499 | MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6500 | MatchedRegs.RegVTs.push_back(RegVT); |
| 6501 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6502 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6503 | i != e; ++i) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6504 | if (const TargetRegisterClass *RC = TLI->getRegClassFor(RegVT)) |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6505 | MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC)); |
| 6506 | else { |
| 6507 | LLVMContext &Ctx = *DAG.getContext(); |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6508 | Ctx.emitError(CS.getInstruction(), |
| 6509 | "inline asm error: This value" |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6510 | " type register class is not natively supported!"); |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6511 | return; |
Chad Rosier | 2871ba9 | 2013-04-24 22:53:10 +0000 | [diff] [blame] | 6512 | } |
| 6513 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6514 | // Use the produced MatchedRegs object to |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6515 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6516 | Chain, &Flag, CS.getInstruction()); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6517 | MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6518 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6519 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6520 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6521 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6522 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6523 | assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!"); |
| 6524 | assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 && |
| 6525 | "Unexpected number of operands"); |
| 6526 | // Add information to the INLINEASM node to know about this input. |
| 6527 | // See InlineAsm.h isUseOperandTiedToDef. |
| 6528 | OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag, |
| 6529 | OpInfo.getMatchedOperand()); |
| 6530 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6531 | TLI->getPointerTy())); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6532 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 6533 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6534 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6535 | |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6536 | // Treat indirect 'X' constraint as memory. |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6537 | if (OpInfo.ConstraintType == TargetLowering::C_Other && |
| 6538 | OpInfo.isIndirect) |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6539 | OpInfo.ConstraintType = TargetLowering::C_Memory; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6540 | |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6541 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6542 | std::vector<SDValue> Ops; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6543 | TLI->LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode, |
| 6544 | Ops, DAG); |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6545 | if (Ops.empty()) { |
| 6546 | LLVMContext &Ctx = *DAG.getContext(); |
| 6547 | Ctx.emitError(CS.getInstruction(), |
| 6548 | "invalid operand for inline asm constraint '" + |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6549 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6550 | return; |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6551 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6552 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6553 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6554 | unsigned ResOpType = |
| 6555 | InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6556 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6557 | TLI->getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6558 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 6559 | break; |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6560 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6561 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6562 | if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6563 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6564 | assert(InOperandVal.getValueType() == TLI->getPointerTy() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6565 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6566 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6567 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6568 | unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 6569 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6570 | TLI->getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6571 | AsmNodeOperands.push_back(InOperandVal); |
| 6572 | break; |
| 6573 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6574 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6575 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 6576 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 6577 | "Unknown constraint type!"); |
Eric Christopher | 9eb4f8a | 2012-07-02 21:16:43 +0000 | [diff] [blame] | 6578 | |
| 6579 | // TODO: Support this. |
| 6580 | if (OpInfo.isIndirect) { |
| 6581 | LLVMContext &Ctx = *DAG.getContext(); |
| 6582 | Ctx.emitError(CS.getInstruction(), |
| 6583 | "Don't know how to handle indirect register inputs yet " |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6584 | "for constraint '" + |
| 6585 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6586 | return; |
Eric Christopher | 9eb4f8a | 2012-07-02 21:16:43 +0000 | [diff] [blame] | 6587 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6588 | |
| 6589 | // Copy the input into the appropriate registers. |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6590 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 6591 | LLVMContext &Ctx = *DAG.getContext(); |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6592 | Ctx.emitError(CS.getInstruction(), |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6593 | "couldn't allocate input reg for constraint '" + |
Eric Christopher | 1a54c57 | 2013-07-31 01:26:24 +0000 | [diff] [blame] | 6594 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6595 | return; |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6596 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6597 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6598 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6599 | Chain, &Flag, CS.getInstruction()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6600 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6601 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6602 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6603 | break; |
| 6604 | } |
| 6605 | case InlineAsm::isClobber: { |
| 6606 | // Add the clobbered value to the operand list, so that the register |
| 6607 | // allocator is aware that the physreg got clobbered. |
| 6608 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Jakob Stoklund Olesen | f792fa9 | 2011-06-27 04:08:33 +0000 | [diff] [blame] | 6609 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6610 | false, 0, DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6611 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6612 | break; |
| 6613 | } |
| 6614 | } |
| 6615 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6616 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6617 | // 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] | 6618 | AsmNodeOperands[InlineAsm::Op_InputChain] = Chain; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6619 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6620 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6621 | Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(), |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 6622 | DAG.getVTList(MVT::Other, MVT::Glue), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6623 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 6624 | Flag = Chain.getValue(1); |
| 6625 | |
| 6626 | // If this asm returns a register value, copy the result from that register |
| 6627 | // and set it as the value of the call. |
| 6628 | if (!RetValRegs.Regs.empty()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6629 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6630 | Chain, &Flag, CS.getInstruction()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6631 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6632 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 6633 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6634 | EVT ResultType = TLI->getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6635 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6636 | // If any of the results of the inline asm is a vector, it may have the |
| 6637 | // wrong width/num elts. This can happen for register classes that can |
| 6638 | // contain multiple different value types. The preg or vreg allocated may |
| 6639 | // not have the same VT as was expected. Convert it to the right type |
| 6640 | // with bit_convert. |
| 6641 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6642 | Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 6643 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6644 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6645 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6646 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 6647 | // If a result value was tied to an input value, the computed result may |
| 6648 | // have a wider width than the expected result. Extract the relevant |
| 6649 | // portion. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6650 | Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6651 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6652 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6653 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 6654 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6655 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6656 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 6657 | // Don't need to use this as a chain in this case. |
| 6658 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 6659 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6660 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6661 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6662 | std::vector<std::pair<SDValue, const Value *> > StoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6663 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6664 | // Process indirect outputs, first output all of the flagged copies out of |
| 6665 | // physregs. |
| 6666 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 6667 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6668 | const Value *Ptr = IndirectStoresToEmit[i].second; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6669 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6670 | Chain, &Flag, IA); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6671 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
| 6672 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6673 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6674 | // Emit the non-flagged stores from the physregs. |
| 6675 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6676 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6677 | SDValue Val = DAG.getStore(Chain, getCurSDLoc(), |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6678 | StoresToEmit[i].first, |
| 6679 | getValue(StoresToEmit[i].second), |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 6680 | MachinePointerInfo(StoresToEmit[i].second), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6681 | false, false, 0); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6682 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6685 | if (!OutChains.empty()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6686 | Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6687 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6688 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6689 | DAG.setRoot(Chain); |
| 6690 | } |
| 6691 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6692 | void SelectionDAGBuilder::visitVAStart(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6693 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6694 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6695 | getValue(I.getArgOperand(0)), |
| 6696 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6697 | } |
| 6698 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6699 | void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6700 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 6701 | const DataLayout &TD = *TLI->getDataLayout(); |
| 6702 | SDValue V = DAG.getVAArg(TLI->getValueType(I.getType()), getCurSDLoc(), |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 6703 | getRoot(), getValue(I.getOperand(0)), |
Rafael Espindola | cbeeae2 | 2010-07-11 04:01:49 +0000 | [diff] [blame] | 6704 | DAG.getSrcValue(I.getOperand(0)), |
Rafael Espindola | 9d544d0 | 2010-07-12 18:11:17 +0000 | [diff] [blame] | 6705 | TD.getABITypeAlignment(I.getType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6706 | setValue(&I, V); |
| 6707 | DAG.setRoot(V.getValue(1)); |
| 6708 | } |
| 6709 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6710 | void SelectionDAGBuilder::visitVAEnd(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6711 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6712 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6713 | getValue(I.getArgOperand(0)), |
| 6714 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6715 | } |
| 6716 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6717 | void SelectionDAGBuilder::visitVACopy(const CallInst &I) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6718 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(), |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6719 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6720 | getValue(I.getArgOperand(0)), |
| 6721 | getValue(I.getArgOperand(1)), |
| 6722 | DAG.getSrcValue(I.getArgOperand(0)), |
| 6723 | DAG.getSrcValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6726 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6727 | /// implementation, which just calls LowerCall. |
| 6728 | /// FIXME: When all targets are |
| 6729 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6730 | std::pair<SDValue, SDValue> |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6731 | TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const { |
Stephen Lin | 3484da9 | 2013-04-30 22:49:28 +0000 | [diff] [blame] | 6732 | // Handle the incoming return values from the call. |
| 6733 | CLI.Ins.clear(); |
| 6734 | SmallVector<EVT, 4> RetTys; |
| 6735 | ComputeValueVTs(*this, CLI.RetTy, RetTys); |
| 6736 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 6737 | EVT VT = RetTys[I]; |
| 6738 | MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT); |
| 6739 | unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT); |
| 6740 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 6741 | ISD::InputArg MyFlags; |
| 6742 | MyFlags.VT = RegisterVT; |
| 6743 | MyFlags.Used = CLI.IsReturnValueUsed; |
| 6744 | if (CLI.RetSExt) |
| 6745 | MyFlags.Flags.setSExt(); |
| 6746 | if (CLI.RetZExt) |
| 6747 | MyFlags.Flags.setZExt(); |
| 6748 | if (CLI.IsInReg) |
| 6749 | MyFlags.Flags.setInReg(); |
| 6750 | CLI.Ins.push_back(MyFlags); |
| 6751 | } |
| 6752 | } |
| 6753 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6754 | // Handle all of the outgoing arguments. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6755 | CLI.Outs.clear(); |
| 6756 | CLI.OutVals.clear(); |
| 6757 | ArgListTy &Args = CLI.Args; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6758 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6759 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6760 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 6761 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 6762 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6763 | EVT VT = ValueVTs[Value]; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6764 | Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6765 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 6766 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6767 | ISD::ArgFlagsTy Flags; |
| 6768 | unsigned OriginalAlignment = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6769 | getDataLayout()->getABITypeAlignment(ArgTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6770 | |
| 6771 | if (Args[i].isZExt) |
| 6772 | Flags.setZExt(); |
| 6773 | if (Args[i].isSExt) |
| 6774 | Flags.setSExt(); |
| 6775 | if (Args[i].isInReg) |
| 6776 | Flags.setInReg(); |
| 6777 | if (Args[i].isSRet) |
| 6778 | Flags.setSRet(); |
| 6779 | if (Args[i].isByVal) { |
| 6780 | Flags.setByVal(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6781 | PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 6782 | Type *ElementTy = Ty->getElementType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6783 | Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6784 | // For ByVal, alignment should come from FE. BE will guess if this |
| 6785 | // info is not there but there are cases it cannot get right. |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6786 | unsigned FrameAlign; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6787 | if (Args[i].Alignment) |
| 6788 | FrameAlign = Args[i].Alignment; |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6789 | else |
| 6790 | FrameAlign = getByValTypeAlignment(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6791 | Flags.setByValAlign(FrameAlign); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6792 | } |
| 6793 | if (Args[i].isNest) |
| 6794 | Flags.setNest(); |
| 6795 | Flags.setOrigAlign(OriginalAlignment); |
| 6796 | |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6797 | MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6798 | unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6799 | SmallVector<SDValue, 4> Parts(NumParts); |
| 6800 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 6801 | |
| 6802 | if (Args[i].isSExt) |
| 6803 | ExtendKind = ISD::SIGN_EXTEND; |
| 6804 | else if (Args[i].isZExt) |
| 6805 | ExtendKind = ISD::ZERO_EXTEND; |
| 6806 | |
Stephen Lin | 3484da9 | 2013-04-30 22:49:28 +0000 | [diff] [blame] | 6807 | // Conservatively only handle 'returned' on non-vectors for now |
| 6808 | if (Args[i].isReturned && !Op.getValueType().isVector()) { |
| 6809 | assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues && |
| 6810 | "unexpected use of 'returned'"); |
| 6811 | // Before passing 'returned' to the target lowering code, ensure that |
| 6812 | // either the register MVT and the actual EVT are the same size or that |
| 6813 | // the return value and argument are extended in the same way; in these |
| 6814 | // cases it's safe to pass the argument register value unchanged as the |
| 6815 | // return register value (although it's at the target's option whether |
| 6816 | // to do so) |
| 6817 | // TODO: allow code generation to take advantage of partially preserved |
| 6818 | // registers rather than clobbering the entire register when the |
| 6819 | // parameter extension method is not compatible with the return |
| 6820 | // extension method |
| 6821 | if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) || |
| 6822 | (ExtendKind != ISD::ANY_EXTEND && |
| 6823 | CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt)) |
| 6824 | Flags.setReturned(); |
| 6825 | } |
| 6826 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6827 | getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6828 | PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6829 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6830 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6831 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 6832 | ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(), |
Manman Ren | 0a1544d | 2012-11-01 23:49:58 +0000 | [diff] [blame] | 6833 | i < CLI.NumFixedArgs, |
| 6834 | i, j*Parts[j].getValueType().getStoreSize()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6835 | if (NumParts > 1 && j == 0) |
| 6836 | MyFlags.Flags.setSplit(); |
| 6837 | else if (j != 0) |
| 6838 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6839 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6840 | CLI.Outs.push_back(MyFlags); |
| 6841 | CLI.OutVals.push_back(Parts[j]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6842 | } |
| 6843 | } |
| 6844 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6845 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6846 | SmallVector<SDValue, 4> InVals; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6847 | CLI.Chain = LowerCall(CLI, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6848 | |
| 6849 | // Verify that the target's LowerCall behaved as expected. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6850 | assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6851 | "LowerCall didn't return a valid chain!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6852 | assert((!CLI.IsTailCall || InVals.empty()) && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6853 | "LowerCall emitted a return value for a tail call!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6854 | assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6855 | "LowerCall didn't emit the correct number of values!"); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6856 | |
| 6857 | // For a tail call, the return value is merely live-out and there aren't |
| 6858 | // any nodes in the DAG representing it. Return a special value to |
| 6859 | // indicate that a tail call has been emitted and no more Instructions |
| 6860 | // should be processed in the current block. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6861 | if (CLI.IsTailCall) { |
| 6862 | CLI.DAG.setRoot(CLI.Chain); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6863 | return std::make_pair(SDValue(), SDValue()); |
| 6864 | } |
| 6865 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6866 | DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) { |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 6867 | assert(InVals[i].getNode() && |
| 6868 | "LowerCall emitted a null value!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6869 | assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() && |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 6870 | "LowerCall emitted a value with the wrong type!"); |
| 6871 | }); |
| 6872 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6873 | // Collect the legal value parts into potentially illegal values |
| 6874 | // that correspond to the original function's return values. |
| 6875 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6876 | if (CLI.RetSExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6877 | AssertOp = ISD::AssertSext; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6878 | else if (CLI.RetZExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6879 | AssertOp = ISD::AssertZext; |
| 6880 | SmallVector<SDValue, 4> ReturnValues; |
| 6881 | unsigned CurReg = 0; |
| 6882 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6883 | EVT VT = RetTys[I]; |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6884 | MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6885 | unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6886 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6887 | ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg], |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6888 | NumRegs, RegisterVT, VT, NULL, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 6889 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6890 | CurReg += NumRegs; |
| 6891 | } |
| 6892 | |
| 6893 | // For a function returning void, there is no return value. We can't create |
| 6894 | // 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] | 6895 | // that case, nothing will actually look at the value. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6896 | if (ReturnValues.empty()) |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6897 | return std::make_pair(SDValue(), CLI.Chain); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6898 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6899 | SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL, |
| 6900 | CLI.DAG.getVTList(&RetTys[0], RetTys.size()), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6901 | &ReturnValues[0], ReturnValues.size()); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6902 | return std::make_pair(Res, CLI.Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6903 | } |
| 6904 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 6905 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 6906 | SmallVectorImpl<SDValue> &Results, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6907 | SelectionDAG &DAG) const { |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 6908 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 6909 | if (Res.getNode()) |
| 6910 | Results.push_back(Res); |
| 6911 | } |
| 6912 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6913 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6914 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6915 | } |
| 6916 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6917 | void |
| 6918 | SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) { |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 6919 | SDValue Op = getNonRegisterValue(V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6920 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 6921 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 6922 | "Copy from a reg to the same reg!"); |
| 6923 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 6924 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6925 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 6926 | RegsForValue RFV(V->getContext(), *TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6927 | SDValue Chain = DAG.getEntryNode(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6928 | RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6929 | PendingExports.push_back(Chain); |
| 6930 | } |
| 6931 | |
| 6932 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 6933 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6934 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 6935 | /// entry block, return true. This includes arguments used by switches, since |
| 6936 | /// the switch may expand into multiple basic blocks. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6937 | static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6938 | // With FastISel active, we may be splitting blocks, so force creation |
| 6939 | // of virtual registers for all non-dead arguments. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6940 | if (FastISel) |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6941 | return A->use_empty(); |
| 6942 | |
| 6943 | const BasicBlock *Entry = A->getParent()->begin(); |
| 6944 | for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end(); |
| 6945 | UI != E; ++UI) { |
| 6946 | const User *U = *UI; |
| 6947 | if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U)) |
| 6948 | return false; // Use not in entry block. |
| 6949 | } |
| 6950 | return true; |
| 6951 | } |
| 6952 | |
Eli Bendersky | 6437d38 | 2013-02-28 23:09:18 +0000 | [diff] [blame] | 6953 | void SelectionDAGISel::LowerArguments(const Function &F) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6954 | SelectionDAG &DAG = SDB->DAG; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6955 | SDLoc dl = SDB->getCurSDLoc(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6956 | const TargetLowering *TLI = getTargetLowering(); |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6957 | const DataLayout *TD = TLI->getDataLayout(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6958 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6959 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6960 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6961 | // Put in an sret pointer parameter before all the other parameters. |
| 6962 | SmallVector<EVT, 1> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6963 | ComputeValueVTs(*getTargetLowering(), |
| 6964 | PointerType::getUnqual(F.getReturnType()), ValueVTs); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6965 | |
| 6966 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 6967 | // or one register. |
| 6968 | ISD::ArgFlagsTy Flags; |
| 6969 | Flags.setSRet(); |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6970 | MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]); |
Stepan Dyatkovskiy | 661afe7 | 2012-10-10 11:37:36 +0000 | [diff] [blame] | 6971 | ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6972 | Ins.push_back(RetArg); |
| 6973 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 6974 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6975 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6976 | unsigned Idx = 1; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6977 | 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] | 6978 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6979 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 6980 | ComputeValueVTs(*TLI, I->getType(), ValueVTs); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6981 | bool isArgValueUsed = !I->use_empty(); |
| 6982 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 6983 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6984 | EVT VT = ValueVTs[Value]; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6985 | Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6986 | ISD::ArgFlagsTy Flags; |
| 6987 | unsigned OriginalAlignment = |
| 6988 | TD->getABITypeAlignment(ArgTy); |
| 6989 | |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6990 | if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6991 | Flags.setZExt(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6992 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6993 | Flags.setSExt(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6994 | if (F.getAttributes().hasAttribute(Idx, Attribute::InReg)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6995 | Flags.setInReg(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6996 | if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6997 | Flags.setSRet(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6998 | if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6999 | Flags.setByVal(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 7000 | PointerType *Ty = cast<PointerType>(I->getType()); |
| 7001 | Type *ElementTy = Ty->getElementType(); |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 7002 | Flags.setByValSize(TD->getTypeAllocSize(ElementTy)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7003 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 7004 | // 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] | 7005 | unsigned FrameAlign; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7006 | if (F.getParamAlignment(Idx)) |
| 7007 | FrameAlign = F.getParamAlignment(Idx); |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 7008 | else |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 7009 | FrameAlign = TLI->getByValTypeAlignment(ElementTy); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7010 | Flags.setByValAlign(FrameAlign); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7011 | } |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 7012 | if (F.getAttributes().hasAttribute(Idx, Attribute::Nest)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7013 | Flags.setNest(); |
| 7014 | Flags.setOrigAlign(OriginalAlignment); |
| 7015 | |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 7016 | MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT); |
| 7017 | unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7018 | for (unsigned i = 0; i != NumRegs; ++i) { |
Stepan Dyatkovskiy | 661afe7 | 2012-10-10 11:37:36 +0000 | [diff] [blame] | 7019 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed, |
| 7020 | Idx-1, i*RegisterVT.getStoreSize()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7021 | if (NumRegs > 1 && i == 0) |
| 7022 | MyFlags.Flags.setSplit(); |
| 7023 | // if it isn't first piece, alignment must be 1 |
| 7024 | else if (i > 0) |
| 7025 | MyFlags.Flags.setOrigAlign(1); |
| 7026 | Ins.push_back(MyFlags); |
| 7027 | } |
| 7028 | } |
| 7029 | } |
| 7030 | |
| 7031 | // Call the target to set up the argument values. |
| 7032 | SmallVector<SDValue, 8> InVals; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 7033 | SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 7034 | F.isVarArg(), Ins, |
| 7035 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 7036 | |
| 7037 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7038 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 7039 | "LowerFormalArguments didn't return a valid chain!"); |
| 7040 | assert(InVals.size() == Ins.size() && |
| 7041 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 7042 | DEBUG({ |
| 7043 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 7044 | assert(InVals[i].getNode() && |
| 7045 | "LowerFormalArguments emitted a null value!"); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 7046 | assert(EVT(Ins[i].VT) == InVals[i].getValueType() && |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 7047 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 7048 | } |
| 7049 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 7050 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 7051 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7052 | DAG.setRoot(NewRoot); |
| 7053 | |
| 7054 | // Set up the argument values. |
| 7055 | unsigned i = 0; |
| 7056 | Idx = 1; |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 7057 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 7058 | // Create a virtual register for the sret pointer, and put in a copy |
| 7059 | // from the sret argument into it. |
| 7060 | SmallVector<EVT, 1> ValueVTs; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 7061 | ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 7062 | MVT VT = ValueVTs[0].getSimpleVT(); |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 7063 | MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 7064 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 7065 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 7066 | RegVT, VT, NULL, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 7067 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 7068 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 7069 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 7070 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT)); |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 7071 | FuncInfo->DemoteRegister = SRetReg; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7072 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 7073 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 7074 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 7075 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 7076 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 7077 | // Idx indexes LLVM arguments. Don't touch it. |
| 7078 | ++i; |
| 7079 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 7080 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7081 | 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] | 7082 | ++I, ++Idx) { |
| 7083 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7084 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 7085 | ComputeValueVTs(*TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7086 | unsigned NumValues = ValueVTs.size(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 7087 | |
| 7088 | // If this argument is unused then remember its value. It is used to generate |
| 7089 | // debugging information. |
Adrian Prantl | df68803 | 2013-05-16 23:44:12 +0000 | [diff] [blame] | 7090 | if (I->use_empty() && NumValues) { |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 7091 | SDB->setUnusedArgValue(I, InVals[i]); |
| 7092 | |
Adrian Prantl | df68803 | 2013-05-16 23:44:12 +0000 | [diff] [blame] | 7093 | // Also remember any frame index for use in FastISel. |
| 7094 | if (FrameIndexSDNode *FI = |
| 7095 | dyn_cast<FrameIndexSDNode>(InVals[i].getNode())) |
| 7096 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
| 7097 | } |
| 7098 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 7099 | for (unsigned Val = 0; Val != NumValues; ++Val) { |
| 7100 | EVT VT = ValueVTs[Val]; |
Bill Wendling | 6a2e7ac | 2013-06-06 00:43:09 +0000 | [diff] [blame] | 7101 | MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT); |
| 7102 | unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7103 | |
| 7104 | if (!I->use_empty()) { |
| 7105 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 7106 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7107 | AssertOp = ISD::AssertSext; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 7108 | else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7109 | AssertOp = ISD::AssertZext; |
| 7110 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 7111 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 7112 | NumParts, PartVT, VT, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 7113 | NULL, AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7114 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 7115 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7116 | i += NumParts; |
| 7117 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 7118 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 7119 | // We don't need to do anything else for unused arguments. |
| 7120 | if (ArgValues.empty()) |
| 7121 | continue; |
| 7122 | |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 7123 | // Note down frame index. |
| 7124 | if (FrameIndexSDNode *FI = |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 7125 | dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode())) |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 7126 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 7127 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 7128 | SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7129 | SDB->getCurSDLoc()); |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 7130 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 7131 | SDB->setValue(I, Res); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 7132 | if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) { |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 7133 | if (LoadSDNode *LNode = |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 7134 | dyn_cast<LoadSDNode>(Res.getOperand(0).getNode())) |
| 7135 | if (FrameIndexSDNode *FI = |
| 7136 | dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) |
| 7137 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
| 7138 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 7139 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 7140 | // If this argument is live outside of the entry block, insert a copy from |
| 7141 | // 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] | 7142 | if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 7143 | // If we can, though, try to skip creating an unnecessary vreg. |
| 7144 | // 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] | 7145 | // general. It's also subtly incompatible with the hacks FastISel |
| 7146 | // uses with vregs. |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 7147 | unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg(); |
| 7148 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 7149 | FuncInfo->ValueMap[I] = Reg; |
| 7150 | continue; |
| 7151 | } |
| 7152 | } |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 7153 | if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 7154 | FuncInfo->InitializeRegForValue(I); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 7155 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7156 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7157 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 7158 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 7159 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7160 | |
| 7161 | // Finally, if the target has anything special to do, allow it to do so. |
| 7162 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 7163 | EmitFunctionEntryCode(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7164 | } |
| 7165 | |
| 7166 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 7167 | /// ensure constants are generated when needed. Remember the virtual registers |
| 7168 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 7169 | /// directly add them, because expansion might result in multiple MBB's for one |
| 7170 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 7171 | /// the end. |
| 7172 | /// |
| 7173 | void |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 7174 | SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7175 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7176 | |
| 7177 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 7178 | |
| 7179 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 7180 | // from this block. |
| 7181 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7182 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7183 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 7184 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 7185 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7186 | // If this terminator has multiple identical successors (common for |
| 7187 | // switches), only handle each succ once. |
| 7188 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 7189 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7190 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7191 | |
| 7192 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 7193 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 7194 | // emitted yet. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7195 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 7196 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7197 | // Ignore dead phi's. |
| 7198 | if (PN->use_empty()) continue; |
| 7199 | |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 7200 | // Skip empty types |
| 7201 | if (PN->getType()->isEmptyTy()) |
| 7202 | continue; |
| 7203 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7204 | unsigned Reg; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7205 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7206 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7207 | if (const Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 7208 | unsigned &RegOut = ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7209 | if (RegOut == 0) { |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 7210 | RegOut = FuncInfo.CreateRegs(C->getType()); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 7211 | CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7212 | } |
| 7213 | Reg = RegOut; |
| 7214 | } else { |
Dan Gohman | c25ad63 | 2010-07-01 01:33:21 +0000 | [diff] [blame] | 7215 | DenseMap<const Value *, unsigned>::iterator I = |
| 7216 | FuncInfo.ValueMap.find(PHIOp); |
| 7217 | if (I != FuncInfo.ValueMap.end()) |
| 7218 | Reg = I->second; |
| 7219 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7220 | assert(isa<AllocaInst>(PHIOp) && |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 7221 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7222 | "Didn't codegen value into a register!??"); |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 7223 | Reg = FuncInfo.CreateRegs(PHIOp->getType()); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 7224 | CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7225 | } |
| 7226 | } |
| 7227 | |
| 7228 | // Remember that this register needs to added to the machine PHI node as |
| 7229 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7230 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 7231 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 7232 | ComputeValueVTs(*TLI, PN->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7233 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7234 | EVT VT = ValueVTs[vti]; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 7235 | unsigned NumRegisters = TLI->getNumRegisters(*DAG.getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7236 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 7237 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 7238 | Reg += NumRegisters; |
| 7239 | } |
| 7240 | } |
| 7241 | } |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 7242 | |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 7243 | ConstantsOut.clear(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 7244 | } |
Michael Gottesman | 657484f | 2013-08-20 07:00:16 +0000 | [diff] [blame] | 7245 | |
| 7246 | /// Add a successor MBB to ParentMBB< creating a new MachineBB for BB if SuccMBB |
| 7247 | /// is 0. |
| 7248 | MachineBasicBlock * |
| 7249 | SelectionDAGBuilder::StackProtectorDescriptor:: |
| 7250 | AddSuccessorMBB(const BasicBlock *BB, |
| 7251 | MachineBasicBlock *ParentMBB, |
| 7252 | MachineBasicBlock *SuccMBB) { |
| 7253 | // If SuccBB has not been created yet, create it. |
| 7254 | if (!SuccMBB) { |
| 7255 | MachineFunction *MF = ParentMBB->getParent(); |
| 7256 | MachineFunction::iterator BBI = ParentMBB; |
| 7257 | SuccMBB = MF->CreateMachineBasicBlock(BB); |
| 7258 | MF->insert(++BBI, SuccMBB); |
| 7259 | } |
| 7260 | // Add it as a successor of ParentMBB. |
| 7261 | ParentMBB->addSuccessor(SuccMBB); |
| 7262 | return SuccMBB; |
| 7263 | } |