Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements routines for translating from LLVM IR into SelectionDAG IR. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 15 | #include "SelectionDAGBuilder.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "SDNodeDbgValue.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Jakub Staszak | 81bfd71 | 2013-01-10 22:13:13 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ConstantFolding.h" |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Analysis.h" |
| 24 | #include "llvm/CodeGen/FastISel.h" |
| 25 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| 26 | #include "llvm/CodeGen/GCMetadata.h" |
| 27 | #include "llvm/CodeGen/GCStrategy.h" |
| 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 29 | #include "llvm/CodeGen/MachineFunction.h" |
| 30 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 31 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 34 | #include "llvm/CodeGen/SelectionDAG.h" |
Bill Wendling | 0bcbd1d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/CallingConv.h" |
| 37 | #include "llvm/IR/Constants.h" |
| 38 | #include "llvm/IR/DataLayout.h" |
| 39 | #include "llvm/IR/DerivedTypes.h" |
| 40 | #include "llvm/IR/Function.h" |
| 41 | #include "llvm/IR/GlobalVariable.h" |
| 42 | #include "llvm/IR/InlineAsm.h" |
| 43 | #include "llvm/IR/Instructions.h" |
| 44 | #include "llvm/IR/IntrinsicInst.h" |
| 45 | #include "llvm/IR/Intrinsics.h" |
| 46 | #include "llvm/IR/LLVMContext.h" |
| 47 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 48 | #include "llvm/Support/CommandLine.h" |
| 49 | #include "llvm/Support/Debug.h" |
| 50 | #include "llvm/Support/ErrorHandling.h" |
| 51 | #include "llvm/Support/IntegersSubsetMapping.h" |
| 52 | #include "llvm/Support/MathExtras.h" |
| 53 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetFrameLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetInstrInfo.h" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetIntrinsicInfo.h" |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 57 | #include "llvm/Target/TargetLibraryInfo.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 58 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 59 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 60 | #include <algorithm> |
| 61 | using namespace llvm; |
| 62 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 63 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 64 | /// some float libcalls (6, 8 or 12 bits). |
| 65 | static unsigned LimitFloatPrecision; |
| 66 | |
| 67 | static cl::opt<unsigned, true> |
| 68 | LimitFPPrecision("limit-float-precision", |
| 69 | cl::desc("Generate low-precision inline sequences " |
| 70 | "for some float libcalls"), |
| 71 | cl::location(LimitFloatPrecision), |
| 72 | cl::init(0)); |
| 73 | |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 74 | // Limit the width of DAG chains. This is important in general to prevent |
| 75 | // prevent DAG-based analysis from blowing up. For example, alias analysis and |
| 76 | // load clustering may not complete in reasonable time. It is difficult to |
| 77 | // recognize and avoid this situation within each individual analysis, and |
| 78 | // future analyses are likely to have the same behavior. Limiting DAG width is |
Andrew Trick | b9e6fe1 | 2010-11-20 07:26:51 +0000 | [diff] [blame] | 79 | // the safe approach, and will be especially important with global DAGs. |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 80 | // |
| 81 | // MaxParallelChains default is arbitrarily high to avoid affecting |
| 82 | // optimization, but could be lowered to improve compile time. Any ld-ld-st-st |
Andrew Trick | b9e6fe1 | 2010-11-20 07:26:51 +0000 | [diff] [blame] | 83 | // sequence over this should have been converted to llvm.memcpy by the |
| 84 | // frontend. It easy to induce this behavior with .ll code such as: |
| 85 | // %buffer = alloca [4096 x i8] |
| 86 | // %data = load [4096 x i8]* %argPtr |
| 87 | // store [4096 x i8] %data, [4096 x i8]* %buffer |
Andrew Trick | 778583a | 2011-03-11 17:46:59 +0000 | [diff] [blame] | 88 | static const unsigned MaxParallelChains = 64; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 90 | static SDValue getCopyFromPartsVector(SelectionDAG &DAG, DebugLoc DL, |
| 91 | const SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 92 | MVT PartVT, EVT ValueVT, const Value *V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 93 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 94 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 95 | /// combined into the value they represent. If the parts combine to a type |
| 96 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 97 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 98 | /// (ISD::AssertSext). |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 99 | static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc DL, |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 100 | const SDValue *Parts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 101 | unsigned NumParts, MVT PartVT, EVT ValueVT, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 102 | const Value *V, |
Duncan Sands | 0b3aa26 | 2009-01-28 14:42:54 +0000 | [diff] [blame] | 103 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 104 | if (ValueVT.isVector()) |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 105 | return getCopyFromPartsVector(DAG, DL, Parts, NumParts, |
| 106 | PartVT, ValueVT, V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 107 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 108 | assert(NumParts > 0 && "No parts to assemble!"); |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 109 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 110 | SDValue Val = Parts[0]; |
| 111 | |
| 112 | if (NumParts > 1) { |
| 113 | // Assemble the value from multiple parts. |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 114 | if (ValueVT.isInteger()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 115 | unsigned PartBits = PartVT.getSizeInBits(); |
| 116 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 117 | |
| 118 | // Assemble the power of 2 part. |
| 119 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 120 | 1 << Log2_32(NumParts) : NumParts; |
| 121 | unsigned RoundBits = PartBits * RoundParts; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 122 | EVT RoundVT = RoundBits == ValueBits ? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 123 | ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 124 | SDValue Lo, Hi; |
| 125 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 126 | EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 127 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 128 | if (RoundParts > 2) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 129 | Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 130 | PartVT, HalfVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 131 | Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 132 | RoundParts / 2, PartVT, HalfVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 133 | } else { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 134 | Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]); |
| 135 | Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 136 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 137 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 138 | if (TLI.isBigEndian()) |
| 139 | std::swap(Lo, Hi); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 141 | Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 142 | |
| 143 | if (RoundParts < NumParts) { |
| 144 | // Assemble the trailing non-power-of-2 part. |
| 145 | unsigned OddParts = NumParts - RoundParts; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 146 | EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 147 | Hi = getCopyFromParts(DAG, DL, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 148 | Parts + RoundParts, OddParts, PartVT, OddVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 149 | |
| 150 | // Combine the round and odd parts. |
| 151 | Lo = Val; |
| 152 | if (TLI.isBigEndian()) |
| 153 | std::swap(Lo, Hi); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 154 | EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 155 | Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi); |
| 156 | Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 157 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 158 | TLI.getPointerTy())); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 159 | Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo); |
| 160 | Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 161 | } |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 162 | } else if (PartVT.isFloatingPoint()) { |
| 163 | // FP split into multiple FP parts (for ppcf128) |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 164 | assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 && |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 165 | "Unexpected split"); |
| 166 | SDValue Lo, Hi; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 167 | Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]); |
| 168 | Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 169 | if (TLI.isBigEndian()) |
| 170 | std::swap(Lo, Hi); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 171 | Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi); |
Eli Friedman | 2ac8b32 | 2009-05-20 06:02:09 +0000 | [diff] [blame] | 172 | } else { |
| 173 | // FP split into integer parts (soft fp) |
| 174 | assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && |
| 175 | !PartVT.isVector() && "Unexpected split"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 176 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 177 | Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | // There is now one part, held in Val. Correct it to match ValueVT. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 182 | EVT PartEVT = Val.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 183 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 184 | if (PartEVT == ValueVT) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 185 | return Val; |
| 186 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 187 | if (PartEVT.isInteger() && ValueVT.isInteger()) { |
| 188 | if (ValueVT.bitsLT(PartEVT)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 189 | // For a truncate, see if we have any information to |
| 190 | // indicate whether the truncated bits will always be |
| 191 | // zero or sign-extension. |
| 192 | if (AssertOp != ISD::DELETED_NODE) |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 193 | Val = DAG.getNode(AssertOp, DL, PartEVT, Val, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 194 | DAG.getValueType(ValueVT)); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 195 | return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 197 | return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 200 | if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 201 | // FP_ROUND's are always exact here. |
| 202 | if (ValueVT.bitsLT(Val.getValueType())) |
| 203 | return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, |
Pete Cooper | f57e1c2 | 2012-01-17 01:54:07 +0000 | [diff] [blame] | 204 | DAG.getTargetConstant(1, TLI.getPointerTy())); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 206 | return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 209 | if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits()) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 210 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 211 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 212 | llvm_unreachable("Unknown mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 215 | /// getCopyFromPartsVector - Create a value that contains the specified legal |
| 216 | /// parts combined into the value they represent. If the parts combine to a |
| 217 | /// type larger then ValueVT then AssertOp can be used to specify whether the |
| 218 | /// extra bits are known to be zero (ISD::AssertZext) or sign extended from |
| 219 | /// ValueVT (ISD::AssertSext). |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 220 | static SDValue getCopyFromPartsVector(SelectionDAG &DAG, DebugLoc DL, |
| 221 | const SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 222 | MVT PartVT, EVT ValueVT, const Value *V) { |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 223 | assert(ValueVT.isVector() && "Not a vector value"); |
| 224 | assert(NumParts > 0 && "No parts to assemble!"); |
| 225 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 226 | SDValue Val = Parts[0]; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 228 | // Handle a multi-element vector. |
| 229 | if (NumParts > 1) { |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 230 | EVT IntermediateVT; |
| 231 | MVT RegisterVT; |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 232 | unsigned NumIntermediates; |
| 233 | unsigned NumRegs = |
| 234 | TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, |
| 235 | NumIntermediates, RegisterVT); |
| 236 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 237 | NumParts = NumRegs; // Silence a compiler warning. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 238 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 239 | assert(RegisterVT == Parts[0].getSimpleValueType() && |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 240 | "Part type doesn't match part!"); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 242 | // Assemble the parts into intermediate operands. |
| 243 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 244 | if (NumIntermediates == NumParts) { |
| 245 | // If the register was not expanded, truncate or copy the value, |
| 246 | // as appropriate. |
| 247 | for (unsigned i = 0; i != NumParts; ++i) |
| 248 | Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 249 | PartVT, IntermediateVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 250 | } else if (NumParts > 0) { |
| 251 | // If the intermediate type was expanded, build the intermediate |
| 252 | // operands from the parts. |
| 253 | assert(NumParts % NumIntermediates == 0 && |
| 254 | "Must expand into a divisible number of parts!"); |
| 255 | unsigned Factor = NumParts / NumIntermediates; |
| 256 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 257 | Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 258 | PartVT, IntermediateVT, V); |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 259 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 261 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the |
| 262 | // intermediate operands. |
| 263 | Val = DAG.getNode(IntermediateVT.isVector() ? |
| 264 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL, |
| 265 | ValueVT, &Ops[0], NumIntermediates); |
| 266 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 268 | // There is now one part, held in Val. Correct it to match ValueVT. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 269 | EVT PartEVT = Val.getValueType(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 270 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 271 | if (PartEVT == ValueVT) |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 272 | return Val; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 273 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 274 | if (PartEVT.isVector()) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 275 | // If the element type of the source/dest vectors are the same, but the |
| 276 | // parts vector has more elements than the value vector, then we have a |
| 277 | // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the |
| 278 | // elements we want. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 279 | if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) { |
| 280 | assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() && |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 281 | "Cannot narrow, it would be a lossy transformation"); |
| 282 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val, |
| 283 | DAG.getIntPtrConstant(0)); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 286 | // Vector/Vector bitcast. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 287 | if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 288 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
| 289 | |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 290 | assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() && |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 291 | "Cannot handle this kind of promotion"); |
| 292 | // Promoted vector extract |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 293 | bool Smaller = ValueVT.bitsLE(PartEVT); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 294 | return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 295 | DL, ValueVT, Val); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 296 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 297 | } |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 298 | |
Eric Christopher | 9aaa02a | 2011-06-01 19:55:10 +0000 | [diff] [blame] | 299 | // Trivial bitcast if the types are the same size and the destination |
| 300 | // vector type is legal. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 301 | if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() && |
Eric Christopher | 9aaa02a | 2011-06-01 19:55:10 +0000 | [diff] [blame] | 302 | TLI.isTypeLegal(ValueVT)) |
| 303 | return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 304 | |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 305 | // Handle cases such as i8 -> <1 x i1> |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 306 | if (ValueVT.getVectorNumElements() != 1) { |
| 307 | LLVMContext &Ctx = *DAG.getContext(); |
| 308 | Twine ErrMsg("non-trivial scalar-to-vector conversion"); |
| 309 | if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 310 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 311 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 312 | ErrMsg = ErrMsg + ", possible invalid constraint for vector type"; |
| 313 | Ctx.emitError(I, ErrMsg); |
| 314 | } else { |
| 315 | Ctx.emitError(ErrMsg); |
| 316 | } |
| 317 | report_fatal_error("Cannot handle scalar-to-vector conversion!"); |
| 318 | } |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 319 | |
| 320 | if (ValueVT.getVectorNumElements() == 1 && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 321 | ValueVT.getVectorElementType() != PartEVT) { |
| 322 | bool Smaller = ValueVT.bitsLE(PartEVT); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 323 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 324 | DL, ValueVT.getScalarType(), Val); |
| 325 | } |
| 326 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 327 | return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val); |
| 328 | } |
| 329 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 330 | static void getCopyToPartsVector(SelectionDAG &DAG, DebugLoc dl, |
| 331 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 332 | MVT PartVT, const Value *V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 333 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 334 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 335 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 336 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 337 | static void getCopyToParts(SelectionDAG &DAG, DebugLoc DL, |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 338 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 339 | MVT PartVT, const Value *V, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 340 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 341 | EVT ValueVT = Val.getValueType(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 342 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 343 | // Handle the vector case separately. |
| 344 | if (ValueVT.isVector()) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 345 | return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 346 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 347 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 348 | unsigned PartBits = PartVT.getSizeInBits(); |
Dale Johannesen | 8a36f50 | 2009-02-25 22:39:13 +0000 | [diff] [blame] | 349 | unsigned OrigNumParts = NumParts; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 350 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 351 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 352 | if (NumParts == 0) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 353 | return; |
| 354 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 355 | assert(!ValueVT.isVector() && "Vector case handled elsewhere"); |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 356 | EVT PartEVT = PartVT; |
| 357 | if (PartEVT == ValueVT) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 358 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 359 | Parts[0] = Val; |
| 360 | return; |
| 361 | } |
| 362 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 363 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 364 | // If the parts cover more bits than the value has, promote the value. |
| 365 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 366 | assert(NumParts == 1 && "Do not know what to promote to!"); |
| 367 | Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val); |
| 368 | } else { |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 369 | assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && |
| 370 | ValueVT.isInteger() && |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 371 | "Unknown mismatch!"); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 372 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 373 | Val = DAG.getNode(ExtendKind, DL, ValueVT, Val); |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 374 | if (PartVT == MVT::x86mmx) |
| 375 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 376 | } |
| 377 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 378 | // Different types of the same size. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 379 | assert(NumParts == 1 && PartEVT != ValueVT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 380 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 381 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 382 | // If the parts cover less bits than value has, truncate the value. |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 383 | assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && |
| 384 | ValueVT.isInteger() && |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 385 | "Unknown mismatch!"); |
| 386 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 387 | Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
Bill Wendling | 9e8ceb0 | 2012-02-23 23:25:25 +0000 | [diff] [blame] | 388 | if (PartVT == MVT::x86mmx) |
| 389 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // The value may have changed - recompute ValueVT. |
| 393 | ValueVT = Val.getValueType(); |
| 394 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 395 | "Failed to tile the value with PartVT!"); |
| 396 | |
| 397 | if (NumParts == 1) { |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 398 | if (PartEVT != ValueVT) { |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 399 | LLVMContext &Ctx = *DAG.getContext(); |
| 400 | Twine ErrMsg("scalar-to-vector conversion failed"); |
| 401 | if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) { |
| 402 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 403 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 404 | ErrMsg = ErrMsg + ", possible invalid constraint for vector type"; |
| 405 | Ctx.emitError(I, ErrMsg); |
| 406 | } else { |
| 407 | Ctx.emitError(ErrMsg); |
| 408 | } |
| 409 | } |
| 410 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 411 | Parts[0] = Val; |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | // Expand the value into multiple parts. |
| 416 | if (NumParts & (NumParts - 1)) { |
| 417 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 418 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 419 | "Do not know what to expand to!"); |
| 420 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 421 | unsigned RoundBits = RoundParts * PartBits; |
| 422 | unsigned OddParts = NumParts - RoundParts; |
| 423 | SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val, |
| 424 | DAG.getIntPtrConstant(RoundBits)); |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 425 | getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 426 | |
| 427 | if (TLI.isBigEndian()) |
| 428 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 429 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 430 | |
| 431 | NumParts = RoundParts; |
| 432 | ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); |
| 433 | Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); |
| 434 | } |
| 435 | |
| 436 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 437 | // EXTRACT_ELEMENT. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 438 | Parts[0] = DAG.getNode(ISD::BITCAST, DL, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 439 | EVT::getIntegerVT(*DAG.getContext(), |
| 440 | ValueVT.getSizeInBits()), |
| 441 | Val); |
| 442 | |
| 443 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 444 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 445 | unsigned ThisBits = StepSize * PartBits / 2; |
| 446 | EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); |
| 447 | SDValue &Part0 = Parts[i]; |
| 448 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 449 | |
| 450 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, |
| 451 | ThisVT, Part0, DAG.getIntPtrConstant(1)); |
| 452 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, |
| 453 | ThisVT, Part0, DAG.getIntPtrConstant(0)); |
| 454 | |
| 455 | if (ThisBits == PartBits && ThisVT != PartVT) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 456 | Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0); |
| 457 | Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (TLI.isBigEndian()) |
| 463 | std::reverse(Parts, Parts + OrigNumParts); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | /// getCopyToPartsVector - Create a series of nodes that contain the specified |
| 468 | /// value split into legal parts. |
| 469 | static void getCopyToPartsVector(SelectionDAG &DAG, DebugLoc DL, |
| 470 | SDValue Val, SDValue *Parts, unsigned NumParts, |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 471 | MVT PartVT, const Value *V) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 472 | EVT ValueVT = Val.getValueType(); |
| 473 | assert(ValueVT.isVector() && "Not a vector"); |
| 474 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 475 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 476 | if (NumParts == 1) { |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 477 | EVT PartEVT = PartVT; |
| 478 | if (PartEVT == ValueVT) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 479 | // Nothing to do. |
| 480 | } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { |
| 481 | // Bitconvert vector->vector case. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 482 | Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 483 | } else if (PartVT.isVector() && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 484 | PartEVT.getVectorElementType() == ValueVT.getVectorElementType() && |
| 485 | PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) { |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 486 | EVT ElementVT = PartVT.getVectorElementType(); |
| 487 | // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in |
| 488 | // undef elements. |
| 489 | SmallVector<SDValue, 16> Ops; |
| 490 | for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i) |
| 491 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
| 492 | ElementVT, Val, DAG.getIntPtrConstant(i))); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 493 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 494 | for (unsigned i = ValueVT.getVectorNumElements(), |
| 495 | e = PartVT.getVectorNumElements(); i != e; ++i) |
| 496 | Ops.push_back(DAG.getUNDEF(ElementVT)); |
| 497 | |
| 498 | Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size()); |
| 499 | |
| 500 | // FIXME: Use CONCAT for 2x -> 4x. |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 501 | |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 502 | //SDValue UndefElts = DAG.getUNDEF(VectorTy); |
| 503 | //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 504 | } else if (PartVT.isVector() && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 505 | PartEVT.getVectorElementType().bitsGE( |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 506 | ValueVT.getVectorElementType()) && |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 507 | PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) { |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 508 | |
| 509 | // Promoted vector extract |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 510 | bool Smaller = PartEVT.bitsLE(ValueVT); |
Nadav Rotem | c6341e6 | 2011-06-19 08:49:38 +0000 | [diff] [blame] | 511 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 512 | DL, PartVT, Val); |
Nadav Rotem | 0b66636 | 2011-06-04 20:58:08 +0000 | [diff] [blame] | 513 | } else{ |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 514 | // Vector -> scalar conversion. |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 515 | assert(ValueVT.getVectorNumElements() == 1 && |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 516 | "Only trivial vector-to-scalar conversions should get here!"); |
| 517 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
| 518 | PartVT, Val, DAG.getIntPtrConstant(0)); |
Nadav Rotem | b05f14b | 2011-06-12 14:49:38 +0000 | [diff] [blame] | 519 | |
| 520 | bool Smaller = ValueVT.bitsLE(PartVT); |
| 521 | Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND), |
| 522 | DL, PartVT, Val); |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 523 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 524 | |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 525 | Parts[0] = Val; |
| 526 | return; |
| 527 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 528 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 529 | // Handle a multi-element vector. |
Patrik Hagglund | ee211d2 | 2012-12-19 11:53:21 +0000 | [diff] [blame] | 530 | EVT IntermediateVT; |
| 531 | MVT RegisterVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 532 | unsigned NumIntermediates; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 533 | unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, |
Devang Patel | 8f09bea | 2010-08-26 20:32:32 +0000 | [diff] [blame] | 534 | IntermediateVT, |
| 535 | NumIntermediates, RegisterVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 536 | unsigned NumElements = ValueVT.getVectorNumElements(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 537 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 538 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 539 | NumParts = NumRegs; // Silence a compiler warning. |
Patrik Hagglund | b9e12e5 | 2012-12-19 12:33:30 +0000 | [diff] [blame] | 540 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 541 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 542 | // Split the vector into intermediate operands. |
| 543 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 544 | for (unsigned i = 0; i != NumIntermediates; ++i) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 545 | if (IntermediateVT.isVector()) |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 546 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 547 | IntermediateVT, Val, |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 548 | DAG.getIntPtrConstant(i * (NumElements / NumIntermediates))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 549 | else |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 550 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, |
Chris Lattner | e6f7c26 | 2010-08-25 22:49:25 +0000 | [diff] [blame] | 551 | IntermediateVT, Val, DAG.getIntPtrConstant(i)); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 552 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 553 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 554 | // Split the intermediate operands into legal parts. |
| 555 | if (NumParts == NumIntermediates) { |
| 556 | // If the register was not expanded, promote or copy the value, |
| 557 | // as appropriate. |
| 558 | for (unsigned i = 0; i != NumParts; ++i) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 559 | getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 560 | } else if (NumParts > 0) { |
| 561 | // If the intermediate type was expanded, split each the value into |
| 562 | // legal parts. |
| 563 | assert(NumParts % NumIntermediates == 0 && |
| 564 | "Must expand into a divisible number of parts!"); |
| 565 | unsigned Factor = NumParts / NumIntermediates; |
| 566 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 567 | getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 571 | namespace { |
| 572 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 573 | /// that a particular set of values is assigned, and the type information |
| 574 | /// about the value. The most common situation is to represent one value at a |
| 575 | /// time, but struct or array values are handled element-wise as multiple |
| 576 | /// values. The splitting of aggregates is performed recursively, so that we |
| 577 | /// never have aggregate-typed registers. The values at this point do not |
| 578 | /// necessarily have legal types, so each value may require one or more |
| 579 | /// registers of some legal type. |
| 580 | /// |
| 581 | struct RegsForValue { |
| 582 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 583 | /// may need be promoted or synthesized from one or more registers. |
| 584 | /// |
| 585 | SmallVector<EVT, 4> ValueVTs; |
| 586 | |
| 587 | /// RegVTs - The value types of the registers. This is the same size as |
| 588 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 589 | /// register or registers are. (Individual values are never synthesized |
| 590 | /// from more than one type of register.) |
| 591 | /// |
| 592 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 593 | /// getRegisterType member function, however when with physical registers |
| 594 | /// it is necessary to have a separate record of the types. |
| 595 | /// |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 596 | SmallVector<MVT, 4> RegVTs; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 597 | |
| 598 | /// Regs - This list holds the registers assigned to the values. |
| 599 | /// Each legal or promoted value requires one register, and each |
| 600 | /// expanded value requires multiple registers. |
| 601 | /// |
| 602 | SmallVector<unsigned, 4> Regs; |
| 603 | |
| 604 | RegsForValue() {} |
| 605 | |
| 606 | RegsForValue(const SmallVector<unsigned, 4> ®s, |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 607 | MVT regvt, EVT valuevt) |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 608 | : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 609 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 610 | RegsForValue(LLVMContext &Context, const TargetLowering &tli, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 611 | unsigned Reg, Type *Ty) { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 612 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 613 | |
| 614 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 615 | EVT ValueVT = ValueVTs[Value]; |
| 616 | unsigned NumRegs = tli.getNumRegisters(Context, ValueVT); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 617 | MVT RegisterVT = tli.getRegisterType(Context, ValueVT); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 618 | for (unsigned i = 0; i != NumRegs; ++i) |
| 619 | Regs.push_back(Reg + i); |
| 620 | RegVTs.push_back(RegisterVT); |
| 621 | Reg += NumRegs; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /// areValueTypesLegal - Return true if types of all the values are legal. |
| 626 | bool areValueTypesLegal(const TargetLowering &TLI) { |
| 627 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 628 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 629 | if (!TLI.isTypeLegal(RegisterVT)) |
| 630 | return false; |
| 631 | } |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | /// append - Add the specified values to this one. |
| 636 | void append(const RegsForValue &RHS) { |
| 637 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 638 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 639 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 640 | } |
| 641 | |
| 642 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 643 | /// this value and returns the result as a ValueVTs value. This uses |
| 644 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 645 | /// If the Flag pointer is NULL, no flag is used. |
| 646 | SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, |
| 647 | DebugLoc dl, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 648 | SDValue &Chain, SDValue *Flag, |
| 649 | const Value *V = 0) const; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 650 | |
| 651 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 652 | /// specified value into the registers specified by this object. This uses |
| 653 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 654 | /// If the Flag pointer is NULL, no flag is used. |
| 655 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 656 | SDValue &Chain, SDValue *Flag, const Value *V) const; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 657 | |
| 658 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 659 | /// operand list. This adds the code marker, matching input operand index |
| 660 | /// (if applicable), and includes the number of values added into it. |
| 661 | void AddInlineAsmOperands(unsigned Kind, |
| 662 | bool HasMatching, unsigned MatchingIdx, |
| 663 | SelectionDAG &DAG, |
| 664 | std::vector<SDValue> &Ops) const; |
| 665 | }; |
| 666 | } |
| 667 | |
| 668 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 669 | /// this value and returns the result as a ValueVT value. This uses |
| 670 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 671 | /// If the Flag pointer is NULL, no flag is used. |
| 672 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
| 673 | FunctionLoweringInfo &FuncInfo, |
| 674 | DebugLoc dl, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 675 | SDValue &Chain, SDValue *Flag, |
| 676 | const Value *V) const { |
Dan Gohman | 7da5d3f | 2010-07-26 18:15:41 +0000 | [diff] [blame] | 677 | // A Value with type {} or [0 x %t] needs no registers. |
| 678 | if (ValueVTs.empty()) |
| 679 | return SDValue(); |
| 680 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 681 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 682 | |
| 683 | // Assemble the legal parts into the final values. |
| 684 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 685 | SmallVector<SDValue, 8> Parts; |
| 686 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 687 | // Copy the legal parts from the registers. |
| 688 | EVT ValueVT = ValueVTs[Value]; |
| 689 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 690 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 691 | |
| 692 | Parts.resize(NumRegs); |
| 693 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 694 | SDValue P; |
| 695 | if (Flag == 0) { |
| 696 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); |
| 697 | } else { |
| 698 | P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag); |
| 699 | *Flag = P.getValue(2); |
| 700 | } |
| 701 | |
| 702 | Chain = P.getValue(1); |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 703 | Parts[i] = P; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 704 | |
| 705 | // If the source register was virtual and if we know something about it, |
| 706 | // add an assert node. |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 707 | if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) || |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 708 | !RegisterVT.isInteger() || RegisterVT.isVector()) |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 709 | continue; |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 710 | |
| 711 | const FunctionLoweringInfo::LiveOutInfo *LOI = |
| 712 | FuncInfo.GetLiveOutRegInfo(Regs[Part+i]); |
| 713 | if (!LOI) |
| 714 | continue; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 715 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 716 | unsigned RegSize = RegisterVT.getSizeInBits(); |
Cameron Zwarich | e1497b9 | 2011-02-24 10:00:08 +0000 | [diff] [blame] | 717 | unsigned NumSignBits = LOI->NumSignBits; |
| 718 | unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes(); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 719 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 720 | // FIXME: We capture more information than the dag can represent. For |
| 721 | // now, just use the tightest assertzext/assertsext possible. |
| 722 | bool isSExt = true; |
| 723 | EVT FromVT(MVT::Other); |
| 724 | if (NumSignBits == RegSize) |
| 725 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 726 | else if (NumZeroBits >= RegSize-1) |
| 727 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 728 | else if (NumSignBits > RegSize-8) |
| 729 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
| 730 | else if (NumZeroBits >= RegSize-8) |
| 731 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 732 | else if (NumSignBits > RegSize-16) |
| 733 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
| 734 | else if (NumZeroBits >= RegSize-16) |
| 735 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
| 736 | else if (NumSignBits > RegSize-32) |
| 737 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
| 738 | else if (NumZeroBits >= RegSize-32) |
| 739 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
| 740 | else |
| 741 | continue; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 742 | |
Chris Lattner | d5b4db9 | 2010-12-13 01:11:17 +0000 | [diff] [blame] | 743 | // Add an assertion node. |
| 744 | assert(FromVT != MVT::Other); |
| 745 | Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, |
| 746 | RegisterVT, P, DAG.getValueType(FromVT)); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 750 | NumRegs, RegisterVT, ValueVT, V); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 751 | Part += NumRegs; |
| 752 | Parts.clear(); |
| 753 | } |
| 754 | |
| 755 | return DAG.getNode(ISD::MERGE_VALUES, dl, |
| 756 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 757 | &Values[0], ValueVTs.size()); |
| 758 | } |
| 759 | |
| 760 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 761 | /// specified value into the registers specified by this object. This uses |
| 762 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 763 | /// If the Flag pointer is NULL, no flag is used. |
| 764 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 765 | SDValue &Chain, SDValue *Flag, |
| 766 | const Value *V) const { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 767 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 768 | |
| 769 | // Get the list of the values's legal parts. |
| 770 | unsigned NumRegs = Regs.size(); |
| 771 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 772 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 773 | EVT ValueVT = ValueVTs[Value]; |
| 774 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 775 | MVT RegisterVT = RegVTs[Value]; |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 776 | ISD::NodeType ExtendKind = |
| 777 | TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 3ac1884 | 2010-08-24 23:20:40 +0000 | [diff] [blame] | 779 | getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 780 | &Parts[Part], NumParts, RegisterVT, V, ExtendKind); |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 781 | Part += NumParts; |
| 782 | } |
| 783 | |
| 784 | // Copy the parts into the registers. |
| 785 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 786 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 787 | SDValue Part; |
| 788 | if (Flag == 0) { |
| 789 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); |
| 790 | } else { |
| 791 | Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag); |
| 792 | *Flag = Part.getValue(1); |
| 793 | } |
| 794 | |
| 795 | Chains[i] = Part.getValue(0); |
| 796 | } |
| 797 | |
| 798 | if (NumRegs == 1 || Flag) |
| 799 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
| 800 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 801 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 802 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 803 | // user as well as a successor (the TF operands are flagged to the user). |
| 804 | // c1, f1 = CopyToReg |
| 805 | // c2, f2 = CopyToReg |
| 806 | // c3 = TokenFactor c1, c2 |
| 807 | // ... |
| 808 | // = op c3, ..., f2 |
| 809 | Chain = Chains[NumRegs-1]; |
| 810 | else |
| 811 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs); |
| 812 | } |
| 813 | |
| 814 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 815 | /// operand list. This adds the code marker and includes the number of |
| 816 | /// values added into it. |
| 817 | void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching, |
| 818 | unsigned MatchingIdx, |
| 819 | SelectionDAG &DAG, |
| 820 | std::vector<SDValue> &Ops) const { |
| 821 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 822 | |
| 823 | unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size()); |
| 824 | if (HasMatching) |
| 825 | Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx); |
Jakob Stoklund Olesen | 459b74b | 2011-10-12 23:37:29 +0000 | [diff] [blame] | 826 | else if (!Regs.empty() && |
| 827 | TargetRegisterInfo::isVirtualRegister(Regs.front())) { |
| 828 | // Put the register class of the virtual registers in the flag word. That |
| 829 | // way, later passes can recompute register class constraints for inline |
| 830 | // assembly as well as normal instructions. |
| 831 | // Don't do this for tied operands that can use the regclass information |
| 832 | // from the def. |
| 833 | const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 834 | const TargetRegisterClass *RC = MRI.getRegClass(Regs.front()); |
| 835 | Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID()); |
| 836 | } |
| 837 | |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 838 | SDValue Res = DAG.getTargetConstant(Flag, MVT::i32); |
| 839 | Ops.push_back(Res); |
| 840 | |
| 841 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 842 | unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 843 | MVT RegisterVT = RegVTs[Value]; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 844 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 845 | assert(Reg < Regs.size() && "Mismatch in # registers expected"); |
| 846 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
| 847 | } |
| 848 | } |
| 849 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 850 | |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 851 | void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa, |
| 852 | const TargetLibraryInfo *li) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 853 | AA = &aa; |
| 854 | GFI = gfi; |
Owen Anderson | 243eb9e | 2011-12-08 22:15:21 +0000 | [diff] [blame] | 855 | LibInfo = li; |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 856 | TD = DAG.getTarget().getDataLayout(); |
Richard Smith | cb1f68d | 2012-08-22 00:42:39 +0000 | [diff] [blame] | 857 | Context = DAG.getContext(); |
Bill Wendling | 4ed1fb0 | 2011-10-15 01:00:26 +0000 | [diff] [blame] | 858 | LPadToCallSiteMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 861 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 862 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 863 | /// for a new block. This doesn't clear out information about |
| 864 | /// additional blocks that are needed to complete switch lowering |
| 865 | /// or PHI node updating; that information is cleared out as it is |
| 866 | /// consumed. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 867 | void SelectionDAGBuilder::clear() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 868 | NodeMap.clear(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 869 | UnusedArgNodeMap.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 870 | PendingLoads.clear(); |
| 871 | PendingExports.clear(); |
Chris Lattner | a4f2bb0 | 2010-04-02 20:17:23 +0000 | [diff] [blame] | 872 | CurDebugLoc = DebugLoc(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 873 | HasTailCall = false; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 876 | /// clearDanglingDebugInfo - Clear the dangling debug information |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 877 | /// map. This function is separated from the clear so that debug |
Devang Patel | 2338575 | 2011-05-23 17:44:13 +0000 | [diff] [blame] | 878 | /// information that is dangling in a basic block can be properly |
| 879 | /// resolved in a different basic block. This allows the |
| 880 | /// SelectionDAG to resolve dangling debug information attached |
| 881 | /// to PHI nodes. |
| 882 | void SelectionDAGBuilder::clearDanglingDebugInfo() { |
| 883 | DanglingDebugInfoMap.clear(); |
| 884 | } |
| 885 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 886 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 887 | /// flushing any PendingLoad items. This must be done before emitting |
| 888 | /// a store or any other node that may need to be ordered after any |
| 889 | /// prior load instructions. |
| 890 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 891 | SDValue SelectionDAGBuilder::getRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 892 | if (PendingLoads.empty()) |
| 893 | return DAG.getRoot(); |
| 894 | |
| 895 | if (PendingLoads.size() == 1) { |
| 896 | SDValue Root = PendingLoads[0]; |
| 897 | DAG.setRoot(Root); |
| 898 | PendingLoads.clear(); |
| 899 | return Root; |
| 900 | } |
| 901 | |
| 902 | // Otherwise, we have to make a token factor node. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 903 | SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 904 | &PendingLoads[0], PendingLoads.size()); |
| 905 | PendingLoads.clear(); |
| 906 | DAG.setRoot(Root); |
| 907 | return Root; |
| 908 | } |
| 909 | |
| 910 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 911 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 912 | /// to do this before emitting a terminator instruction. |
| 913 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 914 | SDValue SelectionDAGBuilder::getControlRoot() { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 915 | SDValue Root = DAG.getRoot(); |
| 916 | |
| 917 | if (PendingExports.empty()) |
| 918 | return Root; |
| 919 | |
| 920 | // Turn all of the CopyToReg chains into one factored node. |
| 921 | if (Root.getOpcode() != ISD::EntryToken) { |
| 922 | unsigned i = 0, e = PendingExports.size(); |
| 923 | for (; i != e; ++i) { |
| 924 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 925 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 926 | break; // Don't add the root if we already indirectly depend on it. |
| 927 | } |
| 928 | |
| 929 | if (i == e) |
| 930 | PendingExports.push_back(Root); |
| 931 | } |
| 932 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 933 | Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 934 | &PendingExports[0], |
| 935 | PendingExports.size()); |
| 936 | PendingExports.clear(); |
| 937 | DAG.setRoot(Root); |
| 938 | return Root; |
| 939 | } |
| 940 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 941 | void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) { |
| 942 | if (DAG.GetOrdering(Node) != 0) return; // Already has ordering. |
| 943 | DAG.AssignOrdering(Node, SDNodeOrder); |
| 944 | |
| 945 | for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) |
| 946 | AssignOrderingToNode(Node->getOperand(I).getNode()); |
| 947 | } |
| 948 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 949 | void SelectionDAGBuilder::visit(const Instruction &I) { |
Dan Gohman | c105a2b | 2010-04-22 20:55:53 +0000 | [diff] [blame] | 950 | // Set up outgoing PHI node register values before emitting the terminator. |
| 951 | if (isa<TerminatorInst>(&I)) |
| 952 | HandlePHINodesInSuccessorBlocks(I.getParent()); |
| 953 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 954 | CurDebugLoc = I.getDebugLoc(); |
| 955 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 956 | visit(I.getOpcode(), I); |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 957 | |
Dan Gohman | 92884f7 | 2010-04-20 15:03:56 +0000 | [diff] [blame] | 958 | if (!isa<TerminatorInst>(&I) && !HasTailCall) |
| 959 | CopyToExportRegsIfNeeded(&I); |
| 960 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 961 | CurDebugLoc = DebugLoc(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 964 | void SelectionDAGBuilder::visitPHI(const PHINode &) { |
| 965 | llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!"); |
| 966 | } |
| 967 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 968 | void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 969 | // Note: this doesn't use InstVisitor, because it has to work with |
| 970 | // ConstantExpr's in addition to instructions. |
| 971 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 972 | default: llvm_unreachable("Unknown instruction type encountered!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 973 | // Build the switch statement using the Instruction.def file. |
| 974 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
Galina Kistanova | 72ea0c9 | 2012-07-19 04:50:12 +0000 | [diff] [blame] | 975 | case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break; |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 976 | #include "llvm/IR/Instruction.def" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 977 | } |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 978 | |
| 979 | // Assign the ordering to the freshly created DAG nodes. |
| 980 | if (NodeMap.count(&I)) { |
| 981 | ++SDNodeOrder; |
| 982 | AssignOrderingToNode(getValue(&I).getNode()); |
| 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 |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +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; |
| 1023 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType()); |
| 1024 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 1025 | N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL, V); |
Devang Patel | 8f31428 | 2011-01-25 18:09:58 +0000 | [diff] [blame] | 1026 | resolveDanglingDebugInfo(V, N); |
| 1027 | return N; |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | // Otherwise create a new SDValue and remember it. |
| 1031 | SDValue Val = getValueImpl(V); |
| 1032 | NodeMap[V] = Val; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1033 | resolveDanglingDebugInfo(V, Val); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1034 | return Val; |
| 1035 | } |
| 1036 | |
| 1037 | /// getNonRegisterValue - Return an SDValue for the given Value, but |
| 1038 | /// don't look in FuncInfo.ValueMap for a virtual register. |
| 1039 | SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) { |
| 1040 | // If we already have an SDValue for this value, use it. |
| 1041 | SDValue &N = NodeMap[V]; |
| 1042 | if (N.getNode()) return N; |
| 1043 | |
| 1044 | // Otherwise create a new SDValue and remember it. |
| 1045 | SDValue Val = getValueImpl(V); |
| 1046 | NodeMap[V] = Val; |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1047 | resolveDanglingDebugInfo(V, Val); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1048 | return Val; |
| 1049 | } |
| 1050 | |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 1051 | /// getValueImpl - Helper function for getValue and getNonRegisterValue. |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1052 | /// Create an SDValue for the given value. |
| 1053 | SDValue SelectionDAGBuilder::getValueImpl(const Value *V) { |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1054 | if (const Constant *C = dyn_cast<Constant>(V)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1055 | EVT VT = TLI.getValueType(V->getType(), true); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1056 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1057 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1058 | return DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1059 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1060 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 1061 | return DAG.getGlobalAddress(GV, getCurDebugLoc(), VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1062 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1063 | if (isa<ConstantPointerNull>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1064 | return DAG.getConstant(0, TLI.getPointerTy()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1065 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1066 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1067 | return DAG.getConstantFP(*CFP, VT); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1068 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1069 | if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1070 | return DAG.getUNDEF(VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1071 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1072 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1073 | visit(CE->getOpcode(), *CE); |
| 1074 | SDValue N1 = NodeMap[V]; |
Dan Gohman | ac7d05c | 2010-04-16 16:55:18 +0000 | [diff] [blame] | 1075 | assert(N1.getNode() && "visit didn't populate the NodeMap!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1076 | return N1; |
| 1077 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1078 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1079 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 1080 | SmallVector<SDValue, 4> Constants; |
| 1081 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 1082 | OI != OE; ++OI) { |
| 1083 | SDNode *Val = getValue(*OI).getNode(); |
Dan Gohman | ed48caf | 2009-09-08 01:44:02 +0000 | [diff] [blame] | 1084 | // If the operand is an empty aggregate, there are no values. |
| 1085 | if (!Val) continue; |
| 1086 | // Add each leaf value from the operand to the Constants list |
| 1087 | // to form a flattened list of all the values. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1088 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 1089 | Constants.push_back(SDValue(Val, i)); |
| 1090 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1091 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1092 | return DAG.getMergeValues(&Constants[0], Constants.size(), |
| 1093 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1094 | } |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (const ConstantDataSequential *CDS = |
| 1097 | dyn_cast<ConstantDataSequential>(C)) { |
| 1098 | SmallVector<SDValue, 4> Ops; |
Chris Lattner | 0f193b8 | 2012-01-25 01:27:20 +0000 | [diff] [blame] | 1099 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1100 | SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode(); |
| 1101 | // Add each leaf value from the operand to the Constants list |
| 1102 | // to form a flattened list of all the values. |
| 1103 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 1104 | Ops.push_back(SDValue(Val, i)); |
| 1105 | } |
| 1106 | |
| 1107 | if (isa<ArrayType>(CDS->getType())) |
| 1108 | return DAG.getMergeValues(&Ops[0], Ops.size(), getCurDebugLoc()); |
| 1109 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 1110 | VT, &Ops[0], Ops.size()); |
| 1111 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1112 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1113 | if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1114 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 1115 | "Unknown struct or array constant!"); |
| 1116 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1117 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1118 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 1119 | unsigned NumElts = ValueVTs.size(); |
| 1120 | if (NumElts == 0) |
| 1121 | return SDValue(); // empty struct |
| 1122 | SmallVector<SDValue, 4> Constants(NumElts); |
| 1123 | for (unsigned i = 0; i != NumElts; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1124 | EVT EltVT = ValueVTs[i]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1125 | if (isa<UndefValue>(C)) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1126 | Constants[i] = DAG.getUNDEF(EltVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1127 | else if (EltVT.isFloatingPoint()) |
| 1128 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 1129 | else |
| 1130 | Constants[i] = DAG.getConstant(0, EltVT); |
| 1131 | } |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1132 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1133 | return DAG.getMergeValues(&Constants[0], NumElts, |
| 1134 | getCurDebugLoc()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 1137 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1138 | return DAG.getBlockAddress(BA, VT); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1140 | VectorType *VecTy = cast<VectorType>(V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1141 | unsigned NumElements = VecTy->getNumElements(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1142 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1143 | // Now that we know the number and type of the elements, get that number of |
| 1144 | // elements into the Ops array based on what kind of constant it is. |
| 1145 | SmallVector<SDValue, 16> Ops; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1146 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1147 | for (unsigned i = 0; i != NumElements; ++i) |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1148 | Ops.push_back(getValue(CV->getOperand(i))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1149 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1150 | assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1151 | EVT EltVT = TLI.getValueType(VecTy->getElementType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1152 | |
| 1153 | SDValue Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1154 | if (EltVT.isFloatingPoint()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1155 | Op = DAG.getConstantFP(0, EltVT); |
| 1156 | else |
| 1157 | Op = DAG.getConstant(0, EltVT); |
| 1158 | Ops.assign(NumElements, Op); |
| 1159 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1160 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1161 | // Create a BUILD_VECTOR node. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1162 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 1163 | VT, &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1164 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1165 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1166 | // If this is a static alloca, generate it as the frameindex instead of |
| 1167 | // computation. |
| 1168 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 1169 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 1170 | FuncInfo.StaticAllocaMap.find(AI); |
| 1171 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 1172 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 1173 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1174 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1175 | // If this is an instruction which fast-isel has deferred, select it now. |
| 1176 | if (const Instruction *Inst = dyn_cast<Instruction>(V)) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1177 | unsigned InReg = FuncInfo.InitializeRegForValue(Inst); |
| 1178 | RegsForValue RFV(*DAG.getContext(), TLI, InReg, Inst->getType()); |
| 1179 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 1180 | return RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL, V); |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1181 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1182 | |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 1183 | llvm_unreachable("Can't get register for value!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1186 | void SelectionDAGBuilder::visitRet(const ReturnInst &I) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1187 | SDValue Chain = getControlRoot(); |
| 1188 | SmallVector<ISD::OutputArg, 8> Outs; |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1189 | SmallVector<SDValue, 8> OutVals; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1190 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 1191 | if (!FuncInfo.CanLowerReturn) { |
| 1192 | unsigned DemoteReg = FuncInfo.DemoteRegister; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1193 | const Function *F = I.getParent()->getParent(); |
| 1194 | |
| 1195 | // Emit a store of the return value through the virtual register. |
| 1196 | // Leave Outs empty so that LowerReturn won't try to load return |
| 1197 | // registers the usual way. |
| 1198 | SmallVector<EVT, 1> PtrValueVTs; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1199 | ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1200 | PtrValueVTs); |
| 1201 | |
| 1202 | SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]); |
| 1203 | SDValue RetOp = getValue(I.getOperand(0)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 1204 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1205 | SmallVector<EVT, 4> ValueVTs; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1206 | SmallVector<uint64_t, 4> Offsets; |
| 1207 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1208 | unsigned NumValues = ValueVTs.size(); |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 1209 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1210 | SmallVector<SDValue, 4> Chains(NumValues); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1211 | for (unsigned i = 0; i != NumValues; ++i) { |
Chris Lattner | a13b860 | 2010-08-24 23:10:06 +0000 | [diff] [blame] | 1212 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 1213 | RetPtr.getValueType(), RetPtr, |
| 1214 | DAG.getIntPtrConstant(Offsets[i])); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1215 | Chains[i] = |
| 1216 | DAG.getStore(Chain, getCurDebugLoc(), |
| 1217 | SDValue(RetOp.getNode(), RetOp.getResNo() + i), |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 1218 | // FIXME: better loc info would be nice. |
| 1219 | Add, MachinePointerInfo(), false, false, 0); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1222 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 1223 | MVT::Other, &Chains[0], NumValues); |
Chris Lattner | 25d5837 | 2010-02-28 18:53:13 +0000 | [diff] [blame] | 1224 | } else if (I.getNumOperands() != 0) { |
| 1225 | SmallVector<EVT, 4> ValueVTs; |
| 1226 | ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs); |
| 1227 | unsigned NumValues = ValueVTs.size(); |
| 1228 | if (NumValues) { |
| 1229 | SDValue RetOp = getValue(I.getOperand(0)); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1230 | for (unsigned j = 0, f = NumValues; j != f; ++j) { |
| 1231 | EVT VT = ValueVTs[j]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1232 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1233 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1234 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1235 | const Function *F = I.getParent()->getParent(); |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1236 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1237 | Attribute::SExt)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1238 | ExtendKind = ISD::SIGN_EXTEND; |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1239 | else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1240 | Attribute::ZExt)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1241 | ExtendKind = ISD::ZERO_EXTEND; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1242 | |
Cameron Zwarich | 7bbf0ee | 2011-03-17 14:53:37 +0000 | [diff] [blame] | 1243 | if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) |
Patrik Hagglund | e5c6591 | 2012-12-19 12:02:25 +0000 | [diff] [blame] | 1244 | VT = TLI.getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1245 | |
| 1246 | unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 1247 | MVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1248 | SmallVector<SDValue, 4> Parts(NumParts); |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 1249 | getCopyToParts(DAG, getCurDebugLoc(), |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1250 | SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 1251 | &Parts[0], NumParts, PartVT, &I, ExtendKind); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1252 | |
| 1253 | // 'inreg' on function refers to return value |
| 1254 | ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 1255 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 1256 | Attribute::InReg)) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1257 | Flags.setInReg(); |
| 1258 | |
| 1259 | // Propagate extension type if any |
Cameron Zwarich | 8df6bf5 | 2011-03-16 22:20:07 +0000 | [diff] [blame] | 1260 | if (ExtendKind == ISD::SIGN_EXTEND) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1261 | Flags.setSExt(); |
Cameron Zwarich | 8df6bf5 | 2011-03-16 22:20:07 +0000 | [diff] [blame] | 1262 | else if (ExtendKind == ISD::ZERO_EXTEND) |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 1263 | Flags.setZExt(); |
| 1264 | |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1265 | for (unsigned i = 0; i < NumParts; ++i) { |
| 1266 | Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(), |
Manman Ren | 0a1544d | 2012-11-01 23:49:58 +0000 | [diff] [blame] | 1267 | /*isfixed=*/true, 0, 0)); |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1268 | OutVals.push_back(Parts[i]); |
| 1269 | } |
Evan Cheng | 3927f43 | 2009-03-25 20:20:11 +0000 | [diff] [blame] | 1270 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1271 | } |
| 1272 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1273 | |
| 1274 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1275 | CallingConv::ID CallConv = |
| 1276 | DAG.getMachineFunction().getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1277 | Chain = TLI.LowerReturn(Chain, CallConv, isVarArg, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1278 | Outs, OutVals, getCurDebugLoc(), DAG); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1279 | |
| 1280 | // Verify that the target's LowerReturn behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1281 | assert(Chain.getNode() && Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 1282 | "LowerReturn didn't return a valid chain!"); |
| 1283 | |
| 1284 | // Update the DAG with the new chain value resulting from return lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1285 | DAG.setRoot(Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1288 | /// CopyToExportRegsIfNeeded - If the given value has virtual registers |
| 1289 | /// created for it, emit nodes to copy the value into the virtual |
| 1290 | /// registers. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1291 | void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) { |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 1292 | // Skip empty types |
| 1293 | if (V->getType()->isEmptyTy()) |
| 1294 | return; |
| 1295 | |
Dan Gohman | 33b7a29 | 2010-04-16 17:15:02 +0000 | [diff] [blame] | 1296 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
| 1297 | if (VMI != FuncInfo.ValueMap.end()) { |
| 1298 | assert(!V->use_empty() && "Unused value assigned virtual registers!"); |
| 1299 | CopyValueToVirtualRegister(V, VMI->second); |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1303 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1304 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1305 | /// CopyTo/FromReg. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1306 | void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1307 | // No need to export constants. |
| 1308 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1309 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1310 | // Already exported? |
| 1311 | if (FuncInfo.isExportedInst(V)) return; |
| 1312 | |
| 1313 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 1314 | CopyValueToVirtualRegister(V, Reg); |
| 1315 | } |
| 1316 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1317 | bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1318 | const BasicBlock *FromBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1319 | // The operands of the setcc have to be in this block. We don't know |
| 1320 | // how to export them from some other block. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1321 | if (const Instruction *VI = dyn_cast<Instruction>(V)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1322 | // Can export from current BB. |
| 1323 | if (VI->getParent() == FromBB) |
| 1324 | return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1325 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1326 | // Is already exported, noop. |
| 1327 | return FuncInfo.isExportedInst(V); |
| 1328 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1329 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1330 | // If this is an argument, we can export it if the BB is the entry block or |
| 1331 | // if it is already exported. |
| 1332 | if (isa<Argument>(V)) { |
| 1333 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1334 | return true; |
| 1335 | |
| 1336 | // Otherwise, can only export this if it is already exported. |
| 1337 | return FuncInfo.isExportedInst(V); |
| 1338 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1339 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1340 | // Otherwise, constants can always be exported. |
| 1341 | return true; |
| 1342 | } |
| 1343 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1344 | /// Return branch probability calculated by BranchProbabilityInfo for IR blocks. |
Jakub Staszak | 25101bb | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 1345 | uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src, |
| 1346 | const MachineBasicBlock *Dst) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1347 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
| 1348 | if (!BPI) |
| 1349 | return 0; |
Jakub Staszak | 95ece8e | 2011-07-29 20:05:36 +0000 | [diff] [blame] | 1350 | const BasicBlock *SrcBB = Src->getBasicBlock(); |
| 1351 | const BasicBlock *DstBB = Dst->getBasicBlock(); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1352 | return BPI->getEdgeWeight(SrcBB, DstBB); |
| 1353 | } |
| 1354 | |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 1355 | void SelectionDAGBuilder:: |
| 1356 | addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst, |
| 1357 | uint32_t Weight /* = 0 */) { |
| 1358 | if (!Weight) |
| 1359 | Weight = getEdgeWeight(Src, Dst); |
| 1360 | Src->addSuccessor(Dst, Weight); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1364 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1365 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1366 | return I->getParent() == BB; |
| 1367 | return true; |
| 1368 | } |
| 1369 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1370 | /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. |
| 1371 | /// This function emits a branch and is used at the leaves of an OR or an |
| 1372 | /// AND operator tree. |
| 1373 | /// |
| 1374 | void |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1375 | SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1376 | MachineBasicBlock *TBB, |
| 1377 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1378 | MachineBasicBlock *CurBB, |
| 1379 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1380 | const BasicBlock *BB = CurBB->getBasicBlock(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1381 | |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1382 | // If the leaf of the tree is a comparison, merge the condition into |
| 1383 | // the caseblock. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1384 | if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1385 | // The operands of the cmp have to be in this block. We don't know |
| 1386 | // how to export them from some other block. If this is the first block |
| 1387 | // of the sequence, no exporting is needed. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1388 | if (CurBB == SwitchBB || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1389 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1390 | isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1391 | ISD::CondCode Condition; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1392 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1393 | Condition = getICmpCondCode(IC->getPredicate()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1394 | } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 1395 | Condition = getFCmpCondCode(FC->getPredicate()); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1396 | if (TM.Options.NoNaNsFPMath) |
| 1397 | Condition = getFCmpCodeWithoutNaN(Condition); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1398 | } else { |
| 1399 | Condition = ISD::SETEQ; // silence warning. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1400 | llvm_unreachable("Unknown compare instruction"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1401 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1402 | |
| 1403 | CaseBlock CB(Condition, BOp->getOperand(0), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1404 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1405 | SwitchCases.push_back(CB); |
| 1406 | return; |
| 1407 | } |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1411 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1412 | NULL, TBB, FBB, CurBB); |
| 1413 | SwitchCases.push_back(CB); |
| 1414 | } |
| 1415 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1416 | /// FindMergedConditions - If Cond is an expression like |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1417 | void SelectionDAGBuilder::FindMergedConditions(const Value *Cond, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1418 | MachineBasicBlock *TBB, |
| 1419 | MachineBasicBlock *FBB, |
| 1420 | MachineBasicBlock *CurBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1421 | MachineBasicBlock *SwitchBB, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1422 | unsigned Opc) { |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1423 | // 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] | 1424 | const Instruction *BOp = dyn_cast<Instruction>(Cond); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1425 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 1426 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1427 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1428 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1429 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1430 | EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1431 | return; |
| 1432 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1433 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1434 | // Create TmpBB after CurBB. |
| 1435 | MachineFunction::iterator BBI = CurBB; |
| 1436 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1437 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1438 | CurBB->getParent()->insert(++BBI, TmpBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1439 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1440 | if (Opc == Instruction::Or) { |
| 1441 | // Codegen X | Y as: |
| 1442 | // jmp_if_X TBB |
| 1443 | // jmp TmpBB |
| 1444 | // TmpBB: |
| 1445 | // jmp_if_Y TBB |
| 1446 | // jmp FBB |
| 1447 | // |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1448 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1449 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1450 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1451 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1452 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1453 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1454 | } else { |
| 1455 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1456 | // Codegen X & Y as: |
| 1457 | // jmp_if_X TmpBB |
| 1458 | // jmp FBB |
| 1459 | // TmpBB: |
| 1460 | // jmp_if_Y TBB |
| 1461 | // jmp FBB |
| 1462 | // |
| 1463 | // This requires creation of TmpBB after CurBB. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1464 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1465 | // Emit the LHS condition. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1466 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1467 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1468 | // Emit the RHS condition into TmpBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1469 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1474 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1475 | /// false. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1476 | bool |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1477 | SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1478 | if (Cases.size() != 2) return true; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1479 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1480 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1481 | // will get folded into a single comparison, so don't emit two blocks. |
| 1482 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1483 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1484 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1485 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1486 | return false; |
| 1487 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 133ce87 | 2010-01-02 00:00:03 +0000 | [diff] [blame] | 1489 | // Handle: (X != null) | (Y != null) --> (X|Y) != 0 |
| 1490 | // Handle: (X == null) & (Y == null) --> (X|Y) == 0 |
| 1491 | if (Cases[0].CmpRHS == Cases[1].CmpRHS && |
| 1492 | Cases[0].CC == Cases[1].CC && |
| 1493 | isa<Constant>(Cases[0].CmpRHS) && |
| 1494 | cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { |
| 1495 | if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) |
| 1496 | return false; |
| 1497 | if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) |
| 1498 | return false; |
| 1499 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 1500 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1501 | return true; |
| 1502 | } |
| 1503 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1504 | void SelectionDAGBuilder::visitBr(const BranchInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1505 | MachineBasicBlock *BrMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1506 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1507 | // Update machine-CFG edges. |
| 1508 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1509 | |
| 1510 | // Figure out which block is immediately after the current one. |
| 1511 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1512 | MachineFunction::iterator BBI = BrMBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1513 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1514 | NextBlock = BBI; |
| 1515 | |
| 1516 | if (I.isUnconditional()) { |
| 1517 | // Update machine-CFG edges. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1518 | BrMBB->addSuccessor(Succ0MBB); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1519 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1520 | // If this is not a fall-through branch, emit the branch. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1521 | if (Succ0MBB != NextBlock) |
| 1522 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1523 | MVT::Other, getControlRoot(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1524 | DAG.getBasicBlock(Succ0MBB))); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1525 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1526 | return; |
| 1527 | } |
| 1528 | |
| 1529 | // If this condition is one of the special cases we handle, do special stuff |
| 1530 | // now. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1531 | const Value *CondVal = I.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1532 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1533 | |
| 1534 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1535 | // 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] | 1536 | // As long as jumps are not expensive, this should improve performance. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1537 | // For example, instead of something like: |
| 1538 | // cmp A, B |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1539 | // C = seteq |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1540 | // cmp D, E |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1541 | // F = setle |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1542 | // or C, F |
| 1543 | // jnz foo |
| 1544 | // Emit: |
| 1545 | // cmp A, B |
| 1546 | // je foo |
| 1547 | // cmp D, E |
| 1548 | // jle foo |
| 1549 | // |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1550 | if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1551 | if (!TLI.isJumpExpensive() && |
Chris Lattner | de189be | 2010-11-30 18:12:52 +0000 | [diff] [blame] | 1552 | BOp->hasOneUse() && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1553 | (BOp->getOpcode() == Instruction::And || |
| 1554 | BOp->getOpcode() == Instruction::Or)) { |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1555 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, |
| 1556 | BOp->getOpcode()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1557 | // If the compares in later blocks need to use values not currently |
| 1558 | // exported from this block, export them now. This block should always |
| 1559 | // be the first entry. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1560 | assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1561 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1562 | // Allow some cases to be rejected. |
| 1563 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1564 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1565 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1566 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1567 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1568 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1569 | // Emit the branch for this block. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1570 | visitSwitchCase(SwitchCases[0], BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1571 | SwitchCases.erase(SwitchCases.begin()); |
| 1572 | return; |
| 1573 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1574 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1575 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1576 | // SwitchCases. |
| 1577 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1578 | FuncInfo.MF->erase(SwitchCases[i].ThisBB); |
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 | SwitchCases.clear(); |
| 1581 | } |
| 1582 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 1583 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1584 | // Create a CaseBlock record representing this branch. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1585 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1586 | NULL, Succ0MBB, Succ1MBB, BrMBB); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1587 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1588 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1589 | // cond branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1590 | visitSwitchCase(CB, BrMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1594 | /// the binary search tree resulting from lowering a switch instruction. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1595 | void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB, |
| 1596 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1597 | SDValue Cond; |
| 1598 | SDValue CondLHS = getValue(CB.CmpLHS); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1599 | DebugLoc dl = getCurDebugLoc(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1600 | |
| 1601 | // Build the setcc now. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1602 | if (CB.CmpMHS == NULL) { |
| 1603 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1604 | // handle common cases produced by branch lowering. |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1605 | if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1606 | CB.CC == ISD::SETEQ) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1607 | Cond = CondLHS; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1608 | else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && |
Owen Anderson | f53c371 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 1609 | CB.CC == ISD::SETEQ) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1610 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1611 | Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1612 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1613 | Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1614 | } else { |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1615 | assert(CB.CC == ISD::SETCC_INVALID && |
| 1616 | "Condition is undefined for to-the-range belonging check."); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1617 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1618 | const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); |
| 1619 | const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1620 | |
| 1621 | SDValue CmpOp = getValue(CB.CmpMHS); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1622 | EVT VT = CmpOp.getValueType(); |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1623 | |
| 1624 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(false)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1625 | Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT), |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 1626 | ISD::SETULE); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1627 | } else { |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1628 | SDValue SUB = DAG.getNode(ISD::SUB, dl, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1629 | VT, CmpOp, DAG.getConstant(Low, VT)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1630 | Cond = DAG.getSetCC(dl, MVT::i1, SUB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1631 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1632 | } |
| 1633 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1634 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1635 | // Update successor info |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 1636 | addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight); |
Jakob Stoklund Olesen | e7fdef4 | 2012-08-20 21:39:52 +0000 | [diff] [blame] | 1637 | // TrueBB and FalseBB are always different unless the incoming IR is |
| 1638 | // degenerate. This only happens when running llc on weird IR. |
| 1639 | if (CB.TrueBB != CB.FalseBB) |
| 1640 | addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1641 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1642 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1643 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1644 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1645 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1646 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1647 | NextBlock = BBI; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1648 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1649 | // If the lhs block is the next block, invert the condition so that we can |
| 1650 | // fall through to the lhs instead of the rhs block. |
| 1651 | if (CB.TrueBB == NextBlock) { |
| 1652 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1653 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1654 | Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1655 | } |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1656 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1657 | SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1658 | MVT::Other, getControlRoot(), Cond, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 1659 | DAG.getBasicBlock(CB.TrueBB)); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1660 | |
Evan Cheng | 266a99d | 2010-09-23 06:51:55 +0000 | [diff] [blame] | 1661 | // Insert the false branch. Do this even if it's a fall through branch, |
| 1662 | // this makes it easier to do DAG optimizations which require inverting |
| 1663 | // the branch condition. |
| 1664 | BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, |
| 1665 | DAG.getBasicBlock(CB.FalseBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1666 | |
| 1667 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1671 | void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1672 | // Emit the code for the jump table |
| 1673 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1674 | EVT PTy = TLI.getPointerTy(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1675 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1676 | JT.Reg, PTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1677 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1678 | SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(), |
| 1679 | MVT::Other, Index.getValue(1), |
| 1680 | Table, Index); |
| 1681 | DAG.setRoot(BrJumpTable); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1685 | /// in the JumpTable from switch case. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1686 | void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1687 | JumpTableHeader &JTH, |
| 1688 | MachineBasicBlock *SwitchBB) { |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1689 | // Subtract the lowest switch case value from the value being switched on and |
| 1690 | // conditional branch to default mbb if the result is greater than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1691 | // difference between smallest and largest cases. |
| 1692 | SDValue SwitchOp = getValue(JTH.SValue); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1693 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1694 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1695 | DAG.getConstant(JTH.First, VT)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1696 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1697 | // 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] | 1698 | // 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] | 1699 | // can be used as an index into the jump table in a subsequent basic block. |
| 1700 | // This value may be smaller or larger than the target's pointer type, and |
| 1701 | // therefore require extension or truncating. |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1702 | SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1703 | |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 1704 | unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy()); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1705 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
| 1706 | JumpTableReg, SwitchOp); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1707 | JT.Reg = JumpTableReg; |
| 1708 | |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1709 | // Emit the range check for the jump table, and branch to the default block |
| 1710 | // for the switch statement if the value being switched on exceeds the largest |
| 1711 | // case in the switch. |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1712 | SDValue CMP = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1713 | TLI.getSetCCResultType(Sub.getValueType()), Sub, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1714 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1715 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1718 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1719 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1720 | MachineFunction::iterator BBI = SwitchBB; |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1721 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1722 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1723 | NextBlock = BBI; |
| 1724 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1725 | SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1726 | MVT::Other, CopyTo, CMP, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1727 | DAG.getBasicBlock(JT.Default)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1728 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1729 | if (JT.MBB != NextBlock) |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1730 | BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond, |
| 1731 | DAG.getBasicBlock(JT.MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1732 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1733 | DAG.setRoot(BrCond); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1737 | /// suitable for "bit tests" |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1738 | void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B, |
| 1739 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1740 | // Subtract the minimum value |
| 1741 | SDValue SwitchOp = getValue(B.SValue); |
Patrik Hagglund | 34525f9 | 2012-12-11 11:14:33 +0000 | [diff] [blame] | 1742 | EVT VT = SwitchOp.getValueType(); |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1743 | SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1744 | DAG.getConstant(B.First, VT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1745 | |
| 1746 | // Check range |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1747 | SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1748 | TLI.getSetCCResultType(Sub.getValueType()), |
| 1749 | Sub, DAG.getConstant(B.Range, VT), |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1750 | ISD::SETUGT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1751 | |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1752 | // Determine the type of the test operands. |
| 1753 | bool UsePtrType = false; |
| 1754 | if (!TLI.isTypeLegal(VT)) |
| 1755 | UsePtrType = true; |
| 1756 | else { |
| 1757 | for (unsigned i = 0, e = B.Cases.size(); i != e; ++i) |
Eli Friedman | 5c75af6 | 2011-10-12 22:46:45 +0000 | [diff] [blame] | 1758 | if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) { |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1759 | // Switch table case range are encoded into series of masks. |
| 1760 | // Just use pointer type, it's guaranteed to fit. |
| 1761 | UsePtrType = true; |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 | if (UsePtrType) { |
| 1766 | VT = TLI.getPointerTy(); |
| 1767 | Sub = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), VT); |
| 1768 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1769 | |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1770 | B.RegVT = VT.getSimpleVT(); |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 1771 | B.Reg = FuncInfo.CreateReg(B.RegVT); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1772 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1773 | B.Reg, Sub); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1774 | |
| 1775 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1776 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1777 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1778 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1779 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1780 | NextBlock = BBI; |
| 1781 | |
| 1782 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1783 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1784 | addSuccessorWithWeight(SwitchBB, B.Default); |
| 1785 | addSuccessorWithWeight(SwitchBB, MBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1786 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1787 | SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1788 | MVT::Other, CopyTo, RangeCmp, |
Anton Korobeynikov | 1bfe237 | 2008-12-23 22:25:45 +0000 | [diff] [blame] | 1789 | DAG.getBasicBlock(B.Default)); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1790 | |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1791 | if (MBB != NextBlock) |
| 1792 | BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo, |
| 1793 | DAG.getBasicBlock(MBB)); |
Bill Wendling | 3b7a41c | 2009-12-21 19:59:38 +0000 | [diff] [blame] | 1794 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1795 | DAG.setRoot(BrRange); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /// visitBitTestCase - this function produces one "bit test" |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1799 | void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB, |
| 1800 | MachineBasicBlock* NextMBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1801 | uint32_t BranchWeightToNext, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1802 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1803 | BitTestCase &B, |
| 1804 | MachineBasicBlock *SwitchBB) { |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 1805 | MVT VT = BB.RegVT; |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1806 | SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), |
| 1807 | Reg, VT); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1808 | SDValue Cmp; |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1809 | unsigned PopCount = CountPopulation_64(B.Mask); |
| 1810 | if (PopCount == 1) { |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1811 | // Testing for a single bit; just compare the shift count with what it |
| 1812 | // would need to be to shift a 1 bit in that position. |
| 1813 | Cmp = DAG.getSetCC(getCurDebugLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1814 | TLI.getSetCCResultType(VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1815 | ShiftOp, |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1816 | DAG.getConstant(CountTrailingZeros_64(B.Mask), VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1817 | ISD::SETEQ); |
Benjamin Kramer | 3ff2551 | 2011-07-14 01:38:42 +0000 | [diff] [blame] | 1818 | } else if (PopCount == BB.Range) { |
| 1819 | // There is only one zero bit in the range, test for it directly. |
| 1820 | Cmp = DAG.getSetCC(getCurDebugLoc(), |
| 1821 | TLI.getSetCCResultType(VT), |
| 1822 | ShiftOp, |
| 1823 | DAG.getConstant(CountTrailingOnes_64(B.Mask), VT), |
| 1824 | ISD::SETNE); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1825 | } else { |
| 1826 | // Make desired shift |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1827 | SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), VT, |
| 1828 | DAG.getConstant(1, VT), ShiftOp); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1829 | |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1830 | // Emit bit tests and jumps |
| 1831 | SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1832 | VT, SwitchVal, DAG.getConstant(B.Mask, VT)); |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1833 | Cmp = DAG.getSetCC(getCurDebugLoc(), |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 1834 | TLI.getSetCCResultType(VT), |
| 1835 | AndOp, DAG.getConstant(0, VT), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1836 | ISD::SETNE); |
| 1837 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1838 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1839 | // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight. |
| 1840 | addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight); |
| 1841 | // The branch weight from SwitchBB to NextMBB is BranchWeightToNext. |
| 1842 | addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1843 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 1844 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1845 | MVT::Other, getControlRoot(), |
Dan Gohman | 8e0163a | 2010-06-24 02:06:24 +0000 | [diff] [blame] | 1846 | Cmp, DAG.getBasicBlock(B.TargetBB)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1847 | |
| 1848 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1849 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1850 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1851 | MachineFunction::iterator BBI = SwitchBB; |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1852 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1853 | NextBlock = BBI; |
| 1854 | |
Evan Cheng | 8c1f432 | 2010-09-23 18:32:19 +0000 | [diff] [blame] | 1855 | if (NextMBB != NextBlock) |
| 1856 | BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd, |
| 1857 | DAG.getBasicBlock(NextMBB)); |
Bill Wendling | 0777e92 | 2009-12-21 21:59:52 +0000 | [diff] [blame] | 1858 | |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 1859 | DAG.setRoot(BrAnd); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1862 | void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1863 | MachineBasicBlock *InvokeMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1864 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1865 | // Retrieve successors. |
| 1866 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1867 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1868 | |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1869 | const Value *Callee(I.getCalledValue()); |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1870 | const Function *Fn = dyn_cast<Function>(Callee); |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1871 | if (isa<InlineAsm>(Callee)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1872 | visitInlineAsm(&I); |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1873 | else if (Fn && Fn->isIntrinsic()) { |
| 1874 | assert(Fn->getIntrinsicID() == Intrinsic::donothing); |
Manman Ren | 5e5974f | 2013-02-27 02:11:57 +0000 | [diff] [blame] | 1875 | // If donothing has a landingpad, we should clear CurrentCallSite. |
| 1876 | if (LandingPad) { |
| 1877 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 1878 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
| 1879 | if (CallSiteIndex) |
| 1880 | MMI.setCurrentCallSite(0); |
| 1881 | } |
Nuno Lopes | 4532bf6 | 2012-07-18 00:07:17 +0000 | [diff] [blame] | 1882 | // Ignore invokes to @llvm.donothing: jump directly to the next BB. |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 1883 | } else |
Gabor Greif | b67e6b3 | 2009-01-15 11:10:44 +0000 | [diff] [blame] | 1884 | LowerCallTo(&I, getValue(Callee), false, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1885 | |
| 1886 | // If the value of the invoke is used outside of its defining block, make it |
| 1887 | // available as a virtual register. |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 1888 | CopyToExportRegsIfNeeded(&I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1889 | |
| 1890 | // Update successor info |
Chandler Carruth | f264568 | 2011-11-22 11:37:46 +0000 | [diff] [blame] | 1891 | addSuccessorWithWeight(InvokeMBB, Return); |
| 1892 | addSuccessorWithWeight(InvokeMBB, LandingPad); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1893 | |
| 1894 | // Drop into normal successor. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 1895 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 1896 | MVT::Other, getControlRoot(), |
| 1897 | DAG.getBasicBlock(Return))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 1900 | void SelectionDAGBuilder::visitResume(const ResumeInst &RI) { |
| 1901 | llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!"); |
| 1902 | } |
| 1903 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1904 | void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) { |
| 1905 | assert(FuncInfo.MBB->isLandingPad() && |
| 1906 | "Call to landingpad not in landing pad!"); |
| 1907 | |
| 1908 | MachineBasicBlock *MBB = FuncInfo.MBB; |
| 1909 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 1910 | AddLandingPadInfo(LP, MMI, MBB); |
| 1911 | |
Bill Wendling | bdf9db6 | 2012-02-13 23:47:16 +0000 | [diff] [blame] | 1912 | // If there aren't registers to copy the values into (e.g., during SjLj |
| 1913 | // exceptions), then don't bother to create these DAG nodes. |
Lang Hames | 0796134 | 2012-02-14 04:45:49 +0000 | [diff] [blame] | 1914 | if (TLI.getExceptionPointerRegister() == 0 && |
Bill Wendling | bdf9db6 | 2012-02-13 23:47:16 +0000 | [diff] [blame] | 1915 | TLI.getExceptionSelectorRegister() == 0) |
| 1916 | return; |
| 1917 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 1918 | SmallVector<EVT, 2> ValueVTs; |
| 1919 | ComputeValueVTs(TLI, LP.getType(), ValueVTs); |
| 1920 | |
| 1921 | // Insert the EXCEPTIONADDR instruction. |
| 1922 | assert(FuncInfo.MBB->isLandingPad() && |
| 1923 | "Call to eh.exception not in landing pad!"); |
| 1924 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 1925 | SDValue Ops[2]; |
| 1926 | Ops[0] = DAG.getRoot(); |
| 1927 | SDValue Op1 = DAG.getNode(ISD::EXCEPTIONADDR, getCurDebugLoc(), VTs, Ops, 1); |
| 1928 | SDValue Chain = Op1.getValue(1); |
| 1929 | |
| 1930 | // Insert the EHSELECTION instruction. |
| 1931 | VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 1932 | Ops[0] = Op1; |
| 1933 | Ops[1] = Chain; |
| 1934 | SDValue Op2 = DAG.getNode(ISD::EHSELECTION, getCurDebugLoc(), VTs, Ops, 2); |
| 1935 | Chain = Op2.getValue(1); |
| 1936 | Op2 = DAG.getSExtOrTrunc(Op2, getCurDebugLoc(), MVT::i32); |
| 1937 | |
| 1938 | Ops[0] = Op1; |
| 1939 | Ops[1] = Op2; |
| 1940 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 1941 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 1942 | &Ops[0], 2); |
| 1943 | |
| 1944 | std::pair<SDValue, SDValue> RetPair = std::make_pair(Res, Chain); |
| 1945 | setValue(&LP, RetPair.first); |
| 1946 | DAG.setRoot(RetPair.second); |
| 1947 | } |
| 1948 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1949 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1950 | /// small case ranges). |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1951 | bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR, |
| 1952 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1953 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 1954 | MachineBasicBlock *Default, |
| 1955 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1956 | // Size is the number of Cases represented by this range. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1957 | size_t Size = CR.Range.second - CR.Range.first; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1958 | if (Size > 3) |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 1959 | return false; |
| 1960 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1961 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1962 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1963 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1964 | |
| 1965 | // Figure out which block is immediately after the current one. |
| 1966 | MachineBasicBlock *NextBlock = 0; |
| 1967 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1968 | |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 1969 | if (++BBI != FuncInfo.MF->end()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1970 | NextBlock = BBI; |
| 1971 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 1972 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1973 | // 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] | 1974 | // is the same as the other, but has one bit unset that the other has set, |
| 1975 | // use bit manipulation to do two compares at once. For example: |
| 1976 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 1977 | // TODO: This could be extended to merge any 2 cases in switches with 3 cases. |
| 1978 | // TODO: Handle cases where CR.CaseBB != SwitchBB. |
| 1979 | if (Size == 2 && CR.CaseBB == SwitchBB) { |
| 1980 | Case &Small = *CR.Range.first; |
| 1981 | Case &Big = *(CR.Range.second-1); |
| 1982 | |
| 1983 | if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) { |
| 1984 | const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue(); |
| 1985 | const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue(); |
| 1986 | |
| 1987 | // Check that there is only one bit different. |
| 1988 | if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 && |
| 1989 | (SmallValue | BigValue) == BigValue) { |
| 1990 | // Isolate the common bit. |
| 1991 | APInt CommonBit = BigValue & ~SmallValue; |
| 1992 | assert((SmallValue | CommonBit) == BigValue && |
| 1993 | CommonBit.countPopulation() == 1 && "Not a common bit?"); |
| 1994 | |
| 1995 | SDValue CondLHS = getValue(SV); |
| 1996 | EVT VT = CondLHS.getValueType(); |
| 1997 | DebugLoc DL = getCurDebugLoc(); |
| 1998 | |
| 1999 | SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS, |
| 2000 | DAG.getConstant(CommonBit, VT)); |
| 2001 | SDValue Cond = DAG.getSetCC(DL, MVT::i1, |
| 2002 | Or, DAG.getConstant(BigValue, VT), |
| 2003 | ISD::SETEQ); |
| 2004 | |
| 2005 | // Update successor info. |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2006 | // Both Small and Big will jump to Small.BB, so we sum up the weights. |
| 2007 | addSuccessorWithWeight(SwitchBB, Small.BB, |
| 2008 | Small.ExtraWeight + Big.ExtraWeight); |
| 2009 | addSuccessorWithWeight(SwitchBB, Default, |
| 2010 | // The default destination is the first successor in IR. |
| 2011 | BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0); |
Benjamin Kramer | ce750f0 | 2010-11-22 09:45:38 +0000 | [diff] [blame] | 2012 | |
| 2013 | // Insert the true branch. |
| 2014 | SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other, |
| 2015 | getControlRoot(), Cond, |
| 2016 | DAG.getBasicBlock(Small.BB)); |
| 2017 | |
| 2018 | // Insert the false branch. |
| 2019 | BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond, |
| 2020 | DAG.getBasicBlock(Default)); |
| 2021 | |
| 2022 | DAG.setRoot(BrCond); |
| 2023 | return true; |
| 2024 | } |
| 2025 | } |
| 2026 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2027 | |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2028 | // 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] | 2029 | uint32_t UnhandledWeights = 0; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2030 | if (BPI) { |
| 2031 | 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] | 2032 | uint32_t IWeight = I->ExtraWeight; |
| 2033 | UnhandledWeights += IWeight; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2034 | for (CaseItr J = CR.Range.first; J < I; ++J) { |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2035 | uint32_t JWeight = J->ExtraWeight; |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2036 | if (IWeight > JWeight) |
| 2037 | std::swap(*I, *J); |
| 2038 | } |
| 2039 | } |
| 2040 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2041 | // 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] | 2042 | Case &BackCase = *(CR.Range.second-1); |
Benjamin Kramer | 5db954d | 2012-05-26 21:19:12 +0000 | [diff] [blame] | 2043 | if (Size > 1 && |
| 2044 | NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2045 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 2046 | // branches in this order. See if rearranging a case value would help. |
Benjamin Kramer | c511b2a | 2012-05-26 20:01:32 +0000 | [diff] [blame] | 2047 | // We start at the bottom as it's the case with the least weight. |
Benjamin Kramer | cf1d69d | 2012-05-27 10:56:55 +0000 | [diff] [blame] | 2048 | 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] | 2049 | if (I->BB == NextBlock) { |
| 2050 | std::swap(*I, BackCase); |
| 2051 | break; |
| 2052 | } |
| 2053 | } |
| 2054 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2055 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2056 | // Create a CaseBlock record representing a conditional branch to |
| 2057 | // the Case's target mbb if the value being switched on SV is equal |
| 2058 | // to C. |
| 2059 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 2060 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 2061 | MachineBasicBlock *FallThrough; |
| 2062 | if (I != E-1) { |
| 2063 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 2064 | CurMF->insert(BBI, FallThrough); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2065 | |
| 2066 | // Put SV in a virtual register to make it available from the new blocks. |
| 2067 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2068 | } else { |
| 2069 | // If the last case doesn't match, go to the default block. |
| 2070 | FallThrough = Default; |
| 2071 | } |
| 2072 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2073 | const Value *RHS, *LHS, *MHS; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2074 | ISD::CondCode CC; |
| 2075 | if (I->High == I->Low) { |
| 2076 | // This is just small small case range :) containing exactly 1 case |
| 2077 | CC = ISD::SETEQ; |
| 2078 | LHS = SV; RHS = I->High; MHS = NULL; |
| 2079 | } else { |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2080 | CC = ISD::SETCC_INVALID; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2081 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 2082 | } |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2083 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2084 | // The false weight should be sum of all un-handled cases. |
| 2085 | UnhandledWeights -= I->ExtraWeight; |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2086 | CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough, |
| 2087 | /* me */ CurBlock, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2088 | /* trueweight */ I->ExtraWeight, |
| 2089 | /* falseweight */ UnhandledWeights); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2090 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2091 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 2092 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 2093 | // vector to be later processed by SDISel, and insert the node's MBB |
| 2094 | // before the next MBB. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2095 | if (CurBlock == SwitchBB) |
| 2096 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2097 | else |
| 2098 | SwitchCases.push_back(CB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2099 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2100 | CurBlock = FallThrough; |
| 2101 | } |
| 2102 | |
| 2103 | return true; |
| 2104 | } |
| 2105 | |
| 2106 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 2107 | return TLI.supportJumpTables() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2108 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 2109 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2110 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2111 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2112 | static APInt ComputeRange(const APInt &First, const APInt &Last) { |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2113 | uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1; |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2114 | APInt LastExt = Last.zext(BitWidth), FirstExt = First.zext(BitWidth); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2115 | return (LastExt - FirstExt + 1ULL); |
| 2116 | } |
| 2117 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2118 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Chris Lattner | c3ab388 | 2011-09-09 22:06:59 +0000 | [diff] [blame] | 2119 | bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR, |
| 2120 | CaseRecVector &WorkList, |
| 2121 | const Value *SV, |
| 2122 | MachineBasicBlock *Default, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2123 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2124 | Case& FrontCase = *CR.Range.first; |
| 2125 | Case& BackCase = *(CR.Range.second-1); |
| 2126 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2127 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2128 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2129 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2130 | APInt TSize(First.getBitWidth(), 0); |
Chris Lattner | c3ab388 | 2011-09-09 22:06:59 +0000 | [diff] [blame] | 2131 | 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] | 2132 | TSize += I->size(); |
| 2133 | |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2134 | if (!areJTsAllowed(TLI) || TSize.ult(TLI.getMinimumJumpTableEntries())) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2135 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2136 | |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2137 | APInt Range = ComputeRange(First, Last); |
Jakob Stoklund Olesen | 7944391 | 2011-10-26 01:47:48 +0000 | [diff] [blame] | 2138 | // The density is TSize / Range. Require at least 40%. |
| 2139 | // It should not be possible for IntTSize to saturate for sane code, but make |
| 2140 | // sure we handle Range saturation correctly. |
| 2141 | uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10); |
| 2142 | uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10); |
| 2143 | if (IntTSize * 10 < IntRange * 4) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2144 | return false; |
| 2145 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2146 | DEBUG(dbgs() << "Lowering jump table\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2147 | << "First entry: " << First << ". Last entry: " << Last << '\n' |
Jakob Stoklund Olesen | 7944391 | 2011-10-26 01:47:48 +0000 | [diff] [blame] | 2148 | << "Range: " << Range << ". Size: " << TSize << ".\n\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2149 | |
| 2150 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2151 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2152 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2153 | |
| 2154 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2155 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 2156 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2157 | |
| 2158 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2159 | |
| 2160 | // Create a new basic block to hold the code for loading the address |
| 2161 | // of the jump table, and jumping to it. Update successor information; |
| 2162 | // we will either branch to the default case for the switch, or the jump |
| 2163 | // table. |
| 2164 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2165 | CurMF->insert(BBI, JumpTableBB); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 2166 | |
| 2167 | addSuccessorWithWeight(CR.CaseBB, Default); |
| 2168 | addSuccessorWithWeight(CR.CaseBB, JumpTableBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2169 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2170 | // Build a vector of destination BBs, corresponding to each target |
| 2171 | // of the jump table. If the value of the jump table slot corresponds to |
| 2172 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 2173 | // the default BB. |
| 2174 | std::vector<MachineBasicBlock*> DestBBs; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2175 | APInt TEI = First; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2176 | 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] | 2177 | const APInt &Low = cast<ConstantInt>(I->Low)->getValue(); |
| 2178 | const APInt &High = cast<ConstantInt>(I->High)->getValue(); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2179 | |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2180 | if (Low.ule(TEI) && TEI.ule(High)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2181 | DestBBs.push_back(I->BB); |
| 2182 | if (TEI==High) |
| 2183 | ++I; |
| 2184 | } else { |
| 2185 | DestBBs.push_back(Default); |
| 2186 | } |
| 2187 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2188 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2189 | // Calculate weight for each unique destination in CR. |
| 2190 | DenseMap<MachineBasicBlock*, uint32_t> DestWeights; |
| 2191 | if (FuncInfo.BPI) |
| 2192 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 2193 | DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr = |
| 2194 | DestWeights.find(I->BB); |
| 2195 | if (Itr != DestWeights.end()) |
| 2196 | Itr->second += I->ExtraWeight; |
| 2197 | else |
| 2198 | DestWeights[I->BB] = I->ExtraWeight; |
| 2199 | } |
| 2200 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2201 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2202 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 2203 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2204 | E = DestBBs.end(); I != E; ++I) { |
| 2205 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 2206 | SuccsHandled[(*I)->getNumber()] = true; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2207 | DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr = |
| 2208 | DestWeights.find(*I); |
| 2209 | addSuccessorWithWeight(JumpTableBB, *I, |
| 2210 | Itr != DestWeights.end() ? Itr->second : 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2211 | } |
| 2212 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2213 | |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 2214 | // Create a jump table index for this jump table. |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 2215 | unsigned JTEncoding = TLI.getJumpTableEncoding(); |
| 2216 | unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding) |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 2217 | ->createJumpTableIndex(DestBBs); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2218 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2219 | // Set the jump table information so that we can codegen it as a second |
| 2220 | // MachineBasicBlock |
| 2221 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2222 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB)); |
| 2223 | if (CR.CaseBB == SwitchBB) |
| 2224 | visitJumpTableHeader(JT, JTH, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2225 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2226 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2227 | return true; |
| 2228 | } |
| 2229 | |
| 2230 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 2231 | /// 2 subtrees. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2232 | bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR, |
| 2233 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2234 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2235 | MachineBasicBlock *Default, |
| 2236 | MachineBasicBlock *SwitchBB) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2237 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2238 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2239 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2240 | |
| 2241 | // Figure out which block is immediately after the current one. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2242 | MachineFunction::iterator BBI = CR.CaseBB; |
Duncan Sands | 5149852 | 2009-09-06 18:03:32 +0000 | [diff] [blame] | 2243 | ++BBI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2244 | |
| 2245 | Case& FrontCase = *CR.Range.first; |
| 2246 | Case& BackCase = *(CR.Range.second-1); |
| 2247 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2248 | |
| 2249 | // Size is the number of Cases represented by this range. |
| 2250 | unsigned Size = CR.Range.second - CR.Range.first; |
| 2251 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2252 | const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2253 | const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2254 | double FMetric = 0; |
| 2255 | CaseItr Pivot = CR.Range.first + Size/2; |
| 2256 | |
| 2257 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 2258 | // (heuristically) allow us to emit JumpTable's later. |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2259 | APInt TSize(First.getBitWidth(), 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2260 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2261 | I!=E; ++I) |
| 2262 | TSize += I->size(); |
| 2263 | |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2264 | APInt LSize = FrontCase.size(); |
| 2265 | APInt RSize = TSize-LSize; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2266 | DEBUG(dbgs() << "Selecting best pivot: \n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2267 | << "First: " << First << ", Last: " << Last <<'\n' |
| 2268 | << "LSize: " << LSize << ", RSize: " << RSize << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2269 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 2270 | J!=E; ++I, ++J) { |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2271 | const APInt &LEnd = cast<ConstantInt>(I->High)->getValue(); |
| 2272 | const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2273 | APInt Range = ComputeRange(LEnd, RBegin); |
Stepan Dyatkovskiy | c2c52a6 | 2012-05-15 06:50:18 +0000 | [diff] [blame] | 2274 | assert((Range - 2ULL).isNonNegative() && |
| 2275 | "Invalid case distance"); |
Chris Lattner | c3e4e59 | 2011-04-09 06:57:13 +0000 | [diff] [blame] | 2276 | // Use volatile double here to avoid excess precision issues on some hosts, |
| 2277 | // e.g. that use 80-bit X87 registers. |
| 2278 | volatile double LDensity = |
| 2279 | (double)LSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2280 | (LEnd - First + 1ULL).roundToDouble(); |
Chris Lattner | c3e4e59 | 2011-04-09 06:57:13 +0000 | [diff] [blame] | 2281 | volatile double RDensity = |
| 2282 | (double)RSize.roundToDouble() / |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 2283 | (Last - RBegin + 1ULL).roundToDouble(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2284 | double Metric = Range.logBase2()*(LDensity+RDensity); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2285 | // Should always split in some non-trivial place |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2286 | DEBUG(dbgs() <<"=>Step\n" |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2287 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n' |
| 2288 | << "LDensity: " << LDensity |
| 2289 | << ", RDensity: " << RDensity << '\n' |
| 2290 | << "Metric: " << Metric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2291 | if (FMetric < Metric) { |
| 2292 | Pivot = J; |
| 2293 | FMetric = Metric; |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2294 | DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | LSize += J->size(); |
| 2298 | RSize -= J->size(); |
| 2299 | } |
| 2300 | if (areJTsAllowed(TLI)) { |
| 2301 | // If our case is dense we *really* should handle it earlier! |
| 2302 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 2303 | } else { |
| 2304 | Pivot = CR.Range.first + Size/2; |
| 2305 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2306 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2307 | CaseRange LHSR(CR.Range.first, Pivot); |
| 2308 | CaseRange RHSR(Pivot, CR.Range.second); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 2309 | const Constant *C = Pivot->Low; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2310 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2311 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2312 | // We know that we branch to the LHS if the Value being switched on is |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2313 | // less than the Pivot value, C. We use this to optimize our binary |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2314 | // tree a bit, by recognizing that if SV is greater than or equal to the |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2315 | // LHS's Case Value, and that Case Value is exactly one less than the |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2316 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 2317 | // rather than creating a leaf node for it. |
| 2318 | if ((LHSR.second - LHSR.first) == 1 && |
| 2319 | LHSR.first->High == CR.GE && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2320 | cast<ConstantInt>(C)->getValue() == |
| 2321 | (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2322 | TrueBB = LHSR.first->BB; |
| 2323 | } else { |
| 2324 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2325 | CurMF->insert(BBI, TrueBB); |
| 2326 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2327 | |
| 2328 | // Put SV in a virtual register to make it available from the new blocks. |
| 2329 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2330 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2331 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2332 | // Similar to the optimization above, if the Value being switched on is |
| 2333 | // known to be less than the Constant CR.LT, and the current Case Value |
| 2334 | // is CR.LT - 1, then we can branch directly to the target block for |
| 2335 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 2336 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2337 | cast<ConstantInt>(RHSR.first->Low)->getValue() == |
| 2338 | (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2339 | FalseBB = RHSR.first->BB; |
| 2340 | } else { |
| 2341 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2342 | CurMF->insert(BBI, FalseBB); |
| 2343 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2344 | |
| 2345 | // Put SV in a virtual register to make it available from the new blocks. |
| 2346 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | // Create a CaseBlock record representing a conditional branch to |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2350 | // the LHS node if the value being switched on SV is less than C. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2351 | // Otherwise, branch to LHS. |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2352 | CaseBlock CB(ISD::SETULT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2353 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2354 | if (CR.CaseBB == SwitchBB) |
| 2355 | visitSwitchCase(CB, SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2356 | else |
| 2357 | SwitchCases.push_back(CB); |
| 2358 | |
| 2359 | return true; |
| 2360 | } |
| 2361 | |
| 2362 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 2363 | /// range span less, than machine word bitwidth, encode case range into series |
| 2364 | /// of masks and emit bit tests with these masks. |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2365 | bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR, |
| 2366 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2367 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2368 | MachineBasicBlock* Default, |
| 2369 | MachineBasicBlock *SwitchBB){ |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2370 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 2371 | unsigned IntPtrBits = PTy.getSizeInBits(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2372 | |
| 2373 | Case& FrontCase = *CR.Range.first; |
| 2374 | Case& BackCase = *(CR.Range.second-1); |
| 2375 | |
| 2376 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2377 | // inserting any additional MBBs necessary to represent the switch. |
Dan Gohman | 0d24bfb | 2009-08-15 02:06:22 +0000 | [diff] [blame] | 2378 | MachineFunction *CurMF = FuncInfo.MF; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2379 | |
Anton Korobeynikov | d34167a | 2009-05-08 18:51:34 +0000 | [diff] [blame] | 2380 | // If target does not have legal shift left, do not emit bit tests at all. |
| 2381 | if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy())) |
| 2382 | return false; |
| 2383 | |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2384 | size_t numCmps = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2385 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2386 | I!=E; ++I) { |
| 2387 | // Single case counts one, case range - two. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2388 | numCmps += (I->Low == I->High ? 1 : 2); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2389 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2390 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2391 | // Count unique destinations |
| 2392 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 2393 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2394 | Dests.insert(I->BB); |
| 2395 | if (Dests.size() > 3) |
| 2396 | // Don't bother the code below, if there are too much unique destinations |
| 2397 | return false; |
| 2398 | } |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2399 | DEBUG(dbgs() << "Total number of unique destinations: " |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2400 | << Dests.size() << '\n' |
| 2401 | << "Total number of comparisons: " << numCmps << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2402 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2403 | // Compute span of values. |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2404 | const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue(); |
| 2405 | const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue(); |
Anton Korobeynikov | e2f95e9 | 2008-12-23 22:26:01 +0000 | [diff] [blame] | 2406 | APInt cmpRange = maxValue - minValue; |
| 2407 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2408 | DEBUG(dbgs() << "Compare range: " << cmpRange << '\n' |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2409 | << "Low bound: " << minValue << '\n' |
| 2410 | << "High bound: " << maxValue << '\n'); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2411 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 2412 | if (cmpRange.uge(IntPtrBits) || |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2413 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 2414 | !(Dests.size() == 2 && numCmps >= 5) && |
| 2415 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 2416 | return false; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2417 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2418 | DEBUG(dbgs() << "Emitting bit tests\n"); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2419 | APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth()); |
| 2420 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2421 | // Optimize the case where all the case values fit in a |
| 2422 | // word without having to subtract minValue. In this case, |
| 2423 | // we can optimize away the subtraction. |
Stepan Dyatkovskiy | c187df2 | 2012-05-17 08:56:30 +0000 | [diff] [blame] | 2424 | if (maxValue.ult(IntPtrBits)) { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2425 | cmpRange = maxValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2426 | } else { |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2427 | lowBound = minValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2428 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2429 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2430 | CaseBitsVector CasesBits; |
| 2431 | unsigned i, count = 0; |
| 2432 | |
| 2433 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2434 | MachineBasicBlock* Dest = I->BB; |
| 2435 | for (i = 0; i < count; ++i) |
| 2436 | if (Dest == CasesBits[i].BB) |
| 2437 | break; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2438 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2439 | if (i == count) { |
| 2440 | assert((count < 3) && "Too much destinations to test!"); |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2441 | CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2442 | count++; |
| 2443 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2444 | |
| 2445 | const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue(); |
| 2446 | const APInt& highValue = cast<ConstantInt>(I->High)->getValue(); |
| 2447 | |
| 2448 | uint64_t lo = (lowValue - lowBound).getZExtValue(); |
| 2449 | uint64_t hi = (highValue - lowBound).getZExtValue(); |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2450 | CasesBits[i].ExtraWeight += I->ExtraWeight; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2451 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2452 | for (uint64_t j = lo; j <= hi; j++) { |
| 2453 | CasesBits[i].Mask |= 1ULL << j; |
| 2454 | CasesBits[i].Bits++; |
| 2455 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2456 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2457 | } |
| 2458 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2459 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2460 | BitTestInfo BTC; |
| 2461 | |
| 2462 | // Figure out which block is immediately after the current one. |
| 2463 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2464 | ++BBI; |
| 2465 | |
| 2466 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2467 | |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2468 | DEBUG(dbgs() << "Cases:\n"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2469 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2470 | DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2471 | << ", Bits: " << CasesBits[i].Bits |
| 2472 | << ", BB: " << CasesBits[i].BB << '\n'); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2473 | |
| 2474 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 2475 | CurMF->insert(BBI, CaseBB); |
| 2476 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 2477 | CaseBB, |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2478 | CasesBits[i].BB, CasesBits[i].ExtraWeight)); |
Dan Gohman | 8e5c0da | 2009-04-09 02:33:36 +0000 | [diff] [blame] | 2479 | |
| 2480 | // Put SV in a virtual register to make it available from the new blocks. |
| 2481 | ExportFromCurrentBlock(SV); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2482 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2483 | |
| 2484 | BitTestBlock BTB(lowBound, cmpRange, SV, |
Evan Cheng | d08e5b4 | 2011-01-06 01:02:44 +0000 | [diff] [blame] | 2485 | -1U, MVT::Other, (CR.CaseBB == SwitchBB), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2486 | CR.CaseBB, Default, BTC); |
| 2487 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2488 | if (CR.CaseBB == SwitchBB) |
| 2489 | visitBitTestHeader(BTB, SwitchBB); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2490 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2491 | BitTestCases.push_back(BTB); |
| 2492 | |
| 2493 | return true; |
| 2494 | } |
| 2495 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2496 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 2497 | size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, |
| 2498 | const SwitchInst& SI) { |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2499 | |
| 2500 | /// Use a shorter form of declaration, and also |
| 2501 | /// show the we want to use CRSBuilder as Clusterifier. |
Stepan Dyatkovskiy | 4319a55 | 2012-06-02 07:26:00 +0000 | [diff] [blame] | 2502 | typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier; |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2503 | |
| 2504 | Clusterifier TheClusterifier; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2505 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2506 | BranchProbabilityInfo *BPI = FuncInfo.BPI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2507 | // Start with "simple" cases |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 2508 | for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 2509 | i != e; ++i) { |
| 2510 | const BasicBlock *SuccBB = i.getCaseSuccessor(); |
Jakub Staszak | c8f34de | 2011-07-29 22:25:21 +0000 | [diff] [blame] | 2511 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB]; |
| 2512 | |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2513 | TheClusterifier.add(i.getCaseValueEx(), SMBB, |
| 2514 | BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2515 | } |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2516 | |
| 2517 | TheClusterifier.optimize(); |
| 2518 | |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2519 | size_t numCmps = 0; |
| 2520 | for (Clusterifier::RangeIterator i = TheClusterifier.begin(), |
| 2521 | e = TheClusterifier.end(); i != e; ++i, ++numCmps) { |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2522 | Clusterifier::Cluster &C = *i; |
Manman Ren | 1a710fd | 2012-08-24 18:14:27 +0000 | [diff] [blame] | 2523 | // Update edge weight for the cluster. |
| 2524 | unsigned W = C.first.Weight; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2525 | |
Stepan Dyatkovskiy | 484fc93 | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 2526 | // FIXME: Currently work with ConstantInt based numbers. |
| 2527 | // Changing it to APInt based is a pretty heavy for this commit. |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2528 | Cases.push_back(Case(C.first.getLow().toConstantInt(), |
| 2529 | C.first.getHigh().toConstantInt(), C.second, W)); |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2530 | |
Stepan Dyatkovskiy | 66d79ce | 2012-07-04 05:53:05 +0000 | [diff] [blame] | 2531 | if (C.first.getLow() != C.first.getHigh()) |
Stepan Dyatkovskiy | 05cfe2e | 2012-05-18 08:32:28 +0000 | [diff] [blame] | 2532 | // A range counts double, since it requires two compares. |
| 2533 | ++numCmps; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | return numCmps; |
| 2537 | } |
| 2538 | |
Jakob Stoklund Olesen | 2622f46 | 2010-09-30 19:44:31 +0000 | [diff] [blame] | 2539 | void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First, |
| 2540 | MachineBasicBlock *Last) { |
| 2541 | // Update JTCases. |
| 2542 | for (unsigned i = 0, e = JTCases.size(); i != e; ++i) |
| 2543 | if (JTCases[i].first.HeaderBB == First) |
| 2544 | JTCases[i].first.HeaderBB = Last; |
| 2545 | |
| 2546 | // Update BitTestCases. |
| 2547 | for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i) |
| 2548 | if (BitTestCases[i].Parent == First) |
| 2549 | BitTestCases[i].Parent = Last; |
| 2550 | } |
| 2551 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2552 | void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 2553 | MachineBasicBlock *SwitchMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2554 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2555 | // Figure out which block is immediately after the current one. |
| 2556 | MachineBasicBlock *NextBlock = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2557 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 2558 | |
| 2559 | // If there is only the default destination, branch to it if it is not the |
| 2560 | // next basic block. Otherwise, just fall through. |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 2561 | if (!SI.getNumCases()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2562 | // Update machine-CFG edges. |
| 2563 | |
| 2564 | // If this is not a fall-through branch, emit the branch. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2565 | SwitchMBB->addSuccessor(Default); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2566 | if (Default != NextBlock) |
| 2567 | DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), |
| 2568 | MVT::Other, getControlRoot(), |
| 2569 | DAG.getBasicBlock(Default))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2570 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2571 | return; |
| 2572 | } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2573 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2574 | // If there are any non-default case statements, create a vector of Cases |
| 2575 | // representing each one, and sort the vector so that we can efficiently |
| 2576 | // create a binary search tree from them. |
| 2577 | CaseVector Cases; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2578 | size_t numCmps = Clusterify(Cases, SI); |
David Greene | 4b69d99 | 2010-01-05 01:24:57 +0000 | [diff] [blame] | 2579 | DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size() |
Anton Korobeynikov | 56d245b | 2008-12-23 22:26:18 +0000 | [diff] [blame] | 2580 | << ". Total compares: " << numCmps << '\n'); |
Duncan Sands | 17001ce | 2011-10-18 12:44:00 +0000 | [diff] [blame] | 2581 | (void)numCmps; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2582 | |
| 2583 | // Get the Value to be switched on and default basic blocks, which will be |
| 2584 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2585 | // search tree. |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 2586 | const Value *SV = SI.getCondition(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Push the initial CaseRec onto the worklist |
| 2589 | CaseRecVector WorkList; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2590 | WorkList.push_back(CaseRec(SwitchMBB,0,0, |
| 2591 | CaseRange(Cases.begin(),Cases.end()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2592 | |
| 2593 | while (!WorkList.empty()) { |
| 2594 | // Grab a record representing a case range to process off the worklist |
| 2595 | CaseRec CR = WorkList.back(); |
| 2596 | WorkList.pop_back(); |
| 2597 | |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2598 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2599 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2600 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2601 | // If the range has few cases (two or less) emit a series of specific |
| 2602 | // tests. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2603 | if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2604 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2605 | |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2606 | // 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] | 2607 | // target supports indirect branches, then emit a jump table rather than |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2608 | // lowering the switch to a binary tree of conditional branches. |
Sebastian Pop | 1a37d7e | 2012-09-25 20:35:36 +0000 | [diff] [blame] | 2609 | // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries(). |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2610 | if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2611 | continue; |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 2612 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2613 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2614 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2615 | handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2616 | } |
| 2617 | } |
| 2618 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2619 | void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 2620 | MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB; |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 2621 | |
Jakob Stoklund Olesen | 598b24c | 2010-02-11 00:34:18 +0000 | [diff] [blame] | 2622 | // Update machine-CFG edges with unique successors. |
Nadav Rotem | ee0ce15 | 2012-10-23 21:05:33 +0000 | [diff] [blame] | 2623 | SmallSet<BasicBlock*, 32> Done; |
| 2624 | for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) { |
| 2625 | BasicBlock *BB = I.getSuccessor(i); |
| 2626 | bool Inserted = Done.insert(BB); |
| 2627 | if (!Inserted) |
| 2628 | continue; |
| 2629 | |
| 2630 | MachineBasicBlock *Succ = FuncInfo.MBBMap[BB]; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 2631 | addSuccessorWithWeight(IndirectBrMBB, Succ); |
| 2632 | } |
Dan Gohman | eef55dc | 2009-10-27 22:10:34 +0000 | [diff] [blame] | 2633 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2634 | DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(), |
| 2635 | MVT::Other, getControlRoot(), |
| 2636 | getValue(I.getAddress()))); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2637 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2638 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2639 | void SelectionDAGBuilder::visitFSub(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2640 | // -0.0 - X --> fneg |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2641 | Type *Ty = I.getType(); |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2642 | if (isa<Constant>(I.getOperand(0)) && |
| 2643 | I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) { |
| 2644 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2645 | setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(), |
| 2646 | Op2.getValueType(), Op2)); |
| 2647 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2648 | } |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2649 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2650 | visitBinary(I, ISD::FSUB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2653 | void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2654 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2655 | SDValue Op2 = getValue(I.getOperand(1)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2656 | setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(), |
| 2657 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2660 | void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2661 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2662 | SDValue Op2 = getValue(I.getOperand(1)); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2663 | |
| 2664 | MVT ShiftTy = TLI.getShiftAmountTy(Op2.getValueType()); |
| 2665 | |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2666 | // Coerce the shift amount to the right type if we can. |
| 2667 | if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) { |
Chris Lattner | 915eeb4 | 2011-02-13 09:10:56 +0000 | [diff] [blame] | 2668 | unsigned ShiftSize = ShiftTy.getSizeInBits(); |
| 2669 | unsigned Op2Size = Op2.getValueType().getSizeInBits(); |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2670 | DebugLoc DL = getCurDebugLoc(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2671 | |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2672 | // If the operand is smaller than the shift count type, promote it. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2673 | if (ShiftSize > Op2Size) |
| 2674 | Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2675 | |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 2676 | // If the operand is larger than the shift count type but the shift |
| 2677 | // count type has enough bits to represent any shift value, truncate |
| 2678 | // it now. This is a common case and it exposes the truncate to |
| 2679 | // optimization early. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2680 | else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits())) |
| 2681 | Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2); |
| 2682 | // Otherwise we'll need to temporarily settle for some other convenient |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 2683 | // type. Type legalization will make adjustments once the shiftee is split. |
Chris Lattner | d302773 | 2011-02-13 09:02:52 +0000 | [diff] [blame] | 2684 | else |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 2685 | Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2686 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 2687 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2688 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), |
| 2689 | Op1.getValueType(), Op1, Op2)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2692 | void SelectionDAGBuilder::visitSDiv(const User &I) { |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2693 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2694 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2695 | |
| 2696 | // Turn exact SDivs into multiplications. |
| 2697 | // FIXME: This should be in DAGCombiner, but it doesn't have access to the |
| 2698 | // exact bit. |
Benjamin Kramer | 3492a4a | 2011-07-08 12:08:24 +0000 | [diff] [blame] | 2699 | if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() && |
| 2700 | !isa<ConstantSDNode>(Op1) && |
Benjamin Kramer | 9c64030 | 2011-07-08 10:31:30 +0000 | [diff] [blame] | 2701 | isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue()) |
| 2702 | setValue(&I, TLI.BuildExactSDIV(Op1, Op2, getCurDebugLoc(), DAG)); |
| 2703 | else |
| 2704 | setValue(&I, DAG.getNode(ISD::SDIV, getCurDebugLoc(), Op1.getValueType(), |
| 2705 | Op1, Op2)); |
| 2706 | } |
| 2707 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2708 | void SelectionDAGBuilder::visitICmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2709 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2710 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2711 | predicate = IC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2712 | else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2713 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2714 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2715 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2716 | ISD::CondCode Opcode = getICmpCondCode(predicate); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2717 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2718 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2719 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2722 | void SelectionDAGBuilder::visitFCmp(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2723 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2724 | if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2725 | predicate = FC->getPredicate(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2726 | else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2727 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2728 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2729 | SDValue Op2 = getValue(I.getOperand(1)); |
Dan Gohman | 8c1a6ca | 2008-10-17 18:18:45 +0000 | [diff] [blame] | 2730 | ISD::CondCode Condition = getFCmpCondCode(predicate); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2731 | if (TM.Options.NoNaNsFPMath) |
| 2732 | Condition = getFCmpCodeWithoutNaN(Condition); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2733 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2734 | setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2737 | void SelectionDAGBuilder::visitSelect(const User &I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2738 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2739 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
| 2740 | unsigned NumValues = ValueVTs.size(); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2741 | if (NumValues == 0) return; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2742 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2743 | SmallVector<SDValue, 4> Values(NumValues); |
| 2744 | SDValue Cond = getValue(I.getOperand(0)); |
| 2745 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2746 | SDValue FalseVal = getValue(I.getOperand(2)); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 2747 | ISD::NodeType OpCode = Cond.getValueType().isVector() ? |
| 2748 | ISD::VSELECT : ISD::SELECT; |
Dan Gohman | 7ea1ca6 | 2008-10-21 20:00:42 +0000 | [diff] [blame] | 2749 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2750 | for (unsigned i = 0; i != NumValues; ++i) |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 2751 | Values[i] = DAG.getNode(OpCode, getCurDebugLoc(), |
| 2752 | TrueVal.getNode()->getValueType(TrueVal.getResNo()+i), |
Chris Lattner | b3e87b2 | 2010-03-12 07:15:36 +0000 | [diff] [blame] | 2753 | Cond, |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2754 | SDValue(TrueVal.getNode(), |
| 2755 | TrueVal.getResNo() + i), |
| 2756 | SDValue(FalseVal.getNode(), |
| 2757 | FalseVal.getResNo() + i)); |
| 2758 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2759 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 2760 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 2761 | &Values[0], NumValues)); |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2762 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2763 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2764 | void SelectionDAGBuilder::visitTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2765 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2766 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2767 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2768 | setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2771 | void SelectionDAGBuilder::visitZExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2772 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2773 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2774 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2775 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2776 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2779 | void SelectionDAGBuilder::visitSExt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2780 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2781 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2782 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2783 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2784 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2787 | void SelectionDAGBuilder::visitFPTrunc(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2788 | // FPTrunc is never a no-op cast, no need to check |
| 2789 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2790 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2791 | setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(), |
Pete Cooper | f57e1c2 | 2012-01-17 01:54:07 +0000 | [diff] [blame] | 2792 | DestVT, N, |
| 2793 | DAG.getTargetConstant(0, TLI.getPointerTy()))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2796 | void SelectionDAGBuilder::visitFPExt(const User &I){ |
Hal Finkel | 46bb70c | 2011-10-18 03:51:57 +0000 | [diff] [blame] | 2797 | // FPExt is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2798 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2799 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2800 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2803 | void SelectionDAGBuilder::visitFPToUI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2804 | // FPToUI is never a no-op cast, no need to check |
| 2805 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2806 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2807 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2810 | void SelectionDAGBuilder::visitFPToSI(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2811 | // FPToSI is never a no-op cast, no need to check |
| 2812 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2813 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2814 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2817 | void SelectionDAGBuilder::visitUIToFP(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2818 | // UIToFP is never a no-op cast, no need to check |
| 2819 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2820 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2821 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2824 | void SelectionDAGBuilder::visitSIToFP(const User &I){ |
Bill Wendling | 181b627 | 2008-10-19 20:34:04 +0000 | [diff] [blame] | 2825 | // SIToFP is never a no-op cast, no need to check |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2826 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2827 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2828 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2831 | void SelectionDAGBuilder::visitPtrToInt(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2832 | // What to do depends on the size of the integer and the size of the pointer. |
| 2833 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2834 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2835 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2836 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2839 | void SelectionDAGBuilder::visitIntToPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2840 | // What to do depends on the size of the integer and the size of the pointer. |
| 2841 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2842 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2843 | EVT DestVT = TLI.getValueType(I.getType()); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2844 | setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2847 | void SelectionDAGBuilder::visitBitCast(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2848 | SDValue N = getValue(I.getOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2849 | EVT DestVT = TLI.getValueType(I.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2850 | |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2851 | // 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] | 2852 | // either a BITCAST or a no-op. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2853 | if (DestVT != N.getValueType()) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2854 | setValue(&I, DAG.getNode(ISD::BITCAST, getCurDebugLoc(), |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2855 | DestVT, N)); // convert types. |
| 2856 | else |
Bill Wendling | 49fcff8 | 2009-12-21 22:30:11 +0000 | [diff] [blame] | 2857 | setValue(&I, N); // noop cast. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2860 | void SelectionDAGBuilder::visitInsertElement(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2861 | SDValue InVec = getValue(I.getOperand(0)); |
| 2862 | SDValue InVal = getValue(I.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2863 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2864 | TLI.getPointerTy(), |
| 2865 | getValue(I.getOperand(2))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2866 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(), |
| 2867 | TLI.getValueType(I.getType()), |
| 2868 | InVec, InVal, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2871 | void SelectionDAGBuilder::visitExtractElement(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2872 | SDValue InVec = getValue(I.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2873 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 2874 | TLI.getPointerTy(), |
| 2875 | getValue(I.getOperand(1))); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2876 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 2877 | TLI.getValueType(I.getType()), InVec, InIdx)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2880 | // Utility for visitShuffleVector - Return true if every element in Mask, |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 2881 | // beginning from position Pos and ending in Pos+Size, falls within the |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2882 | // specified sequential range [L, L+Pos). or is undef. |
| 2883 | static bool isSequentialInRange(const SmallVectorImpl<int> &Mask, |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 2884 | unsigned Pos, unsigned Size, int Low) { |
| 2885 | for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low) |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2886 | if (Mask[i] >= 0 && Mask[i] != Low) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2887 | return false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2888 | return true; |
| 2889 | } |
| 2890 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2891 | void SelectionDAGBuilder::visitShuffleVector(const User &I) { |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2892 | SDValue Src1 = getValue(I.getOperand(0)); |
| 2893 | SDValue Src2 = getValue(I.getOperand(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2894 | |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 2895 | SmallVector<int, 8> Mask; |
| 2896 | ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask); |
| 2897 | unsigned MaskNumElts = Mask.size(); |
| 2898 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2899 | EVT VT = TLI.getValueType(I.getType()); |
| 2900 | EVT SrcVT = Src1.getValueType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2901 | unsigned SrcNumElts = SrcVT.getVectorNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2902 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2903 | if (SrcNumElts == MaskNumElts) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2904 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2905 | &Mask[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2906 | return; |
| 2907 | } |
| 2908 | |
| 2909 | // 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] | 2910 | if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) { |
| 2911 | // Mask is longer than the source vectors and is a multiple of the source |
| 2912 | // 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] | 2913 | // lengths match. |
Craig Topper | 5157834 | 2012-01-04 09:23:09 +0000 | [diff] [blame] | 2914 | if (SrcNumElts*2 == MaskNumElts) { |
| 2915 | // First check for Src1 in low and Src2 in high |
| 2916 | if (isSequentialInRange(Mask, 0, SrcNumElts, 0) && |
| 2917 | isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) { |
| 2918 | // The shuffle is concatenating two vectors together. |
| 2919 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
| 2920 | VT, Src1, Src2)); |
| 2921 | return; |
| 2922 | } |
| 2923 | // Then check for Src2 in low and Src1 in high |
| 2924 | if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) && |
| 2925 | isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) { |
| 2926 | // The shuffle is concatenating two vectors together. |
| 2927 | setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(), |
| 2928 | VT, Src2, Src1)); |
| 2929 | return; |
| 2930 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2933 | // Pad both vectors with undefs to make them the same length as the mask. |
| 2934 | unsigned NumConcat = MaskNumElts / SrcNumElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2935 | bool Src1U = Src1.getOpcode() == ISD::UNDEF; |
| 2936 | bool Src2U = Src2.getOpcode() == ISD::UNDEF; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2937 | SDValue UndefVal = DAG.getUNDEF(SrcVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2938 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2939 | SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); |
| 2940 | SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2941 | MOps1[0] = Src1; |
| 2942 | MOps2[0] = Src2; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2943 | |
| 2944 | Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
| 2945 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2946 | &MOps1[0], NumConcat); |
| 2947 | Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS, |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2948 | getCurDebugLoc(), VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2949 | &MOps2[0], NumConcat); |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2950 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2951 | // Readjust mask for new input vector length. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2952 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2953 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2954 | int Idx = Mask[i]; |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 2955 | if (Idx >= (int)SrcNumElts) |
| 2956 | Idx -= SrcNumElts - MaskNumElts; |
| 2957 | MappedOps.push_back(Idx); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2958 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 2959 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 2960 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 2961 | &MappedOps[0])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2962 | return; |
| 2963 | } |
| 2964 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2965 | if (SrcNumElts > MaskNumElts) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2966 | // Analyze the access pattern of the vector to see if we can extract |
| 2967 | // two subvectors and do the shuffle. The analysis is done by calculating |
| 2968 | // the range of elements the mask access on both vectors. |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2969 | int MinRange[2] = { static_cast<int>(SrcNumElts), |
| 2970 | static_cast<int>(SrcNumElts)}; |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2971 | int MaxRange[2] = {-1, -1}; |
| 2972 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2973 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2974 | int Idx = Mask[i]; |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2975 | unsigned Input = 0; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2976 | if (Idx < 0) |
| 2977 | continue; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 2978 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2979 | if (Idx >= (int)SrcNumElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2980 | Input = 1; |
| 2981 | Idx -= SrcNumElts; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2982 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2983 | if (Idx > MaxRange[Input]) |
| 2984 | MaxRange[Input] = Idx; |
| 2985 | if (Idx < MinRange[Input]) |
| 2986 | MinRange[Input] = Idx; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2987 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2988 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2989 | // Check if the access is smaller than the vector size and can we find |
| 2990 | // a reasonable extract index. |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2991 | int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not |
| 2992 | // Extract. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2993 | int StartIdx[2]; // StartIdx to extract from |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 2994 | for (unsigned Input = 0; Input < 2; ++Input) { |
| 2995 | if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 2996 | RangeUse[Input] = 0; // Unused |
| 2997 | StartIdx[Input] = 0; |
Craig Topper | f873dde | 2012-04-08 17:53:33 +0000 | [diff] [blame] | 2998 | continue; |
Mon P Wang | 230e4fa | 2008-11-21 04:25:21 +0000 | [diff] [blame] | 2999 | } |
Craig Topper | f873dde | 2012-04-08 17:53:33 +0000 | [diff] [blame] | 3000 | |
| 3001 | // Find a good start index that is a multiple of the mask length. Then |
| 3002 | // see if the rest of the elements are in range. |
| 3003 | StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts; |
| 3004 | if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts && |
| 3005 | StartIdx[Input] + MaskNumElts <= SrcNumElts) |
| 3006 | RangeUse[Input] = 1; // Extract from a multiple of the mask length. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Bill Wendling | 636e258 | 2009-08-21 18:16:06 +0000 | [diff] [blame] | 3009 | if (RangeUse[0] == 0 && RangeUse[1] == 0) { |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3010 | setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3011 | return; |
| 3012 | } |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3013 | if (RangeUse[0] >= 0 && RangeUse[1] >= 0) { |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3014 | // Extract appropriate subvector and generate a vector shuffle |
Craig Topper | 10612dc | 2012-04-08 23:15:04 +0000 | [diff] [blame] | 3015 | for (unsigned Input = 0; Input < 2; ++Input) { |
Bill Wendling | 87710f0 | 2009-12-21 23:47:40 +0000 | [diff] [blame] | 3016 | SDValue &Src = Input == 0 ? Src1 : Src2; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3017 | if (RangeUse[Input] == 0) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3018 | Src = DAG.getUNDEF(VT); |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3019 | else |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3020 | Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3021 | Src, DAG.getIntPtrConstant(StartIdx[Input])); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3022 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3023 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3024 | // Calculate new mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3025 | SmallVector<int, 8> MappedOps; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3026 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3027 | int Idx = Mask[i]; |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3028 | if (Idx >= 0) { |
| 3029 | if (Idx < (int)SrcNumElts) |
| 3030 | Idx -= StartIdx[0]; |
| 3031 | else |
| 3032 | Idx -= SrcNumElts + StartIdx[1] - MaskNumElts; |
| 3033 | } |
| 3034 | MappedOps.push_back(Idx); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3035 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3036 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3037 | setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2, |
| 3038 | &MappedOps[0])); |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3039 | return; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3040 | } |
| 3041 | } |
| 3042 | |
Mon P Wang | c7849c2 | 2008-11-16 05:06:27 +0000 | [diff] [blame] | 3043 | // We can't use either concat vectors or extract subvectors so fall back to |
| 3044 | // replacing the shuffle with extract and build vector. |
| 3045 | // to insert and build vector. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3046 | EVT EltVT = VT.getVectorElementType(); |
| 3047 | EVT PtrVT = TLI.getPointerTy(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3048 | SmallVector<SDValue,8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 3049 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3050 | int Idx = Mask[i]; |
| 3051 | SDValue Res; |
| 3052 | |
| 3053 | if (Idx < 0) { |
| 3054 | Res = DAG.getUNDEF(EltVT); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3055 | } else { |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3056 | SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2; |
| 3057 | if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts; |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3058 | |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3059 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(), |
| 3060 | EltVT, Src, DAG.getConstant(Idx, PtrVT)); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3061 | } |
Craig Topper | 23de31b | 2012-04-11 03:06:35 +0000 | [diff] [blame] | 3062 | |
| 3063 | Ops.push_back(Res); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3064 | } |
Bill Wendling | b85b6e8 | 2009-12-21 22:42:14 +0000 | [diff] [blame] | 3065 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3066 | setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(), |
| 3067 | VT, &Ops[0], Ops.size())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3070 | void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3071 | const Value *Op0 = I.getOperand(0); |
| 3072 | const Value *Op1 = I.getOperand(1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3073 | Type *AggTy = I.getType(); |
| 3074 | Type *ValTy = Op1->getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3075 | bool IntoUndef = isa<UndefValue>(Op0); |
| 3076 | bool FromUndef = isa<UndefValue>(Op1); |
| 3077 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3078 | unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3079 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3080 | SmallVector<EVT, 4> AggValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3081 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3082 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3083 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 3084 | |
| 3085 | unsigned NumAggValues = AggValueVTs.size(); |
| 3086 | unsigned NumValValues = ValValueVTs.size(); |
| 3087 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 3088 | |
| 3089 | SDValue Agg = getValue(Op0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3090 | unsigned i = 0; |
| 3091 | // Copy the beginning value(s) from the original aggregate. |
| 3092 | for (; i != LinearIndex; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3093 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3094 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 3095 | // Copy values from the inserted value(s). |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 3096 | if (NumValValues) { |
| 3097 | SDValue Val = getValue(Op1); |
| 3098 | for (; i != LinearIndex + NumValValues; ++i) |
| 3099 | Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
| 3100 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 3101 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3102 | // Copy remaining value(s) from the original aggregate. |
| 3103 | for (; i != NumAggValues; ++i) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3104 | Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3105 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 3106 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3107 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 3108 | DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 3109 | &Values[0], NumAggValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3112 | void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3113 | const Value *Op0 = I.getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3114 | Type *AggTy = Op0->getType(); |
| 3115 | Type *ValTy = I.getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3116 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 3117 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3118 | unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3119 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3120 | SmallVector<EVT, 4> ValValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3121 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 3122 | |
| 3123 | unsigned NumValValues = ValValueVTs.size(); |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Ignore a extractvalue that produces an empty object |
| 3126 | if (!NumValValues) { |
| 3127 | setValue(&I, DAG.getUNDEF(MVT(MVT::Other))); |
| 3128 | return; |
| 3129 | } |
| 3130 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3131 | SmallVector<SDValue, 4> Values(NumValValues); |
| 3132 | |
| 3133 | SDValue Agg = getValue(Op0); |
| 3134 | // Copy out the selected value(s). |
| 3135 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 3136 | Values[i - LinearIndex] = |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 3137 | OutOfUndef ? |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3138 | DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
Bill Wendling | f0a2d0c | 2008-11-20 07:24:30 +0000 | [diff] [blame] | 3139 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3140 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3141 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 3142 | DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 3143 | &Values[0], NumValValues)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3146 | void SelectionDAGBuilder::visitGetElementPtr(const User &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3147 | SDValue N = getValue(I.getOperand(0)); |
Nadav Rotem | 1c23920 | 2012-02-28 14:13:19 +0000 | [diff] [blame] | 3148 | // Note that the pointer operand may be a vector of pointers. Take the scalar |
| 3149 | // element which holds a pointer. |
| 3150 | Type *Ty = I.getOperand(0)->getType()->getScalarType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3151 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3152 | 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] | 3153 | OI != E; ++OI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3154 | const Value *Idx = *OI; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3155 | if (StructType *StTy = dyn_cast<StructType>(Ty)) { |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3156 | unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3157 | if (Field) { |
| 3158 | // N = N + Offset |
| 3159 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3160 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3161 | DAG.getConstant(Offset, N.getValueType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3162 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3163 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3164 | Ty = StTy->getElementType(Field); |
| 3165 | } else { |
| 3166 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 3167 | |
| 3168 | // If this is a constant subscript, handle it quickly. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3169 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 3170 | if (CI->isZero()) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3171 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 3172 | TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 3173 | SDValue OffsVal; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3174 | EVT PTy = TLI.getPointerTy(); |
Owen Anderson | 77547be | 2009-08-10 18:56:59 +0000 | [diff] [blame] | 3175 | unsigned PtrBits = PTy.getSizeInBits(); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3176 | if (PtrBits < 64) |
Evan Cheng | 65b52df | 2009-02-09 21:01:06 +0000 | [diff] [blame] | 3177 | OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), |
| 3178 | TLI.getPointerTy(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3179 | DAG.getConstant(Offs, MVT::i64)); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3180 | else |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 3181 | OffsVal = DAG.getIntPtrConstant(Offs); |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3182 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3183 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N, |
Evan Cheng | b1032a8 | 2009-02-09 20:54:38 +0000 | [diff] [blame] | 3184 | OffsVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3185 | continue; |
| 3186 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3187 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3188 | // N = N + Idx * ElementSize; |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 3189 | APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(), |
| 3190 | TD->getTypeAllocSize(Ty)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3191 | SDValue IdxN = getValue(Idx); |
| 3192 | |
| 3193 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 3194 | // it. |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 3195 | IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3196 | |
| 3197 | // If this is a multiply by a power of two, turn it into a shl |
| 3198 | // immediately. This is a very common case. |
| 3199 | if (ElementSize != 1) { |
Dan Gohman | 7abbd04 | 2009-10-23 17:57:43 +0000 | [diff] [blame] | 3200 | if (ElementSize.isPowerOf2()) { |
| 3201 | unsigned Amt = ElementSize.logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3202 | IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3203 | N.getValueType(), IdxN, |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3204 | DAG.getConstant(Amt, IdxN.getValueType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3205 | } else { |
Duncan Sands | b2df01a | 2012-11-13 13:01:58 +0000 | [diff] [blame] | 3206 | SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3207 | IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3208 | N.getValueType(), IdxN, Scale); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3209 | } |
| 3210 | } |
| 3211 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3212 | N = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3213 | N.getValueType(), N, IdxN); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3214 | } |
| 3215 | } |
Bill Wendling | e1a9042 | 2009-12-21 23:10:19 +0000 | [diff] [blame] | 3216 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3217 | setValue(&I, N); |
| 3218 | } |
| 3219 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3220 | void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3221 | // If this is a fixed sized alloca in the entry block of the function, |
| 3222 | // allocate it statically on the stack. |
| 3223 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 3224 | return; // getValue will auto-populate this. |
| 3225 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3226 | Type *Ty = I.getAllocatedType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3227 | uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3228 | unsigned Align = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3229 | std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3230 | I.getAlignment()); |
| 3231 | |
| 3232 | SDValue AllocSize = getValue(I.getArraySize()); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3233 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3234 | EVT IntPtr = TLI.getPointerTy(); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 3235 | if (AllocSize.getValueType() != IntPtr) |
| 3236 | AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr); |
| 3237 | |
| 3238 | AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr, |
| 3239 | AllocSize, |
| 3240 | DAG.getConstant(TySize, IntPtr)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 3241 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3242 | // Handle alignment. If the requested alignment is less than or equal to |
| 3243 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 3244 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 3245 | unsigned StackAlign = TM.getFrameLowering()->getStackAlignment(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3246 | if (Align <= StackAlign) |
| 3247 | Align = 0; |
| 3248 | |
| 3249 | // Round the size of the allocation up to the stack alignment size |
| 3250 | // by add SA-1 to the size. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3251 | AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3252 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3253 | DAG.getIntPtrConstant(StackAlign-1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3254 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3255 | // Mask out the low bits for alignment purposes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3256 | AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 3257 | AllocSize.getValueType(), AllocSize, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3258 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 3259 | |
| 3260 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3261 | SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3262 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3263 | VTs, Ops, 3); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3264 | setValue(&I, DSA); |
| 3265 | DAG.setRoot(DSA.getValue(1)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3266 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3267 | // Inform the Frame Information that we have just allocated a variable-sized |
| 3268 | // object. |
Bob Wilson | 8f637ad | 2013-02-08 20:35:15 +0000 | [diff] [blame] | 3269 | FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3272 | void SelectionDAGBuilder::visitLoad(const LoadInst &I) { |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3273 | if (I.isAtomic()) |
| 3274 | return visitAtomicLoad(I); |
| 3275 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3276 | const Value *SV = I.getOperand(0); |
| 3277 | SDValue Ptr = getValue(SV); |
| 3278 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3279 | Type *Ty = I.getType(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3280 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3281 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3282 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 3283 | bool isInvariant = I.getMetadata("invariant.load") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3284 | unsigned Alignment = I.getAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3285 | const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa); |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 3286 | const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3287 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3288 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3289 | SmallVector<uint64_t, 4> Offsets; |
| 3290 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 3291 | unsigned NumValues = ValueVTs.size(); |
| 3292 | if (NumValues == 0) |
| 3293 | return; |
| 3294 | |
| 3295 | SDValue Root; |
| 3296 | bool ConstantMemory = false; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3297 | if (I.isVolatile() || NumValues > MaxParallelChains) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3298 | // Serialize volatile loads with other side effects. |
| 3299 | Root = getRoot(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3300 | else if (AA->pointsToConstantMemory( |
| 3301 | AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3302 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 3303 | Root = DAG.getEntryNode(); |
| 3304 | ConstantMemory = true; |
| 3305 | } else { |
| 3306 | // Do not serialize non-volatile loads against each other. |
| 3307 | Root = DAG.getRoot(); |
| 3308 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3309 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3310 | SmallVector<SDValue, 4> Values(NumValues); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3311 | SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains), |
| 3312 | NumValues)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3313 | EVT PtrVT = Ptr.getValueType(); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3314 | unsigned ChainI = 0; |
| 3315 | for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { |
| 3316 | // Serializing loads here may result in excessive register pressure, and |
| 3317 | // TokenFactor places arbitrary choke points on the scheduler. SD scheduling |
| 3318 | // could recover a bit by hoisting nodes upward in the chain by recognizing |
| 3319 | // they are side-effect free or do not alias. The optimizer should really |
| 3320 | // avoid this case by converting large object/array copies to llvm.memcpy |
| 3321 | // (MaxParallelChains should always remain as failsafe). |
| 3322 | if (ChainI == MaxParallelChains) { |
| 3323 | assert(PendingLoads.empty() && "PendingLoads must be serialized first"); |
| 3324 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 3325 | MVT::Other, &Chains[0], ChainI); |
| 3326 | Root = Chain; |
| 3327 | ChainI = 0; |
| 3328 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3329 | SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(), |
| 3330 | PtrVT, Ptr, |
| 3331 | DAG.getConstant(Offsets[i], PtrVT)); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3332 | SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root, |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 3333 | A, MachinePointerInfo(SV, Offsets[i]), isVolatile, |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 3334 | isNonTemporal, isInvariant, Alignment, TBAAInfo, |
| 3335 | Ranges); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3336 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3337 | Values[i] = L; |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3338 | Chains[ChainI] = L.getValue(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3339 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3340 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3341 | if (!ConstantMemory) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3342 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3343 | MVT::Other, &Chains[0], ChainI); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3344 | if (isVolatile) |
| 3345 | DAG.setRoot(Chain); |
| 3346 | else |
| 3347 | PendingLoads.push_back(Chain); |
| 3348 | } |
| 3349 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3350 | setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 3351 | DAG.getVTList(&ValueVTs[0], NumValues), |
| 3352 | &Values[0], NumValues)); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3353 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3354 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3355 | void SelectionDAGBuilder::visitStore(const StoreInst &I) { |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3356 | if (I.isAtomic()) |
| 3357 | return visitAtomicStore(I); |
| 3358 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3359 | const Value *SrcV = I.getOperand(0); |
| 3360 | const Value *PtrV = I.getOperand(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3361 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3362 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3363 | SmallVector<uint64_t, 4> Offsets; |
| 3364 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 3365 | unsigned NumValues = ValueVTs.size(); |
| 3366 | if (NumValues == 0) |
| 3367 | return; |
| 3368 | |
| 3369 | // Get the lowered operands. Note that we do this after |
| 3370 | // checking if NumResults is zero, because with zero results |
| 3371 | // the operands won't have values in the map. |
| 3372 | SDValue Src = getValue(SrcV); |
| 3373 | SDValue Ptr = getValue(PtrV); |
| 3374 | |
| 3375 | SDValue Root = getRoot(); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3376 | SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains), |
| 3377 | NumValues)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3378 | EVT PtrVT = Ptr.getValueType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3379 | bool isVolatile = I.isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3380 | bool isNonTemporal = I.getMetadata("nontemporal") != 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3381 | unsigned Alignment = I.getAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 3382 | const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3383 | |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3384 | unsigned ChainI = 0; |
| 3385 | for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { |
| 3386 | // See visitLoad comments. |
| 3387 | if (ChainI == MaxParallelChains) { |
| 3388 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 3389 | MVT::Other, &Chains[0], ChainI); |
| 3390 | Root = Chain; |
| 3391 | ChainI = 0; |
| 3392 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3393 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr, |
| 3394 | DAG.getConstant(Offsets[i], PtrVT)); |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3395 | SDValue St = DAG.getStore(Root, getCurDebugLoc(), |
| 3396 | SDValue(Src.getNode(), Src.getResNo() + i), |
| 3397 | Add, MachinePointerInfo(PtrV, Offsets[i]), |
| 3398 | isVolatile, isNonTemporal, Alignment, TBAAInfo); |
| 3399 | Chains[ChainI] = St; |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Devang Patel | 7e13efa | 2010-10-26 22:14:52 +0000 | [diff] [blame] | 3402 | SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
Andrew Trick | de91f3c | 2010-11-12 17:50:46 +0000 | [diff] [blame] | 3403 | MVT::Other, &Chains[0], ChainI); |
Devang Patel | 7e13efa | 2010-10-26 22:14:52 +0000 | [diff] [blame] | 3404 | ++SDNodeOrder; |
| 3405 | AssignOrderingToNode(StoreNode.getNode()); |
| 3406 | DAG.setRoot(StoreNode); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3409 | static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3410 | SynchronizationScope Scope, |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3411 | bool Before, DebugLoc dl, |
| 3412 | SelectionDAG &DAG, |
| 3413 | const TargetLowering &TLI) { |
| 3414 | // Fence, if necessary |
| 3415 | if (Before) { |
Eli Friedman | 069e2ed | 2011-08-26 02:59:24 +0000 | [diff] [blame] | 3416 | if (Order == AcquireRelease || Order == SequentiallyConsistent) |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3417 | Order = Release; |
| 3418 | else if (Order == Acquire || Order == Monotonic) |
| 3419 | return Chain; |
| 3420 | } else { |
| 3421 | if (Order == AcquireRelease) |
| 3422 | Order = Acquire; |
| 3423 | else if (Order == Release || Order == Monotonic) |
| 3424 | return Chain; |
| 3425 | } |
| 3426 | SDValue Ops[3]; |
| 3427 | Ops[0] = Chain; |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3428 | Ops[1] = DAG.getConstant(Order, TLI.getPointerTy()); |
| 3429 | Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy()); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3430 | return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3); |
| 3431 | } |
| 3432 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3433 | void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) { |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3434 | DebugLoc dl = getCurDebugLoc(); |
| 3435 | AtomicOrdering Order = I.getOrdering(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3436 | SynchronizationScope Scope = I.getSynchScope(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3437 | |
| 3438 | SDValue InChain = getRoot(); |
| 3439 | |
| 3440 | if (TLI.getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3441 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
| 3442 | DAG, TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3443 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3444 | SDValue L = |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3445 | DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3446 | getValue(I.getCompareOperand()).getValueType().getSimpleVT(), |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3447 | InChain, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3448 | getValue(I.getPointerOperand()), |
| 3449 | getValue(I.getCompareOperand()), |
| 3450 | getValue(I.getNewValOperand()), |
| 3451 | MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3452 | TLI.getInsertFencesForAtomic() ? Monotonic : Order, |
| 3453 | Scope); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3454 | |
| 3455 | SDValue OutChain = L.getValue(1); |
| 3456 | |
| 3457 | if (TLI.getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3458 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
| 3459 | DAG, TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3460 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3461 | setValue(&I, L); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3462 | DAG.setRoot(OutChain); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
| 3465 | void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) { |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3466 | DebugLoc dl = getCurDebugLoc(); |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3467 | ISD::NodeType NT; |
| 3468 | switch (I.getOperation()) { |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 3469 | default: llvm_unreachable("Unknown atomicrmw operation"); |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3470 | case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break; |
| 3471 | case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break; |
| 3472 | case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break; |
| 3473 | case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break; |
| 3474 | case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break; |
| 3475 | case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break; |
| 3476 | case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break; |
| 3477 | case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break; |
| 3478 | case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break; |
| 3479 | case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break; |
| 3480 | case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break; |
| 3481 | } |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3482 | AtomicOrdering Order = I.getOrdering(); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3483 | SynchronizationScope Scope = I.getSynchScope(); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3484 | |
| 3485 | SDValue InChain = getRoot(); |
| 3486 | |
| 3487 | if (TLI.getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3488 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
| 3489 | DAG, TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3490 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3491 | SDValue L = |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3492 | DAG.getAtomic(NT, dl, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3493 | getValue(I.getValOperand()).getValueType().getSimpleVT(), |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3494 | InChain, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3495 | getValue(I.getPointerOperand()), |
| 3496 | getValue(I.getValOperand()), |
| 3497 | I.getPointerOperand(), 0 /* Alignment */, |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3498 | TLI.getInsertFencesForAtomic() ? Monotonic : Order, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3499 | Scope); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3500 | |
| 3501 | SDValue OutChain = L.getValue(1); |
| 3502 | |
| 3503 | if (TLI.getInsertFencesForAtomic()) |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3504 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
| 3505 | DAG, TLI); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3506 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 3507 | setValue(&I, L); |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 3508 | DAG.setRoot(OutChain); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3511 | void SelectionDAGBuilder::visitFence(const FenceInst &I) { |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 3512 | DebugLoc dl = getCurDebugLoc(); |
| 3513 | SDValue Ops[3]; |
| 3514 | Ops[0] = getRoot(); |
| 3515 | Ops[1] = DAG.getConstant(I.getOrdering(), TLI.getPointerTy()); |
| 3516 | Ops[2] = DAG.getConstant(I.getSynchScope(), TLI.getPointerTy()); |
| 3517 | DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3)); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3520 | void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) { |
| 3521 | DebugLoc dl = getCurDebugLoc(); |
| 3522 | AtomicOrdering Order = I.getOrdering(); |
| 3523 | SynchronizationScope Scope = I.getSynchScope(); |
| 3524 | |
| 3525 | SDValue InChain = getRoot(); |
| 3526 | |
Eli Friedman | fd45fa1 | 2012-08-17 23:24:29 +0000 | [diff] [blame] | 3527 | EVT VT = TLI.getValueType(I.getType()); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3528 | |
Evan Cheng | 607acd6 | 2013-02-06 02:06:33 +0000 | [diff] [blame] | 3529 | if (I.getAlignment() < VT.getSizeInBits() / 8) |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3530 | report_fatal_error("Cannot generate unaligned atomic load"); |
| 3531 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3532 | SDValue L = |
| 3533 | DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain, |
| 3534 | getValue(I.getPointerOperand()), |
| 3535 | I.getPointerOperand(), I.getAlignment(), |
| 3536 | TLI.getInsertFencesForAtomic() ? Monotonic : Order, |
| 3537 | Scope); |
| 3538 | |
| 3539 | SDValue OutChain = L.getValue(1); |
| 3540 | |
| 3541 | if (TLI.getInsertFencesForAtomic()) |
| 3542 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
| 3543 | DAG, TLI); |
| 3544 | |
| 3545 | setValue(&I, L); |
| 3546 | DAG.setRoot(OutChain); |
| 3547 | } |
| 3548 | |
| 3549 | void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) { |
| 3550 | DebugLoc dl = getCurDebugLoc(); |
| 3551 | |
| 3552 | AtomicOrdering Order = I.getOrdering(); |
| 3553 | SynchronizationScope Scope = I.getSynchScope(); |
| 3554 | |
| 3555 | SDValue InChain = getRoot(); |
| 3556 | |
Eli Friedman | fd45fa1 | 2012-08-17 23:24:29 +0000 | [diff] [blame] | 3557 | EVT VT = TLI.getValueType(I.getValueOperand()->getType()); |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3558 | |
Evan Cheng | 607acd6 | 2013-02-06 02:06:33 +0000 | [diff] [blame] | 3559 | if (I.getAlignment() < VT.getSizeInBits() / 8) |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3560 | report_fatal_error("Cannot generate unaligned atomic store"); |
| 3561 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3562 | if (TLI.getInsertFencesForAtomic()) |
| 3563 | InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl, |
| 3564 | DAG, TLI); |
| 3565 | |
| 3566 | SDValue OutChain = |
Eli Friedman | fe73121 | 2011-09-13 20:50:54 +0000 | [diff] [blame] | 3567 | DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 3568 | InChain, |
| 3569 | getValue(I.getPointerOperand()), |
| 3570 | getValue(I.getValueOperand()), |
| 3571 | I.getPointerOperand(), I.getAlignment(), |
| 3572 | TLI.getInsertFencesForAtomic() ? Monotonic : Order, |
| 3573 | Scope); |
| 3574 | |
| 3575 | if (TLI.getInsertFencesForAtomic()) |
| 3576 | OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl, |
| 3577 | DAG, TLI); |
| 3578 | |
| 3579 | DAG.setRoot(OutChain); |
| 3580 | } |
| 3581 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3582 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 3583 | /// node. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 3584 | void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 3585 | unsigned Intrinsic) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3586 | bool HasChain = !I.doesNotAccessMemory(); |
| 3587 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 3588 | |
| 3589 | // Build the operand list. |
| 3590 | SmallVector<SDValue, 8> Ops; |
| 3591 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 3592 | if (OnlyLoad) { |
| 3593 | // We don't need to serialize loads against other loads. |
| 3594 | Ops.push_back(DAG.getRoot()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3595 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3596 | Ops.push_back(getRoot()); |
| 3597 | } |
| 3598 | } |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3599 | |
| 3600 | // Info is set by getTgtMemInstrinsic |
| 3601 | TargetLowering::IntrinsicInfo Info; |
| 3602 | bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic); |
| 3603 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3604 | // 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] | 3605 | if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID || |
| 3606 | Info.opc == ISD::INTRINSIC_W_CHAIN) |
Pete Cooper | bf42139 | 2012-01-16 04:08:12 +0000 | [diff] [blame] | 3607 | Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI.getPointerTy())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3608 | |
| 3609 | // Add all operands of the call to the operand list. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 3610 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 3611 | SDValue Op = getValue(I.getArgOperand(i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3612 | Ops.push_back(Op); |
| 3613 | } |
| 3614 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3615 | SmallVector<EVT, 4> ValueVTs; |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 3616 | ComputeValueVTs(TLI, I.getType(), ValueVTs); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3617 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3618 | if (HasChain) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3619 | ValueVTs.push_back(MVT::Other); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3620 | |
Bob Wilson | 8d91955 | 2009-07-31 22:41:21 +0000 | [diff] [blame] | 3621 | SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3622 | |
| 3623 | // Create the node. |
| 3624 | SDValue Result; |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3625 | if (IsTgtIntrinsic) { |
| 3626 | // This is target intrinsic that touches memory |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3627 | Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3628 | VTs, &Ops[0], Ops.size(), |
Chris Lattner | e9ba5dd | 2010-09-21 04:57:15 +0000 | [diff] [blame] | 3629 | Info.memVT, |
| 3630 | MachinePointerInfo(Info.ptrVal, Info.offset), |
Mon P Wang | 3efcd4a | 2008-11-01 20:24:53 +0000 | [diff] [blame] | 3631 | Info.align, Info.vol, |
| 3632 | Info.readMem, Info.writeMem); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3633 | } else if (!HasChain) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3634 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3635 | VTs, &Ops[0], Ops.size()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3636 | } else if (!I.getType()->isVoidTy()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3637 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3638 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3639 | } else { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3640 | Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(), |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 3641 | VTs, &Ops[0], Ops.size()); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3644 | if (HasChain) { |
| 3645 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 3646 | if (OnlyLoad) |
| 3647 | PendingLoads.push_back(Chain); |
| 3648 | else |
| 3649 | DAG.setRoot(Chain); |
| 3650 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3651 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 3652 | if (!I.getType()->isVoidTy()) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3653 | if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3654 | EVT VT = TLI.getValueType(PTy); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3655 | Result = DAG.getNode(ISD::BITCAST, getCurDebugLoc(), VT, Result); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3656 | } |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3657 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3658 | setValue(&I, Result); |
Evan Cheng | 5aef795 | 2012-03-22 19:29:09 +0000 | [diff] [blame] | 3659 | } else { |
| 3660 | // Assign order to result here. If the intrinsic does not produce a result, |
| 3661 | // it won't be mapped to a SDNode and visit() will not assign it an order |
| 3662 | // number. |
| 3663 | ++SDNodeOrder; |
| 3664 | AssignOrderingToNode(Result.getNode()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3668 | /// GetSignificand - Get the significand and build it into a floating-point |
| 3669 | /// number with exponent of 1: |
| 3670 | /// |
| 3671 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 3672 | /// |
Matt Beaumont-Gay | 50e75bf | 2013-02-25 18:11:18 +0000 | [diff] [blame] | 3673 | /// where Op is the hexadecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3674 | static SDValue |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3675 | GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3676 | SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3677 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 3678 | SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, |
| 3679 | DAG.getConstant(0x3f800000, MVT::i32)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3680 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3683 | /// GetExponent - Get the exponent: |
| 3684 | /// |
Bill Wendling | e9a7286 | 2009-01-20 21:17:57 +0000 | [diff] [blame] | 3685 | /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3686 | /// |
Matt Beaumont-Gay | 50e75bf | 2013-02-25 18:11:18 +0000 | [diff] [blame] | 3687 | /// where Op is the hexadecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3688 | static SDValue |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 3689 | GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3690 | DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3691 | SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, |
| 3692 | DAG.getConstant(0x7f800000, MVT::i32)); |
| 3693 | SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3694 | DAG.getConstant(23, TLI.getPointerTy())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3695 | SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, |
| 3696 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 3697 | return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3700 | /// getF32Constant - Get 32-bit floating point constant. |
| 3701 | static SDValue |
| 3702 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3703 | return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)), |
| 3704 | MVT::f32); |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3707 | /// expandExp - Lower an exp intrinsic. Handles the special sequences for |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3708 | /// limited-precision mode. |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3709 | static SDValue expandExp(DebugLoc dl, SDValue Op, SelectionDAG &DAG, |
| 3710 | const TargetLowering &TLI) { |
| 3711 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3712 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3713 | |
| 3714 | // Put the exponent in the right bit position for later addition to the |
| 3715 | // final result: |
| 3716 | // |
| 3717 | // #define LOG2OFe 1.4426950f |
| 3718 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3719 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3720 | getF32Constant(DAG, 0x3fb8aa3b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3721 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3722 | |
| 3723 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3724 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 3725 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3726 | |
| 3727 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3728 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3729 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3730 | |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3731 | SDValue TwoToFracPartOfX; |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3732 | if (LimitFloatPrecision <= 6) { |
| 3733 | // For floating-point precision of 6: |
| 3734 | // |
| 3735 | // TwoToFractionalPartOfX = |
| 3736 | // 0.997535578f + |
| 3737 | // (0.735607626f + 0.252464424f * x) * x; |
| 3738 | // |
| 3739 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3740 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3741 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3742 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3743 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3744 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3745 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 3746 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3747 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3748 | // For floating-point precision of 12: |
| 3749 | // |
| 3750 | // TwoToFractionalPartOfX = |
| 3751 | // 0.999892986f + |
| 3752 | // (0.696457318f + |
| 3753 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3754 | // |
| 3755 | // 0.000107046256 error, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3756 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3757 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3758 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3759 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3760 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3761 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3762 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3763 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3764 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 3765 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3766 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3767 | // For floating-point precision of 18: |
| 3768 | // |
| 3769 | // TwoToFractionalPartOfX = |
| 3770 | // 0.999999982f + |
| 3771 | // (0.693148872f + |
| 3772 | // (0.240227044f + |
| 3773 | // (0.554906021e-1f + |
| 3774 | // (0.961591928e-2f + |
| 3775 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3776 | // |
| 3777 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3778 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3779 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3780 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3781 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3782 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3783 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3784 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3785 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3786 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3787 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3788 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3789 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3790 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3791 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 3792 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3793 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3794 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3795 | TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 3796 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3797 | } |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 3798 | |
| 3799 | // Add the exponent into the result in integer domain. |
| 3800 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX); |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3801 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 3802 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 3803 | t13, IntegerPartOfX)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 3806 | // No special expansion. |
| 3807 | return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3810 | /// expandLog - Lower a log intrinsic. Handles the special sequences for |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3811 | /// limited-precision mode. |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3812 | static SDValue expandLog(DebugLoc dl, SDValue Op, SelectionDAG &DAG, |
| 3813 | const TargetLowering &TLI) { |
| 3814 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3815 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3816 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3817 | |
| 3818 | // Scale the exponent by log(2) [0.69314718f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3819 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3820 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3821 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3822 | |
| 3823 | // Get the significand and build it into a floating-point number with |
| 3824 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3825 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3826 | |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3827 | SDValue LogOfMantissa; |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3828 | if (LimitFloatPrecision <= 6) { |
| 3829 | // For floating-point precision of 6: |
| 3830 | // |
| 3831 | // LogofMantissa = |
| 3832 | // -1.1609546f + |
| 3833 | // (1.4034025f - 0.23903021f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3834 | // |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3835 | // error 0.0034276066, which is better than 8 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3836 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3837 | getF32Constant(DAG, 0xbe74c456)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3838 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3839 | getF32Constant(DAG, 0x3fb3a2b1)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3840 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3841 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3842 | getF32Constant(DAG, 0x3f949a29)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3843 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3844 | // For floating-point precision of 12: |
| 3845 | // |
| 3846 | // LogOfMantissa = |
| 3847 | // -1.7417939f + |
| 3848 | // (2.8212026f + |
| 3849 | // (-1.4699568f + |
| 3850 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 3851 | // |
| 3852 | // error 0.000061011436, which is 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3853 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3854 | getF32Constant(DAG, 0xbd67b6d6)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3855 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3856 | getF32Constant(DAG, 0x3ee4f4b8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3857 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3858 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3859 | getF32Constant(DAG, 0x3fbc278b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3860 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3861 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3862 | getF32Constant(DAG, 0x40348e95)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3863 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3864 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3865 | getF32Constant(DAG, 0x3fdef31a)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3866 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3867 | // For floating-point precision of 18: |
| 3868 | // |
| 3869 | // LogOfMantissa = |
| 3870 | // -2.1072184f + |
| 3871 | // (4.2372794f + |
| 3872 | // (-3.7029485f + |
| 3873 | // (2.2781945f + |
| 3874 | // (-0.87823314f + |
| 3875 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 3876 | // |
| 3877 | // error 0.0000023660568, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3878 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3879 | getF32Constant(DAG, 0xbc91e5ac)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3880 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3881 | getF32Constant(DAG, 0x3e4350aa)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3882 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3883 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3884 | getF32Constant(DAG, 0x3f60d3e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3885 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3886 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3887 | getF32Constant(DAG, 0x4011cdf0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3888 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3889 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3890 | getF32Constant(DAG, 0x406cfd1c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3891 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3892 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3893 | getF32Constant(DAG, 0x408797cb)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3894 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3895 | LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3896 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3897 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3898 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3899 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3902 | // No special expansion. |
| 3903 | return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3906 | /// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3907 | /// limited-precision mode. |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3908 | static SDValue expandLog2(DebugLoc dl, SDValue Op, SelectionDAG &DAG, |
| 3909 | const TargetLowering &TLI) { |
| 3910 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3911 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3912 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3913 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3914 | // Get the exponent. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3915 | SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); |
Bill Wendling | 856ff41 | 2009-12-22 00:12:37 +0000 | [diff] [blame] | 3916 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3917 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3918 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3919 | SDValue X = GetSignificand(DAG, Op1, dl); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3920 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3921 | // Different possible minimax approximations of significand in |
| 3922 | // floating-point for various degrees of accuracy over [1,2]. |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3923 | SDValue Log2ofMantissa; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3924 | if (LimitFloatPrecision <= 6) { |
| 3925 | // For floating-point precision of 6: |
| 3926 | // |
| 3927 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3928 | // |
| 3929 | // error 0.0049451742, which is more than 7 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3930 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3931 | getF32Constant(DAG, 0xbeb08fe0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3932 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3933 | getF32Constant(DAG, 0x40019463)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3934 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3935 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 3936 | getF32Constant(DAG, 0x3fd6633d)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3937 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3938 | // For floating-point precision of 12: |
| 3939 | // |
| 3940 | // Log2ofMantissa = |
| 3941 | // -2.51285454f + |
| 3942 | // (4.07009056f + |
| 3943 | // (-2.12067489f + |
| 3944 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 3945 | // |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3946 | // error 0.0000876136000, which is better than 13 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3947 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3948 | getF32Constant(DAG, 0xbda7262e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3949 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3950 | getF32Constant(DAG, 0x3f25280b)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3951 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3952 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3953 | getF32Constant(DAG, 0x4007b923)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3954 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3955 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3956 | getF32Constant(DAG, 0x40823e2f)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3957 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3958 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
| 3959 | getF32Constant(DAG, 0x4020d29c)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 3960 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3961 | // For floating-point precision of 18: |
| 3962 | // |
| 3963 | // Log2ofMantissa = |
| 3964 | // -3.0400495f + |
| 3965 | // (6.1129976f + |
| 3966 | // (-5.3420409f + |
| 3967 | // (3.2865683f + |
| 3968 | // (-1.2669343f + |
| 3969 | // (0.27515199f - |
| 3970 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3971 | // |
| 3972 | // error 0.0000018516, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3973 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3974 | getF32Constant(DAG, 0xbcd2769e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3975 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3976 | getF32Constant(DAG, 0x3e8ce0b9)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3977 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 3978 | SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3979 | getF32Constant(DAG, 0x3fa22ae7)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3980 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 3981 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3982 | getF32Constant(DAG, 0x40525723)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3983 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 3984 | SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3985 | getF32Constant(DAG, 0x40aaf200)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3986 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 3987 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3988 | getF32Constant(DAG, 0x40c39dad)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3989 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3990 | Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, |
| 3991 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3992 | } |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 3993 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3994 | return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa); |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3995 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3996 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 3997 | // No special expansion. |
| 3998 | return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4001 | /// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4002 | /// limited-precision mode. |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4003 | static SDValue expandLog10(DebugLoc dl, SDValue Op, SelectionDAG &DAG, |
| 4004 | const TargetLowering &TLI) { |
| 4005 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4006 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4007 | SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4008 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 4009 | // Scale the exponent by log10(2) [0.30102999f]. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4010 | SDValue Exp = GetExponent(DAG, Op1, TLI, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4011 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4012 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4013 | |
| 4014 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 4015 | // exponent of 1. |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4016 | SDValue X = GetSignificand(DAG, Op1, dl); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4017 | |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4018 | SDValue Log10ofMantissa; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4019 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4020 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4021 | // |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4022 | // Log10ofMantissa = |
| 4023 | // -0.50419619f + |
| 4024 | // (0.60948995f - 0.10380950f * x) * x; |
| 4025 | // |
| 4026 | // error 0.0014886165, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4027 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4028 | getF32Constant(DAG, 0xbdd49a13)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4029 | SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4030 | getF32Constant(DAG, 0x3f1c0789)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4031 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4032 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, |
| 4033 | getF32Constant(DAG, 0x3f011300)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4034 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 4035 | // For floating-point precision of 12: |
| 4036 | // |
| 4037 | // Log10ofMantissa = |
| 4038 | // -0.64831180f + |
| 4039 | // (0.91751397f + |
| 4040 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 4041 | // |
| 4042 | // error 0.00019228036, which is better than 12 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4043 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4044 | getF32Constant(DAG, 0x3d431f31)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4045 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4046 | getF32Constant(DAG, 0x3ea21fb2)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4047 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4048 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4049 | getF32Constant(DAG, 0x3f6ae232)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4050 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4051 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
| 4052 | getF32Constant(DAG, 0x3f25f7c3)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4053 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 4054 | // For floating-point precision of 18: |
| 4055 | // |
| 4056 | // Log10ofMantissa = |
| 4057 | // -0.84299375f + |
| 4058 | // (1.5327582f + |
| 4059 | // (-1.0688956f + |
| 4060 | // (0.49102474f + |
| 4061 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 4062 | // |
| 4063 | // error 0.0000037995730, which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4064 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4065 | getF32Constant(DAG, 0x3c5d51ce)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4066 | SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4067 | getF32Constant(DAG, 0x3e00685a)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4068 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); |
| 4069 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4070 | getF32Constant(DAG, 0x3efb6798)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4071 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4072 | SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4073 | getF32Constant(DAG, 0x3f88d192)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4074 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4075 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4076 | getF32Constant(DAG, 0x3fc4316c)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4077 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
Craig Topper | df0ea8d | 2012-11-16 19:08:44 +0000 | [diff] [blame] | 4078 | Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, |
| 4079 | getF32Constant(DAG, 0x3f57ce70)); |
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, Log10ofMantissa); |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +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::FLOG10, dl, Op.getValueType(), Op); |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4089 | /// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4090 | /// limited-precision mode. |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4091 | static SDValue expandExp2(DebugLoc dl, SDValue Op, SelectionDAG &DAG, |
| 4092 | const TargetLowering &TLI) { |
| 4093 | if (Op.getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4094 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4095 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4096 | |
| 4097 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4098 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 4099 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4100 | |
| 4101 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4102 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 4103 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4104 | |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4105 | SDValue TwoToFractionalPartOfX; |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4106 | if (LimitFloatPrecision <= 6) { |
| 4107 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4108 | // |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4109 | // TwoToFractionalPartOfX = |
| 4110 | // 0.997535578f + |
| 4111 | // (0.735607626f + 0.252464424f * x) * x; |
| 4112 | // |
| 4113 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4114 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4115 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4116 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4117 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4118 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4119 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 4120 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4121 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4122 | // For floating-point precision of 12: |
| 4123 | // |
| 4124 | // TwoToFractionalPartOfX = |
| 4125 | // 0.999892986f + |
| 4126 | // (0.696457318f + |
| 4127 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 4128 | // |
| 4129 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4130 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4131 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4132 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4133 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4134 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4135 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4136 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4137 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4138 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 4139 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4140 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4141 | // For floating-point precision of 18: |
| 4142 | // |
| 4143 | // TwoToFractionalPartOfX = |
| 4144 | // 0.999999982f + |
| 4145 | // (0.693148872f + |
| 4146 | // (0.240227044f + |
| 4147 | // (0.554906021e-1f + |
| 4148 | // (0.961591928e-2f + |
| 4149 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 4150 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4151 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4152 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4153 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4154 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4155 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4156 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4157 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4158 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4159 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4160 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4161 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4162 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4163 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4164 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 4165 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4166 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4167 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4168 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 4169 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4170 | } |
Craig Topper | b315772 | 2012-11-24 08:22:37 +0000 | [diff] [blame] | 4171 | |
| 4172 | // Add the exponent into the result in integer domain. |
| 4173 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, |
| 4174 | TwoToFractionalPartOfX); |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4175 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 4176 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 4177 | t13, IntegerPartOfX)); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4178 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 4179 | |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4180 | // No special expansion. |
| 4181 | return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 4182 | } |
| 4183 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4184 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 4185 | /// limited-precision mode with x == 10.0f. |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4186 | static SDValue expandPow(DebugLoc dl, SDValue LHS, SDValue RHS, |
| 4187 | SelectionDAG &DAG, const TargetLowering &TLI) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4188 | bool IsExp10 = false; |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4189 | if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4190 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4191 | if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) { |
| 4192 | APFloat Ten(10.0f); |
| 4193 | IsExp10 = LHSC->isExactlyValue(Ten); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4194 | } |
| 4195 | } |
| 4196 | |
Craig Topper | c1aa638 | 2012-11-25 00:48:58 +0000 | [diff] [blame] | 4197 | if (IsExp10) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4198 | // Put the exponent in the right bit position for later addition to the |
| 4199 | // final result: |
| 4200 | // |
| 4201 | // #define LOG2OF10 3.3219281f |
| 4202 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4203 | SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4204 | getF32Constant(DAG, 0x40549a78)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4205 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4206 | |
| 4207 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4208 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); |
| 4209 | SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4210 | |
| 4211 | // IntegerPartOfX <<= 23; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4212 | IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 4213 | DAG.getConstant(23, TLI.getPointerTy())); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4214 | |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4215 | SDValue TwoToFractionalPartOfX; |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4216 | if (LimitFloatPrecision <= 6) { |
| 4217 | // For floating-point precision of 6: |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4218 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4219 | // twoToFractionalPartOfX = |
| 4220 | // 0.997535578f + |
| 4221 | // (0.735607626f + 0.252464424f * x) * x; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 4222 | // |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4223 | // error 0.0144103317, which is 6 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4224 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4225 | getF32Constant(DAG, 0x3e814304)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4226 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4227 | getF32Constant(DAG, 0x3f3c50c8)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4228 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4229 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
| 4230 | getF32Constant(DAG, 0x3f7f5e7e)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4231 | } else if (LimitFloatPrecision <= 12) { |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4232 | // For floating-point precision of 12: |
| 4233 | // |
| 4234 | // TwoToFractionalPartOfX = |
| 4235 | // 0.999892986f + |
| 4236 | // (0.696457318f + |
| 4237 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 4238 | // |
| 4239 | // error 0.000107046256, which is 13 to 14 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4240 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4241 | getF32Constant(DAG, 0x3da235e3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4242 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4243 | getF32Constant(DAG, 0x3e65b8f3)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4244 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4245 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4246 | getF32Constant(DAG, 0x3f324b07)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4247 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4248 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
| 4249 | getF32Constant(DAG, 0x3f7ff8fd)); |
Craig Topper | 08ac469 | 2012-11-16 20:01:39 +0000 | [diff] [blame] | 4250 | } else { // LimitFloatPrecision <= 18 |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4251 | // For floating-point precision of 18: |
| 4252 | // |
| 4253 | // TwoToFractionalPartOfX = |
| 4254 | // 0.999999982f + |
| 4255 | // (0.693148872f + |
| 4256 | // (0.240227044f + |
| 4257 | // (0.554906021e-1f + |
| 4258 | // (0.961591928e-2f + |
| 4259 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 4260 | // error 2.47208000*10^(-7), which is better than 18 bits |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4261 | SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4262 | getF32Constant(DAG, 0x3924b03e)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4263 | SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4264 | getF32Constant(DAG, 0x3ab24b87)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4265 | SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); |
| 4266 | SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4267 | getF32Constant(DAG, 0x3c1d8c17)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4268 | SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); |
| 4269 | SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4270 | getF32Constant(DAG, 0x3d634a1d)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4271 | SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); |
| 4272 | SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4273 | getF32Constant(DAG, 0x3e75fe14)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4274 | SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); |
| 4275 | SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 4276 | getF32Constant(DAG, 0x3f317234)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4277 | SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4278 | TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, |
| 4279 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4280 | } |
Craig Topper | 915562e | 2012-11-25 00:15:07 +0000 | [diff] [blame] | 4281 | |
| 4282 | SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX); |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4283 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, |
| 4284 | DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 4285 | t13, IntegerPartOfX)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4288 | // No special expansion. |
| 4289 | return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4292 | |
| 4293 | /// ExpandPowI - Expand a llvm.powi intrinsic. |
| 4294 | static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS, |
| 4295 | SelectionDAG &DAG) { |
| 4296 | // If RHS is a constant, we can expand this out to a multiplication tree, |
| 4297 | // otherwise we end up lowering to a call to __powidf2 (for example). When |
| 4298 | // optimizing for size, we only want to do this if the expansion would produce |
| 4299 | // a small number of multiplies, otherwise we do the full expansion. |
| 4300 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 4301 | // Get the exponent as a positive value. |
| 4302 | unsigned Val = RHSC->getSExtValue(); |
| 4303 | if ((int)Val < 0) Val = -Val; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4304 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4305 | // powi(x, 0) -> 1.0 |
| 4306 | if (Val == 0) |
| 4307 | return DAG.getConstantFP(1.0, LHS.getValueType()); |
| 4308 | |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 4309 | const Function *F = DAG.getMachineFunction().getFunction(); |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 4310 | if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 4311 | Attribute::OptimizeForSize) || |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4312 | // If optimizing for size, don't insert too many multiplies. This |
| 4313 | // inserts up to 5 multiplies. |
| 4314 | CountPopulation_32(Val)+Log2_32(Val) < 7) { |
| 4315 | // We use the simple binary decomposition method to generate the multiply |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4316 | // sequence. There are more optimal ways to do this (for example, |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4317 | // powi(x,15) generates one more multiply than it should), but this has |
| 4318 | // the benefit of being both really simple and much better than a libcall. |
| 4319 | SDValue Res; // Logically starts equal to 1.0 |
| 4320 | SDValue CurSquare = LHS; |
| 4321 | while (Val) { |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 4322 | if (Val & 1) { |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4323 | if (Res.getNode()) |
| 4324 | Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare); |
| 4325 | else |
| 4326 | Res = CurSquare; // 1.0*CurSquare. |
Mikhail Glushenkov | bfdfea8 | 2010-01-01 04:41:36 +0000 | [diff] [blame] | 4327 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4328 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4329 | CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), |
| 4330 | CurSquare, CurSquare); |
| 4331 | Val >>= 1; |
| 4332 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 4333 | |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4334 | // If the original was negative, invert the result, producing 1/(x*x*x). |
| 4335 | if (RHSC->getSExtValue() < 0) |
| 4336 | Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), |
| 4337 | DAG.getConstantFP(1.0, LHS.getValueType()), Res); |
| 4338 | return Res; |
| 4339 | } |
| 4340 | } |
| 4341 | |
| 4342 | // Otherwise, expand to a libcall. |
| 4343 | return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); |
| 4344 | } |
| 4345 | |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4346 | // getTruncatedArgReg - Find underlying register used for an truncated |
| 4347 | // argument. |
| 4348 | static unsigned getTruncatedArgReg(const SDValue &N) { |
| 4349 | if (N.getOpcode() != ISD::TRUNCATE) |
| 4350 | return 0; |
| 4351 | |
| 4352 | const SDValue &Ext = N.getOperand(0); |
| 4353 | if (Ext.getOpcode() == ISD::AssertZext || Ext.getOpcode() == ISD::AssertSext){ |
| 4354 | const SDValue &CFR = Ext.getOperand(0); |
| 4355 | if (CFR.getOpcode() == ISD::CopyFromReg) |
| 4356 | return cast<RegisterSDNode>(CFR.getOperand(1))->getReg(); |
Craig Topper | 7eb46d8 | 2012-04-11 04:55:51 +0000 | [diff] [blame] | 4357 | if (CFR.getOpcode() == ISD::TRUNCATE) |
| 4358 | return getTruncatedArgReg(CFR); |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4359 | } |
| 4360 | return 0; |
| 4361 | } |
| 4362 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4363 | /// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function |
| 4364 | /// argument, create the corresponding DBG_VALUE machine instruction for it now. |
| 4365 | /// 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] | 4366 | bool |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 4367 | SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable, |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4368 | int64_t Offset, |
Dan Gohman | 5d11ea3 | 2010-05-01 00:33:16 +0000 | [diff] [blame] | 4369 | const SDValue &N) { |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4370 | const Argument *Arg = dyn_cast<Argument>(V); |
| 4371 | if (!Arg) |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4372 | return false; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4373 | |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 4374 | MachineFunction &MF = DAG.getMachineFunction(); |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4375 | const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo(); |
| 4376 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 4377 | |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 4378 | // Ignore inlined function arguments here. |
| 4379 | DIVariable DV(Variable); |
Devang Patel | 719f6a9 | 2010-04-29 20:40:36 +0000 | [diff] [blame] | 4380 | if (DV.isInlinedFnArgument(MF.getFunction())) |
Devang Patel | a83ce98 | 2010-04-29 18:50:36 +0000 | [diff] [blame] | 4381 | return false; |
| 4382 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4383 | unsigned Reg = 0; |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 4384 | // Some arguments' frame index is recorded during argument lowering. |
| 4385 | Offset = FuncInfo.getArgumentFrameIndex(Arg); |
| 4386 | if (Offset) |
Craig Topper | 7eb46d8 | 2012-04-11 04:55:51 +0000 | [diff] [blame] | 4387 | Reg = TRI->getFrameRegister(MF); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4388 | |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 4389 | if (!Reg && N.getNode()) { |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4390 | if (N.getOpcode() == ISD::CopyFromReg) |
| 4391 | Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
| 4392 | else |
| 4393 | Reg = getTruncatedArgReg(N); |
| 4394 | if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) { |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4395 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 4396 | unsigned PR = RegInfo.getLiveInPhysReg(Reg); |
| 4397 | if (PR) |
| 4398 | Reg = PR; |
| 4399 | } |
| 4400 | } |
| 4401 | |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4402 | if (!Reg) { |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4403 | // Check if ValueMap has reg number. |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4404 | DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V); |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4405 | if (VMI != FuncInfo.ValueMap.end()) |
| 4406 | Reg = VMI->second; |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4407 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4408 | |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4409 | if (!Reg && N.getNode()) { |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4410 | // Check if frame index is available. |
| 4411 | if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode())) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4412 | if (FrameIndexSDNode *FINode = |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4413 | dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) { |
| 4414 | Reg = TRI->getFrameRegister(MF); |
| 4415 | Offset = FINode->getIndex(); |
| 4416 | } |
Devang Patel | 8bc9ef7 | 2010-11-02 17:19:03 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | if (!Reg) |
| 4420 | return false; |
Devang Patel | a90b305 | 2010-11-02 17:01:30 +0000 | [diff] [blame] | 4421 | |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4422 | MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(), |
| 4423 | TII->get(TargetOpcode::DBG_VALUE)) |
Evan Cheng | a36acad | 2010-04-29 06:33:38 +0000 | [diff] [blame] | 4424 | .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4425 | FuncInfo.ArgDbgValues.push_back(&*MIB); |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4426 | return true; |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 4427 | } |
Chris Lattner | f031e8a | 2010-01-01 03:32:16 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 4429 | // VisualStudio defines setjmp as _setjmp |
Michael J. Spencer | 1f40960 | 2010-09-24 19:48:47 +0000 | [diff] [blame] | 4430 | #if defined(_MSC_VER) && defined(setjmp) && \ |
| 4431 | !defined(setjmp_undefined_for_msvc) |
| 4432 | # pragma push_macro("setjmp") |
| 4433 | # undef setjmp |
| 4434 | # define setjmp_undefined_for_msvc |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 4435 | #endif |
| 4436 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4437 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 4438 | /// we want to emit this as a call to a named external function, return the name |
| 4439 | /// otherwise lower it and return null. |
| 4440 | const char * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4441 | SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 4442 | DebugLoc dl = getCurDebugLoc(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4443 | SDValue Res; |
| 4444 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4445 | switch (Intrinsic) { |
| 4446 | default: |
| 4447 | // By default, turn this into a target intrinsic node. |
| 4448 | visitTargetIntrinsic(I, Intrinsic); |
| 4449 | return 0; |
| 4450 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 4451 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 4452 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 4453 | case Intrinsic::returnaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4454 | setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4455 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4456 | return 0; |
Bill Wendling | d5d8191 | 2008-09-26 22:10:44 +0000 | [diff] [blame] | 4457 | case Intrinsic::frameaddress: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4458 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4459 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4460 | return 0; |
| 4461 | case Intrinsic::setjmp: |
Bill Wendling | c27facc | 2012-03-05 19:29:36 +0000 | [diff] [blame] | 4462 | return &"_setjmp"[!TLI.usesUnderscoreSetJmp()]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4463 | case Intrinsic::longjmp: |
Bill Wendling | c27facc | 2012-03-05 19:29:36 +0000 | [diff] [blame] | 4464 | return &"_longjmp"[!TLI.usesUnderscoreLongJmp()]; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4465 | case Intrinsic::memcpy: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4466 | // Assert for address < 256 since we support only user defined address |
| 4467 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4468 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4469 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4470 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4471 | < 256 && |
| 4472 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4473 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4474 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4475 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4476 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4477 | if (!Align) |
| 4478 | 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] | 4479 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4480 | DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4481 | MachinePointerInfo(I.getArgOperand(0)), |
| 4482 | MachinePointerInfo(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4483 | return 0; |
| 4484 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4485 | case Intrinsic::memset: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4486 | // Assert for address < 256 since we support only user defined address |
| 4487 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4488 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4489 | < 256 && |
| 4490 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4491 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4492 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4493 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4494 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4495 | if (!Align) |
| 4496 | 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] | 4497 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4498 | DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4499 | MachinePointerInfo(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4500 | return 0; |
| 4501 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 4502 | case Intrinsic::memmove: { |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4503 | // Assert for address < 256 since we support only user defined address |
| 4504 | // spaces. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4505 | assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4506 | < 256 && |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4507 | cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace() |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4508 | < 256 && |
| 4509 | "Unknown address space"); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4510 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 4511 | SDValue Op2 = getValue(I.getArgOperand(1)); |
| 4512 | SDValue Op3 = getValue(I.getArgOperand(2)); |
| 4513 | unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue(); |
Chandler Carruth | af23f8e | 2013-02-25 14:20:21 +0000 | [diff] [blame] | 4514 | if (!Align) |
| 4515 | 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] | 4516 | bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4517 | DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4518 | MachinePointerInfo(I.getArgOperand(0)), |
| 4519 | MachinePointerInfo(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4520 | return 0; |
| 4521 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4522 | case Intrinsic::dbg_declare: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4523 | const DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Devang Patel | ac1ceb3 | 2009-10-09 22:42:28 +0000 | [diff] [blame] | 4524 | MDNode *Variable = DI.getVariable(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4525 | const Value *Address = DI.getAddress(); |
Eric Christopher | 8f2a88d | 2012-03-15 21:33:41 +0000 | [diff] [blame] | 4526 | if (!Address || !DIVariable(Variable).Verify()) { |
| 4527 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Dale Johannesen | 8ac38f2 | 2010-02-08 21:53:27 +0000 | [diff] [blame] | 4528 | return 0; |
Eric Christopher | 8f2a88d | 2012-03-15 21:33:41 +0000 | [diff] [blame] | 4529 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4530 | |
| 4531 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 4532 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 4533 | // absolute, but not relative, values are different depending on whether |
| 4534 | // debug info exists. |
| 4535 | ++SDNodeOrder; |
Devang Patel | 3f74a11 | 2010-09-02 21:29:42 +0000 | [diff] [blame] | 4536 | |
| 4537 | // Check if address has undef value. |
| 4538 | if (isa<UndefValue>(Address) || |
| 4539 | (Address->use_empty() && !isa<Argument>(Address))) { |
Eric Christopher | 2441367 | 2012-02-23 03:39:39 +0000 | [diff] [blame] | 4540 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Devang Patel | 3f74a11 | 2010-09-02 21:29:42 +0000 | [diff] [blame] | 4541 | return 0; |
| 4542 | } |
| 4543 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4544 | SDValue &N = NodeMap[Address]; |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 4545 | if (!N.getNode() && isa<Argument>(Address)) |
| 4546 | // Check unused arguments map. |
| 4547 | N = UnusedArgNodeMap[Address]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4548 | SDDbgValue *SDV; |
| 4549 | if (N.getNode()) { |
Devang Patel | 8e741ed | 2010-09-02 21:02:27 +0000 | [diff] [blame] | 4550 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 4551 | Address = BCI->getOperand(0); |
Eric Christopher | 178606d | 2012-02-24 01:59:08 +0000 | [diff] [blame] | 4552 | // Parameters are handled specially. |
| 4553 | bool isParameter = |
| 4554 | (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable || |
| 4555 | isa<Argument>(Address)); |
| 4556 | |
Devang Patel | 8e741ed | 2010-09-02 21:02:27 +0000 | [diff] [blame] | 4557 | const AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 4558 | |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4559 | if (isParameter && !AI) { |
| 4560 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode()); |
| 4561 | if (FINode) |
| 4562 | // Byval parameter. We have a frame index at this point. |
| 4563 | SDV = DAG.getDbgValue(Variable, FINode->getIndex(), |
| 4564 | 0, dl, SDNodeOrder); |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4565 | else { |
Devang Patel | 227dfdb | 2011-05-16 21:24:05 +0000 | [diff] [blame] | 4566 | // Address is an argument, so try to emit its dbg value using |
| 4567 | // virtual register info from the FuncInfo.ValueMap. |
| 4568 | EmitFuncArgumentDbgValue(Address, Variable, 0, N); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4569 | return 0; |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4570 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4571 | } else if (AI) |
| 4572 | SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(), |
| 4573 | 0, dl, SDNodeOrder); |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4574 | else { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4575 | // Can't do anything with other non-AI cases yet. |
Eric Christopher | 2441367 | 2012-02-23 03:39:39 +0000 | [diff] [blame] | 4576 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Eric Christopher | 178606d | 2012-02-24 01:59:08 +0000 | [diff] [blame] | 4577 | DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t"); |
| 4578 | DEBUG(Address->dump()); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4579 | return 0; |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 4580 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4581 | DAG.AddDbgValue(SDV, N.getNode(), isParameter); |
| 4582 | } else { |
Gabor Greif | fb4032f | 2010-10-01 10:32:19 +0000 | [diff] [blame] | 4583 | // 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] | 4584 | // virtual register info from the FuncInfo.ValueMap. |
Devang Patel | 6cd467b | 2010-08-26 22:53:27 +0000 | [diff] [blame] | 4585 | if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) { |
Devang Patel | 1397fdc | 2010-09-15 14:48:53 +0000 | [diff] [blame] | 4586 | // If variable is pinned by a alloca in dominating bb then |
| 4587 | // use StaticAllocaMap. |
| 4588 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
Devang Patel | 27ede1b | 2010-09-15 18:13:55 +0000 | [diff] [blame] | 4589 | if (AI->getParent() != DI.getParent()) { |
| 4590 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4591 | FuncInfo.StaticAllocaMap.find(AI); |
| 4592 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 4593 | SDV = DAG.getDbgValue(Variable, SI->second, |
| 4594 | 0, dl, SDNodeOrder); |
| 4595 | DAG.AddDbgValue(SDV, 0, false); |
| 4596 | return 0; |
| 4597 | } |
Devang Patel | 1397fdc | 2010-09-15 14:48:53 +0000 | [diff] [blame] | 4598 | } |
| 4599 | } |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 4600 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Devang Patel | 6cd467b | 2010-08-26 22:53:27 +0000 | [diff] [blame] | 4601 | } |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4602 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4603 | return 0; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 4604 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4605 | case Intrinsic::dbg_value: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4606 | const DbgValueInst &DI = cast<DbgValueInst>(I); |
Devang Patel | 02f0dbd | 2010-05-07 22:04:20 +0000 | [diff] [blame] | 4607 | if (!DIVariable(DI.getVariable()).Verify()) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4608 | return 0; |
| 4609 | |
| 4610 | MDNode *Variable = DI.getVariable(); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4611 | uint64_t Offset = DI.getOffset(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4612 | const Value *V = DI.getValue(); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4613 | if (!V) |
| 4614 | return 0; |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4615 | |
| 4616 | // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder |
| 4617 | // but do not always have a corresponding SDNode built. The SDNodeOrder |
| 4618 | // absolute, but not relative, values are different depending on whether |
| 4619 | // debug info exists. |
| 4620 | ++SDNodeOrder; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4621 | SDDbgValue *SDV; |
Devang Patel | 5787124 | 2011-08-03 23:13:55 +0000 | [diff] [blame] | 4622 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) { |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4623 | SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder); |
| 4624 | DAG.AddDbgValue(SDV, 0, false); |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4625 | } else { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 4626 | // Do not use getValue() in here; we don't want to generate code at |
| 4627 | // this point if it hasn't been done yet. |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 4628 | SDValue N = NodeMap[V]; |
| 4629 | if (!N.getNode() && isa<Argument>(V)) |
| 4630 | // Check unused arguments map. |
| 4631 | N = UnusedArgNodeMap[V]; |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4632 | if (N.getNode()) { |
Devang Patel | 78a06e5 | 2010-08-25 20:39:26 +0000 | [diff] [blame] | 4633 | if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) { |
Evan Cheng | 9e8a2b9 | 2010-04-29 01:40:30 +0000 | [diff] [blame] | 4634 | SDV = DAG.getDbgValue(Variable, N.getNode(), |
| 4635 | N.getResNo(), Offset, dl, SDNodeOrder); |
| 4636 | DAG.AddDbgValue(SDV, N.getNode(), false); |
| 4637 | } |
Devang Patel | a778f5c | 2011-02-18 22:43:42 +0000 | [diff] [blame] | 4638 | } else if (!V->use_empty() ) { |
Dale Johannesen | bdc09d9 | 2010-07-16 00:02:08 +0000 | [diff] [blame] | 4639 | // Do not call getValue(V) yet, as we don't want to generate code. |
| 4640 | // Remember it for later. |
| 4641 | DanglingDebugInfo DDI(&DI, dl, SDNodeOrder); |
| 4642 | DanglingDebugInfoMap[V] = DDI; |
Devang Patel | 0991dfb | 2010-08-27 22:25:51 +0000 | [diff] [blame] | 4643 | } else { |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4644 | // 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] | 4645 | // data available is an unreferenced parameter. |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 4646 | DEBUG(dbgs() << "Dropping debug info for " << DI << "\n"); |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 4647 | } |
Devang Patel | 0019034 | 2010-03-15 19:15:44 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | // Build a debug info table entry. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4651 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4652 | V = BCI->getOperand(0); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 4653 | const AllocaInst *AI = dyn_cast<AllocaInst>(V); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4654 | // Don't handle byval struct arguments or VLAs, for example. |
Eric Christopher | 7e1e18f | 2012-03-26 06:10:32 +0000 | [diff] [blame] | 4655 | if (!AI) { |
Eric Christopher | 9fc5c83 | 2012-03-28 07:34:36 +0000 | [diff] [blame] | 4656 | DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n"); |
| 4657 | DEBUG(dbgs() << " Last seen at:\n " << *V << "\n"); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4658 | return 0; |
Eric Christopher | 7e1e18f | 2012-03-26 06:10:32 +0000 | [diff] [blame] | 4659 | } |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4660 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 4661 | FuncInfo.StaticAllocaMap.find(AI); |
| 4662 | if (SI == FuncInfo.StaticAllocaMap.end()) |
| 4663 | return 0; // VLAs. |
| 4664 | int FI = SI->second; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 4665 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4666 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
| 4667 | if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo()) |
| 4668 | MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc()); |
Dale Johannesen | 904c2fa | 2010-02-01 19:54:53 +0000 | [diff] [blame] | 4669 | return 0; |
| 4670 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4671 | |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 4672 | case Intrinsic::eh_typeid_for: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4673 | // Find the type id for the given typeinfo. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4674 | GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0)); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4675 | unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV); |
| 4676 | Res = DAG.getConstant(TypeID, MVT::i32); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4677 | setValue(&I, Res); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4678 | return 0; |
| 4679 | } |
| 4680 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4681 | case Intrinsic::eh_return_i32: |
| 4682 | case Intrinsic::eh_return_i64: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4683 | DAG.getMachineFunction().getMMI().setCallsEHReturn(true); |
| 4684 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl, |
| 4685 | MVT::Other, |
| 4686 | getControlRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4687 | getValue(I.getArgOperand(0)), |
| 4688 | getValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4689 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4690 | case Intrinsic::eh_unwind_init: |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4691 | DAG.getMachineFunction().getMMI().setCallsUnwindInit(true); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4692 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4693 | case Intrinsic::eh_dwarf_cfa: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4694 | SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), dl, |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 4695 | TLI.getPointerTy()); |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4696 | SDValue Offset = DAG.getNode(ISD::ADD, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4697 | TLI.getPointerTy(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 4698 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4699 | TLI.getPointerTy()), |
| 4700 | CfaArg); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4701 | SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl, |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4702 | TLI.getPointerTy(), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4703 | DAG.getConstant(0, TLI.getPointerTy())); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4704 | setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), |
| 4705 | FA, Offset)); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 4706 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4707 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4708 | case Intrinsic::eh_sjlj_callsite: { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4709 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4710 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0)); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4711 | assert(CI && "Non-constant call site value in eh.sjlj.callsite!"); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4712 | assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4713 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 4714 | MMI.setCurrentCallSite(CI->getZExtValue()); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4715 | return 0; |
| 4716 | } |
Bill Wendling | 6ef9417 | 2011-09-28 03:36:43 +0000 | [diff] [blame] | 4717 | case Intrinsic::eh_sjlj_functioncontext: { |
| 4718 | // Get and store the index of the function context. |
| 4719 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
Bill Wendling | adbf7b2 | 2011-09-28 03:52:41 +0000 | [diff] [blame] | 4720 | AllocaInst *FnCtx = |
| 4721 | cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts()); |
Bill Wendling | 6ef9417 | 2011-09-28 03:36:43 +0000 | [diff] [blame] | 4722 | int FI = FuncInfo.StaticAllocaMap[FnCtx]; |
| 4723 | MFI->setFunctionContextIndex(FI); |
| 4724 | return 0; |
| 4725 | } |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4726 | case Intrinsic::eh_sjlj_setjmp: { |
Bill Wendling | ce370cf | 2011-10-07 21:25:38 +0000 | [diff] [blame] | 4727 | SDValue Ops[2]; |
| 4728 | Ops[0] = getRoot(); |
| 4729 | Ops[1] = getValue(I.getArgOperand(0)); |
| 4730 | SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, dl, |
| 4731 | DAG.getVTList(MVT::i32, MVT::Other), |
| 4732 | Ops, 2); |
| 4733 | setValue(&I, Op.getValue(0)); |
| 4734 | DAG.setRoot(Op.getValue(1)); |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4735 | return 0; |
| 4736 | } |
Jim Grosbach | 5eb1951 | 2010-05-22 01:06:18 +0000 | [diff] [blame] | 4737 | case Intrinsic::eh_sjlj_longjmp: { |
Jim Grosbach | 23ff7cf | 2010-05-26 20:22:18 +0000 | [diff] [blame] | 4738 | DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, dl, MVT::Other, |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 4739 | getRoot(), getValue(I.getArgOperand(0)))); |
| 4740 | return 0; |
| 4741 | } |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 4742 | |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4743 | case Intrinsic::x86_mmx_pslli_w: |
| 4744 | case Intrinsic::x86_mmx_pslli_d: |
| 4745 | case Intrinsic::x86_mmx_pslli_q: |
| 4746 | case Intrinsic::x86_mmx_psrli_w: |
| 4747 | case Intrinsic::x86_mmx_psrli_d: |
| 4748 | case Intrinsic::x86_mmx_psrli_q: |
| 4749 | case Intrinsic::x86_mmx_psrai_w: |
| 4750 | case Intrinsic::x86_mmx_psrai_d: { |
| 4751 | SDValue ShAmt = getValue(I.getArgOperand(1)); |
| 4752 | if (isa<ConstantSDNode>(ShAmt)) { |
| 4753 | visitTargetIntrinsic(I, Intrinsic); |
| 4754 | return 0; |
| 4755 | } |
| 4756 | unsigned NewIntrinsic = 0; |
| 4757 | EVT ShAmtVT = MVT::v2i32; |
| 4758 | switch (Intrinsic) { |
| 4759 | case Intrinsic::x86_mmx_pslli_w: |
| 4760 | NewIntrinsic = Intrinsic::x86_mmx_psll_w; |
| 4761 | break; |
| 4762 | case Intrinsic::x86_mmx_pslli_d: |
| 4763 | NewIntrinsic = Intrinsic::x86_mmx_psll_d; |
| 4764 | break; |
| 4765 | case Intrinsic::x86_mmx_pslli_q: |
| 4766 | NewIntrinsic = Intrinsic::x86_mmx_psll_q; |
| 4767 | break; |
| 4768 | case Intrinsic::x86_mmx_psrli_w: |
| 4769 | NewIntrinsic = Intrinsic::x86_mmx_psrl_w; |
| 4770 | break; |
| 4771 | case Intrinsic::x86_mmx_psrli_d: |
| 4772 | NewIntrinsic = Intrinsic::x86_mmx_psrl_d; |
| 4773 | break; |
| 4774 | case Intrinsic::x86_mmx_psrli_q: |
| 4775 | NewIntrinsic = Intrinsic::x86_mmx_psrl_q; |
| 4776 | break; |
| 4777 | case Intrinsic::x86_mmx_psrai_w: |
| 4778 | NewIntrinsic = Intrinsic::x86_mmx_psra_w; |
| 4779 | break; |
| 4780 | case Intrinsic::x86_mmx_psrai_d: |
| 4781 | NewIntrinsic = Intrinsic::x86_mmx_psra_d; |
| 4782 | break; |
| 4783 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 4784 | } |
| 4785 | |
| 4786 | // The vector shift intrinsics with scalars uses 32b shift amounts but |
| 4787 | // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits |
| 4788 | // to be zero. |
| 4789 | // We must do this early because v2i32 is not a legal type. |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4790 | SDValue ShOps[2]; |
| 4791 | ShOps[0] = ShAmt; |
| 4792 | ShOps[1] = DAG.getConstant(0, MVT::i32); |
| 4793 | ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2); |
| 4794 | EVT DestVT = TLI.getValueType(I.getType()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4795 | ShAmt = DAG.getNode(ISD::BITCAST, dl, DestVT, ShAmt); |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4796 | Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
| 4797 | DAG.getConstant(NewIntrinsic, MVT::i32), |
| 4798 | getValue(I.getArgOperand(0)), ShAmt); |
| 4799 | setValue(&I, Res); |
| 4800 | return 0; |
| 4801 | } |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4802 | case Intrinsic::x86_avx_vinsertf128_pd_256: |
| 4803 | case Intrinsic::x86_avx_vinsertf128_ps_256: |
Craig Topper | b45c969 | 2012-04-07 22:32:29 +0000 | [diff] [blame] | 4804 | case Intrinsic::x86_avx_vinsertf128_si_256: |
| 4805 | case Intrinsic::x86_avx2_vinserti128: { |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4806 | EVT DestVT = TLI.getValueType(I.getType()); |
| 4807 | EVT ElVT = TLI.getValueType(I.getArgOperand(1)->getType()); |
| 4808 | uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) * |
| 4809 | ElVT.getVectorNumElements(); |
| 4810 | Res = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, DestVT, |
| 4811 | getValue(I.getArgOperand(0)), |
| 4812 | getValue(I.getArgOperand(1)), |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4813 | DAG.getIntPtrConstant(Idx)); |
| 4814 | setValue(&I, Res); |
| 4815 | return 0; |
| 4816 | } |
| 4817 | case Intrinsic::x86_avx_vextractf128_pd_256: |
| 4818 | case Intrinsic::x86_avx_vextractf128_ps_256: |
| 4819 | case Intrinsic::x86_avx_vextractf128_si_256: |
| 4820 | case Intrinsic::x86_avx2_vextracti128: { |
Craig Topper | f6dc792 | 2012-09-05 05:48:09 +0000 | [diff] [blame] | 4821 | EVT DestVT = TLI.getValueType(I.getType()); |
| 4822 | uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) * |
| 4823 | DestVT.getVectorNumElements(); |
| 4824 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, |
| 4825 | getValue(I.getArgOperand(0)), |
| 4826 | DAG.getIntPtrConstant(Idx)); |
Pete Cooper | d18134f | 2012-02-24 03:51:49 +0000 | [diff] [blame] | 4827 | setValue(&I, Res); |
| 4828 | return 0; |
| 4829 | } |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4830 | case Intrinsic::convertff: |
| 4831 | case Intrinsic::convertfsi: |
| 4832 | case Intrinsic::convertfui: |
| 4833 | case Intrinsic::convertsif: |
| 4834 | case Intrinsic::convertuif: |
| 4835 | case Intrinsic::convertss: |
| 4836 | case Intrinsic::convertsu: |
| 4837 | case Intrinsic::convertus: |
| 4838 | case Intrinsic::convertuu: { |
| 4839 | ISD::CvtCode Code = ISD::CVT_INVALID; |
| 4840 | switch (Intrinsic) { |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 4841 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4842 | case Intrinsic::convertff: Code = ISD::CVT_FF; break; |
| 4843 | case Intrinsic::convertfsi: Code = ISD::CVT_FS; break; |
| 4844 | case Intrinsic::convertfui: Code = ISD::CVT_FU; break; |
| 4845 | case Intrinsic::convertsif: Code = ISD::CVT_SF; break; |
| 4846 | case Intrinsic::convertuif: Code = ISD::CVT_UF; break; |
| 4847 | case Intrinsic::convertss: Code = ISD::CVT_SS; break; |
| 4848 | case Intrinsic::convertsu: Code = ISD::CVT_SU; break; |
| 4849 | case Intrinsic::convertus: Code = ISD::CVT_US; break; |
| 4850 | case Intrinsic::convertuu: Code = ISD::CVT_UU; break; |
| 4851 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4852 | EVT DestVT = TLI.getValueType(I.getType()); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4853 | const Value *Op1 = I.getArgOperand(0); |
Craig Topper | 134f78c | 2012-11-24 23:05:23 +0000 | [diff] [blame] | 4854 | Res = DAG.getConvertRndSat(DestVT, dl, getValue(Op1), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4855 | DAG.getValueType(DestVT), |
| 4856 | DAG.getValueType(getValue(Op1).getValueType()), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4857 | getValue(I.getArgOperand(1)), |
| 4858 | getValue(I.getArgOperand(2)), |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4859 | Code); |
| 4860 | setValue(&I, Res); |
Mon P Wang | 77cdf30 | 2008-11-10 20:54:11 +0000 | [diff] [blame] | 4861 | return 0; |
| 4862 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4863 | case Intrinsic::powi: |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4864 | setValue(&I, ExpandPowI(dl, getValue(I.getArgOperand(0)), |
| 4865 | getValue(I.getArgOperand(1)), DAG)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4866 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4867 | case Intrinsic::log: |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4868 | setValue(&I, expandLog(dl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4869 | return 0; |
| 4870 | case Intrinsic::log2: |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4871 | setValue(&I, expandLog2(dl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4872 | return 0; |
| 4873 | case Intrinsic::log10: |
Craig Topper | 5d1e089 | 2012-11-23 18:38:31 +0000 | [diff] [blame] | 4874 | setValue(&I, expandLog10(dl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4875 | return 0; |
| 4876 | case Intrinsic::exp: |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4877 | setValue(&I, expandExp(dl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4878 | return 0; |
| 4879 | case Intrinsic::exp2: |
Craig Topper | 538cd48 | 2012-11-24 18:52:06 +0000 | [diff] [blame] | 4880 | setValue(&I, expandExp2(dl, getValue(I.getArgOperand(0)), DAG, TLI)); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 4881 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4882 | case Intrinsic::pow: |
Craig Topper | 327e4cb | 2012-11-25 08:08:58 +0000 | [diff] [blame] | 4883 | setValue(&I, expandPow(dl, getValue(I.getArgOperand(0)), |
| 4884 | getValue(I.getArgOperand(1)), DAG, TLI)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4885 | return 0; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4886 | case Intrinsic::sqrt: |
Peter Collingbourne | b34d3aa | 2012-05-28 21:48:37 +0000 | [diff] [blame] | 4887 | case Intrinsic::fabs: |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4888 | case Intrinsic::sin: |
| 4889 | case Intrinsic::cos: |
Dan Gohman | 27db99f | 2012-07-26 17:43:27 +0000 | [diff] [blame] | 4890 | case Intrinsic::floor: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4891 | case Intrinsic::ceil: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4892 | case Intrinsic::trunc: |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4893 | case Intrinsic::rint: |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4894 | case Intrinsic::nearbyint: { |
| 4895 | unsigned Opcode; |
| 4896 | switch (Intrinsic) { |
| 4897 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 4898 | case Intrinsic::sqrt: Opcode = ISD::FSQRT; break; |
| 4899 | case Intrinsic::fabs: Opcode = ISD::FABS; break; |
| 4900 | case Intrinsic::sin: Opcode = ISD::FSIN; break; |
| 4901 | case Intrinsic::cos: Opcode = ISD::FCOS; break; |
| 4902 | case Intrinsic::floor: Opcode = ISD::FFLOOR; break; |
| 4903 | case Intrinsic::ceil: Opcode = ISD::FCEIL; break; |
| 4904 | case Intrinsic::trunc: Opcode = ISD::FTRUNC; break; |
| 4905 | case Intrinsic::rint: Opcode = ISD::FRINT; break; |
| 4906 | case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break; |
| 4907 | } |
| 4908 | |
| 4909 | setValue(&I, DAG.getNode(Opcode, dl, |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 4910 | getValue(I.getArgOperand(0)).getValueType(), |
| 4911 | getValue(I.getArgOperand(0)))); |
| 4912 | return 0; |
Craig Topper | 9bd4dd7 | 2012-11-16 07:48:23 +0000 | [diff] [blame] | 4913 | } |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 4914 | case Intrinsic::fma: |
| 4915 | setValue(&I, DAG.getNode(ISD::FMA, dl, |
| 4916 | getValue(I.getArgOperand(0)).getValueType(), |
| 4917 | getValue(I.getArgOperand(0)), |
| 4918 | getValue(I.getArgOperand(1)), |
| 4919 | getValue(I.getArgOperand(2)))); |
| 4920 | return 0; |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4921 | case Intrinsic::fmuladd: { |
| 4922 | EVT VT = TLI.getValueType(I.getType()); |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 4923 | if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict && |
Lang Hames | b47ec40 | 2012-11-22 03:31:45 +0000 | [diff] [blame] | 4924 | TLI.isOperationLegalOrCustom(ISD::FMA, VT) && |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 4925 | TLI.isFMAFasterThanMulAndAdd(VT)){ |
Lang Hames | 5afba6f | 2012-06-05 19:07:46 +0000 | [diff] [blame] | 4926 | setValue(&I, DAG.getNode(ISD::FMA, dl, |
| 4927 | getValue(I.getArgOperand(0)).getValueType(), |
| 4928 | getValue(I.getArgOperand(0)), |
| 4929 | getValue(I.getArgOperand(1)), |
| 4930 | getValue(I.getArgOperand(2)))); |
| 4931 | } else { |
| 4932 | SDValue Mul = DAG.getNode(ISD::FMUL, dl, |
| 4933 | getValue(I.getArgOperand(0)).getValueType(), |
| 4934 | getValue(I.getArgOperand(0)), |
| 4935 | getValue(I.getArgOperand(1))); |
| 4936 | SDValue Add = DAG.getNode(ISD::FADD, dl, |
| 4937 | getValue(I.getArgOperand(0)).getValueType(), |
| 4938 | Mul, |
| 4939 | getValue(I.getArgOperand(2))); |
| 4940 | setValue(&I, Add); |
| 4941 | } |
| 4942 | return 0; |
| 4943 | } |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4944 | case Intrinsic::convert_to_fp16: |
| 4945 | setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4946 | MVT::i16, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4947 | return 0; |
| 4948 | case Intrinsic::convert_from_fp16: |
| 4949 | setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4950 | MVT::f32, getValue(I.getArgOperand(0)))); |
Anton Korobeynikov | be5b032 | 2010-03-14 18:42:15 +0000 | [diff] [blame] | 4951 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4952 | case Intrinsic::pcmarker: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4953 | SDValue Tmp = getValue(I.getArgOperand(0)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4954 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4955 | return 0; |
| 4956 | } |
| 4957 | case Intrinsic::readcyclecounter: { |
| 4958 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4959 | Res = DAG.getNode(ISD::READCYCLECOUNTER, dl, |
| 4960 | DAG.getVTList(MVT::i64, MVT::Other), |
| 4961 | &Op, 1); |
| 4962 | setValue(&I, Res); |
| 4963 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4964 | return 0; |
| 4965 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4966 | case Intrinsic::bswap: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4967 | setValue(&I, DAG.getNode(ISD::BSWAP, dl, |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4968 | getValue(I.getArgOperand(0)).getValueType(), |
| 4969 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4970 | return 0; |
| 4971 | case Intrinsic::cttz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4972 | SDValue Arg = getValue(I.getArgOperand(0)); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4973 | ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4974 | EVT Ty = Arg.getValueType(); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4975 | setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF, |
| 4976 | dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4977 | return 0; |
| 4978 | } |
| 4979 | case Intrinsic::ctlz: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4980 | SDValue Arg = getValue(I.getArgOperand(0)); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4981 | ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4982 | EVT Ty = Arg.getValueType(); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4983 | setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF, |
| 4984 | dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4985 | return 0; |
| 4986 | } |
| 4987 | case Intrinsic::ctpop: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 4988 | SDValue Arg = getValue(I.getArgOperand(0)); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4989 | EVT Ty = Arg.getValueType(); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 4990 | setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4991 | return 0; |
| 4992 | } |
| 4993 | case Intrinsic::stacksave: { |
| 4994 | SDValue Op = getRoot(); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 4995 | Res = DAG.getNode(ISD::STACKSAVE, dl, |
| 4996 | DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1); |
| 4997 | setValue(&I, Res); |
| 4998 | DAG.setRoot(Res.getValue(1)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4999 | return 0; |
| 5000 | } |
| 5001 | case Intrinsic::stackrestore: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5002 | Res = getValue(I.getArgOperand(0)); |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5003 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5004 | return 0; |
| 5005 | } |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 5006 | case Intrinsic::stackprotector: { |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5007 | // Emit code into the DAG to store the stack guard onto the stack. |
| 5008 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5009 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5010 | EVT PtrTy = TLI.getPointerTy(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5011 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5012 | SDValue Src = getValue(I.getArgOperand(0)); // The guard's value. |
| 5013 | AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1)); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5014 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 5015 | int FI = FuncInfo.StaticAllocaMap[Slot]; |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5016 | MFI->setStackProtectorIndex(FI); |
| 5017 | |
| 5018 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 5019 | |
| 5020 | // Store the stack protector onto the stack. |
Craig Topper | 134f78c | 2012-11-24 23:05:23 +0000 | [diff] [blame] | 5021 | Res = DAG.getStore(getRoot(), dl, Src, FIN, |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 5022 | MachinePointerInfo::getFixedStack(FI), |
| 5023 | true, false, 0); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5024 | setValue(&I, Res); |
| 5025 | DAG.setRoot(Res); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 5026 | return 0; |
| 5027 | } |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5028 | case Intrinsic::objectsize: { |
| 5029 | // 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] | 5030 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1)); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5031 | |
| 5032 | assert(CI && "Non-constant type in __builtin_object_size?"); |
| 5033 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5034 | SDValue Arg = getValue(I.getCalledValue()); |
Eric Christopher | 7e5d2ff | 2009-10-28 21:32:16 +0000 | [diff] [blame] | 5035 | EVT Ty = Arg.getValueType(); |
| 5036 | |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 5037 | if (CI->isZero()) |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5038 | Res = DAG.getConstant(-1ULL, Ty); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5039 | else |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5040 | Res = DAG.getConstant(0, Ty); |
| 5041 | |
| 5042 | setValue(&I, Res); |
Eric Christopher | 7b5e617 | 2009-10-27 00:52:25 +0000 | [diff] [blame] | 5043 | return 0; |
| 5044 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5045 | case Intrinsic::var_annotation: |
| 5046 | // Discard annotate attributes |
| 5047 | return 0; |
| 5048 | |
| 5049 | case Intrinsic::init_trampoline: { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5050 | const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5051 | |
| 5052 | SDValue Ops[6]; |
| 5053 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5054 | Ops[1] = getValue(I.getArgOperand(0)); |
| 5055 | Ops[2] = getValue(I.getArgOperand(1)); |
| 5056 | Ops[3] = getValue(I.getArgOperand(2)); |
| 5057 | Ops[4] = DAG.getSrcValue(I.getArgOperand(0)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5058 | Ops[5] = DAG.getSrcValue(F); |
| 5059 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5060 | Res = DAG.getNode(ISD::INIT_TRAMPOLINE, dl, MVT::Other, Ops, 6); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5061 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5062 | DAG.setRoot(Res); |
| 5063 | return 0; |
| 5064 | } |
| 5065 | case Intrinsic::adjust_trampoline: { |
| 5066 | setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, dl, |
| 5067 | TLI.getPointerTy(), |
| 5068 | getValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5069 | return 0; |
| 5070 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5071 | case Intrinsic::gcroot: |
| 5072 | if (GFI) { |
Bill Wendling | 95dd442 | 2012-05-01 22:50:45 +0000 | [diff] [blame] | 5073 | const Value *Alloca = I.getArgOperand(0)->stripPointerCasts(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5074 | const Constant *TypeMap = cast<Constant>(I.getArgOperand(1)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5075 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5076 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 5077 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 5078 | } |
| 5079 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5080 | case Intrinsic::gcread: |
| 5081 | case Intrinsic::gcwrite: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5082 | llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); |
Bill Wendling | d0283fa | 2009-12-22 00:40:51 +0000 | [diff] [blame] | 5083 | case Intrinsic::flt_rounds: |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5084 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5085 | return 0; |
Jakub Staszak | 9da9934 | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 5086 | |
| 5087 | case Intrinsic::expect: { |
| 5088 | // Just replace __builtin_expect(exp, c) with EXP. |
| 5089 | setValue(&I, getValue(I.getArgOperand(0))); |
| 5090 | return 0; |
| 5091 | } |
| 5092 | |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5093 | case Intrinsic::debugtrap: |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5094 | case Intrinsic::trap: { |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5095 | StringRef TrapFuncName = TM.Options.getTrapFunctionName(); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5096 | if (TrapFuncName.empty()) { |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5097 | ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ? |
| 5098 | ISD::TRAP : ISD::DEBUGTRAP; |
| 5099 | DAG.setRoot(DAG.getNode(Op, dl,MVT::Other, getRoot())); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5100 | return 0; |
| 5101 | } |
| 5102 | TargetLowering::ArgListTy Args; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5103 | TargetLowering:: |
| 5104 | CallLoweringInfo CLI(getRoot(), I.getType(), |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5105 | false, false, false, false, 0, CallingConv::C, |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 5106 | /*isTailCall=*/false, |
| 5107 | /*doesNotRet=*/false, /*isReturnValueUsed=*/true, |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5108 | DAG.getExternalSymbol(TrapFuncName.data(), TLI.getPointerTy()), |
Craig Topper | 134f78c | 2012-11-24 23:05:23 +0000 | [diff] [blame] | 5109 | Args, DAG, dl); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5110 | std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI); |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5111 | DAG.setRoot(Result.second); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5112 | return 0; |
Evan Cheng | 4da0c7c | 2011-04-08 21:37:21 +0000 | [diff] [blame] | 5113 | } |
Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 5114 | |
Bill Wendling | ef37546 | 2008-11-21 02:38:44 +0000 | [diff] [blame] | 5115 | case Intrinsic::uadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5116 | case Intrinsic::sadd_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5117 | case Intrinsic::usub_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5118 | case Intrinsic::ssub_with_overflow: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5119 | case Intrinsic::umul_with_overflow: |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5120 | case Intrinsic::smul_with_overflow: { |
| 5121 | ISD::NodeType Op; |
| 5122 | switch (Intrinsic) { |
| 5123 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 5124 | case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break; |
| 5125 | case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break; |
| 5126 | case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break; |
| 5127 | case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break; |
| 5128 | case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break; |
| 5129 | case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break; |
| 5130 | } |
| 5131 | SDValue Op1 = getValue(I.getArgOperand(0)); |
| 5132 | SDValue Op2 = getValue(I.getArgOperand(1)); |
Bill Wendling | 7cdc3c8 | 2008-11-21 02:03:52 +0000 | [diff] [blame] | 5133 | |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5134 | SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1); |
Craig Topper | 134f78c | 2012-11-24 23:05:23 +0000 | [diff] [blame] | 5135 | setValue(&I, DAG.getNode(Op, dl, VTs, Op1, Op2)); |
Craig Topper | c42e640 | 2012-04-11 04:34:11 +0000 | [diff] [blame] | 5136 | return 0; |
| 5137 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5138 | case Intrinsic::prefetch: { |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5139 | SDValue Ops[5]; |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5140 | unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5141 | Ops[0] = getRoot(); |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5142 | Ops[1] = getValue(I.getArgOperand(0)); |
| 5143 | Ops[2] = getValue(I.getArgOperand(1)); |
| 5144 | Ops[3] = getValue(I.getArgOperand(2)); |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5145 | Ops[4] = getValue(I.getArgOperand(3)); |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5146 | DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, dl, |
| 5147 | DAG.getVTList(MVT::Other), |
Bruno Cardoso Lopes | 9a76733 | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 5148 | &Ops[0], 5, |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 5149 | EVT::getIntegerVT(*Context, 8), |
| 5150 | MachinePointerInfo(I.getArgOperand(0)), |
| 5151 | 0, /* align */ |
| 5152 | false, /* volatile */ |
| 5153 | rw==0, /* read */ |
| 5154 | rw==1)); /* write */ |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5155 | return 0; |
| 5156 | } |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5157 | case Intrinsic::lifetime_start: |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5158 | case Intrinsic::lifetime_end: { |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5159 | bool IsStart = (Intrinsic == Intrinsic::lifetime_start); |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5160 | // Stack coloring is not enabled in O0, discard region information. |
| 5161 | if (TM.getOptLevel() == CodeGenOpt::None) |
| 5162 | return 0; |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5163 | |
Nadav Rotem | 9a2ae00 | 2012-09-10 08:43:23 +0000 | [diff] [blame] | 5164 | SmallVector<Value *, 4> Allocas; |
| 5165 | GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD); |
| 5166 | |
| 5167 | for (SmallVector<Value*, 4>::iterator Object = Allocas.begin(), |
| 5168 | E = Allocas.end(); Object != E; ++Object) { |
| 5169 | AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object); |
| 5170 | |
| 5171 | // Could not find an Alloca. |
| 5172 | if (!LifetimeObject) |
| 5173 | continue; |
| 5174 | |
| 5175 | int FI = FuncInfo.StaticAllocaMap[LifetimeObject]; |
| 5176 | |
| 5177 | SDValue Ops[2]; |
| 5178 | Ops[0] = getRoot(); |
| 5179 | Ops[1] = DAG.getFrameIndex(FI, TLI.getPointerTy(), true); |
| 5180 | unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END); |
| 5181 | |
| 5182 | Res = DAG.getNode(Opcode, dl, MVT::Other, Ops, 2); |
| 5183 | DAG.setRoot(Res); |
| 5184 | } |
Nadav Rotem | 5882e56 | 2013-02-01 19:25:23 +0000 | [diff] [blame] | 5185 | return 0; |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 5186 | } |
| 5187 | case Intrinsic::invariant_start: |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5188 | // Discard region information. |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5189 | setValue(&I, DAG.getUNDEF(TLI.getPointerTy())); |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5190 | return 0; |
| 5191 | case Intrinsic::invariant_end: |
Duncan Sands | f07c949 | 2009-11-10 09:08:09 +0000 | [diff] [blame] | 5192 | // Discard region information. |
| 5193 | return 0; |
Nuno Lopes | 85b4089 | 2012-06-28 22:30:12 +0000 | [diff] [blame] | 5194 | case Intrinsic::donothing: |
| 5195 | // ignore |
| 5196 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5197 | } |
| 5198 | } |
| 5199 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5200 | void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 5201 | bool isTailCall, |
| 5202 | MachineBasicBlock *LandingPad) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5203 | PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 5204 | FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 5205 | Type *RetTy = FTy->getReturnType(); |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5206 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 5207 | MCSymbol *BeginLabel = 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5208 | |
| 5209 | TargetLowering::ArgListTy Args; |
| 5210 | TargetLowering::ArgListEntry Entry; |
| 5211 | Args.reserve(CS.arg_size()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5212 | |
| 5213 | // Check whether the function can return without sret-demotion. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 5214 | SmallVector<ISD::OutputArg, 4> Outs; |
Bill Wendling | 8b62abd | 2012-12-30 13:01:51 +0000 | [diff] [blame] | 5215 | GetReturnInfo(RetTy, CS.getAttributes(), Outs, TLI); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5216 | |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5217 | bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(), |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 5218 | DAG.getMachineFunction(), |
| 5219 | FTy->isVarArg(), Outs, |
| 5220 | FTy->getContext()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5221 | |
| 5222 | SDValue DemoteStackSlot; |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5223 | int DemoteStackIdx = -100; |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5224 | |
| 5225 | if (!CanLowerReturn) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5226 | uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize( |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5227 | FTy->getReturnType()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5228 | unsigned Align = TLI.getDataLayout()->getPrefTypeAlignment( |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5229 | FTy->getReturnType()); |
| 5230 | MachineFunction &MF = DAG.getMachineFunction(); |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5231 | DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5232 | Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5233 | |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5234 | DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI.getPointerTy()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5235 | Entry.Node = DemoteStackSlot; |
| 5236 | Entry.Ty = StackSlotPtrType; |
| 5237 | Entry.isSExt = false; |
| 5238 | Entry.isZExt = false; |
| 5239 | Entry.isInReg = false; |
| 5240 | Entry.isSRet = true; |
| 5241 | Entry.isNest = false; |
| 5242 | Entry.isByVal = false; |
| 5243 | Entry.Alignment = Align; |
| 5244 | Args.push_back(Entry); |
| 5245 | RetTy = Type::getVoidTy(FTy->getContext()); |
| 5246 | } |
| 5247 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5248 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5249 | i != e; ++i) { |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 5250 | const Value *V = *i; |
| 5251 | |
| 5252 | // Skip empty types |
| 5253 | if (V->getType()->isEmptyTy()) |
| 5254 | continue; |
| 5255 | |
| 5256 | SDValue ArgNode = getValue(V); |
| 5257 | Entry.Node = ArgNode; Entry.Ty = V->getType(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5258 | |
| 5259 | unsigned attrInd = i - CS.arg_begin() + 1; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 5260 | Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt); |
| 5261 | Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt); |
| 5262 | Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg); |
| 5263 | Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet); |
| 5264 | Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest); |
| 5265 | Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5266 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 5267 | Args.push_back(Entry); |
| 5268 | } |
| 5269 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5270 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5271 | // Insert a label before the invoke call to mark the try range. This can be |
| 5272 | // used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5273 | BeginLabel = MMI.getContext().CreateTempSymbol(); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 5274 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5275 | // For SjLj, keep track of which landing pads go with which invokes |
| 5276 | // so as to maintain the ordering of pads in the LSDA. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5277 | unsigned CallSiteIndex = MMI.getCurrentCallSite(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5278 | if (CallSiteIndex) { |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5279 | MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); |
Bill Wendling | 30e6740 | 2011-10-05 22:24:35 +0000 | [diff] [blame] | 5280 | LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex); |
Bill Wendling | a8512ed | 2011-10-04 22:00:35 +0000 | [diff] [blame] | 5281 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5282 | // Now that the call site is handled, stop tracking it. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5283 | MMI.setCurrentCallSite(0); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5286 | // Both PendingLoads and PendingExports must be flushed here; |
| 5287 | // this call might not return. |
| 5288 | (void)getRoot(); |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 5289 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5292 | // Check if target-independent constraints permit a tail call here. |
| 5293 | // Target-dependent constraints are checked within TLI.LowerCallTo. |
Bill Wendling | 1a17bd2 | 2013-01-18 21:50:24 +0000 | [diff] [blame] | 5294 | if (isTailCall && !isInTailCallPosition(CS, TLI)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5295 | isTailCall = false; |
| 5296 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 5297 | TargetLowering:: |
| 5298 | CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG, |
| 5299 | getCurDebugLoc(), CS); |
| 5300 | std::pair<SDValue,SDValue> Result = TLI.LowerCallTo(CLI); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5301 | assert((isTailCall || Result.second.getNode()) && |
| 5302 | "Non-null chain expected with non-tail call!"); |
| 5303 | assert((Result.second.getNode() || !Result.first.getNode()) && |
| 5304 | "Null value expected with tail call!"); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5305 | if (Result.first.getNode()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5306 | setValue(CS.getInstruction(), Result.first); |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5307 | } else if (!CanLowerReturn && Result.second.getNode()) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5308 | // The instruction result is the result of loading from the |
| 5309 | // hidden sret parameter. |
| 5310 | SmallVector<EVT, 1> PVTs; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5311 | Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType()); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5312 | |
| 5313 | ComputeValueVTs(TLI, PtrRetTy, PVTs); |
| 5314 | assert(PVTs.size() == 1 && "Pointers should fit in one register"); |
| 5315 | EVT PtrVT = PVTs[0]; |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5316 | |
| 5317 | SmallVector<EVT, 4> RetTys; |
| 5318 | SmallVector<uint64_t, 4> Offsets; |
| 5319 | RetTy = FTy->getReturnType(); |
| 5320 | ComputeValueVTs(TLI, RetTy, RetTys, &Offsets); |
| 5321 | |
| 5322 | unsigned NumValues = RetTys.size(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5323 | SmallVector<SDValue, 4> Values(NumValues); |
| 5324 | SmallVector<SDValue, 4> Chains(NumValues); |
| 5325 | |
| 5326 | for (unsigned i = 0; i < NumValues; ++i) { |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5327 | SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, |
| 5328 | DemoteStackSlot, |
| 5329 | DAG.getConstant(Offsets[i], PtrVT)); |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5330 | SDValue L = DAG.getLoad(RetTys[i], getCurDebugLoc(), Result.second, Add, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5331 | MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5332 | false, false, false, 1); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5333 | Values[i] = L; |
| 5334 | Chains[i] = L.getValue(1); |
| 5335 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5336 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5337 | SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), |
| 5338 | MVT::Other, &Chains[0], NumValues); |
| 5339 | PendingLoads.push_back(Chain); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5340 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 5341 | setValue(CS.getInstruction(), |
| 5342 | DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(), |
| 5343 | DAG.getVTList(&RetTys[0], RetTys.size()), |
Eli Friedman | 2db0e9e | 2012-05-25 00:09:29 +0000 | [diff] [blame] | 5344 | &Values[0], Values.size())); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 5345 | } |
Bill Wendling | e80ae83 | 2009-12-22 00:50:32 +0000 | [diff] [blame] | 5346 | |
Evan Cheng | c249e48 | 2011-04-01 19:57:01 +0000 | [diff] [blame] | 5347 | // Assign order to nodes here. If the call does not produce a result, it won't |
| 5348 | // be mapped to a SDNode and visit() will not assign it an order number. |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5349 | if (!Result.second.getNode()) { |
Evan Cheng | c249e48 | 2011-04-01 19:57:01 +0000 | [diff] [blame] | 5350 | // As a special case, a null chain means that a tail call has been emitted and |
| 5351 | // the DAG root is already updated. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5352 | HasTailCall = true; |
Evan Cheng | 8380c03 | 2011-04-01 19:42:22 +0000 | [diff] [blame] | 5353 | ++SDNodeOrder; |
| 5354 | AssignOrderingToNode(DAG.getRoot().getNode()); |
| 5355 | } else { |
| 5356 | DAG.setRoot(Result.second); |
| 5357 | ++SDNodeOrder; |
| 5358 | AssignOrderingToNode(Result.second.getNode()); |
| 5359 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5360 | |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5361 | if (LandingPad) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5362 | // Insert a label at the end of the invoke call to mark the try range. This |
| 5363 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5364 | MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol(); |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 5365 | DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5366 | |
| 5367 | // Inform MachineModuleInfo of range. |
Chris Lattner | 512063d | 2010-04-05 06:19:28 +0000 | [diff] [blame] | 5368 | MMI.addInvoke(LandingPad, BeginLabel, EndLabel); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5369 | } |
| 5370 | } |
| 5371 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5372 | /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the |
| 5373 | /// value is equal or not-equal to zero. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5374 | static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) { |
| 5375 | 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] | 5376 | UI != E; ++UI) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5377 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5378 | if (IC->isEquality()) |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5379 | if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5380 | if (C->isNullValue()) |
| 5381 | continue; |
| 5382 | // Unknown instruction. |
| 5383 | return false; |
| 5384 | } |
| 5385 | return true; |
| 5386 | } |
| 5387 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5388 | static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5389 | Type *LoadTy, |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5390 | SelectionDAGBuilder &Builder) { |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5391 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5392 | // Check to see if this load can be trivially constant folded, e.g. if the |
| 5393 | // input is from a string literal. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5394 | if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5395 | // Cast pointer to the type we really want to load. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5396 | LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput), |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5397 | PointerType::getUnqual(LoadTy)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5398 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5399 | if (const Constant *LoadCst = |
| 5400 | ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput), |
| 5401 | Builder.TD)) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5402 | return Builder.getValue(LoadCst); |
| 5403 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5404 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5405 | // Otherwise, we have to emit the load. If the pointer is to unfoldable but |
| 5406 | // still constant memory, the input chain can be the entry node. |
| 5407 | SDValue Root; |
| 5408 | bool ConstantMemory = false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5409 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5410 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 5411 | if (Builder.AA->pointsToConstantMemory(PtrVal)) { |
| 5412 | Root = Builder.DAG.getEntryNode(); |
| 5413 | ConstantMemory = true; |
| 5414 | } else { |
| 5415 | // Do not serialize non-volatile loads against each other. |
| 5416 | Root = Builder.DAG.getRoot(); |
| 5417 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5418 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5419 | SDValue Ptr = Builder.getValue(PtrVal); |
| 5420 | SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 5421 | Ptr, MachinePointerInfo(PtrVal), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5422 | false /*volatile*/, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5423 | false /*nontemporal*/, |
| 5424 | false /*isinvariant*/, 1 /* align=1 */); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5425 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5426 | if (!ConstantMemory) |
| 5427 | Builder.PendingLoads.push_back(LoadVal.getValue(1)); |
| 5428 | return LoadVal; |
| 5429 | } |
| 5430 | |
| 5431 | |
| 5432 | /// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form. |
| 5433 | /// If so, return true and lower it, otherwise return false and it will be |
| 5434 | /// lowered like a normal call. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5435 | bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) { |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5436 | // Verify that the prototype makes sense. int memcmp(void*,void*,size_t) |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 5437 | if (I.getNumArgOperands() != 3) |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5438 | return false; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5439 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5440 | const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 5441 | if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() || |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5442 | !I.getArgOperand(2)->getType()->isIntegerTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 5443 | !I.getType()->isIntegerTy()) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5444 | return false; |
| 5445 | |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5446 | const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2)); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5447 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5448 | // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 |
| 5449 | // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5450 | if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) { |
| 5451 | bool ActuallyDoIt = true; |
| 5452 | MVT LoadVT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5453 | Type *LoadTy; |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5454 | switch (Size->getZExtValue()) { |
| 5455 | default: |
| 5456 | LoadVT = MVT::Other; |
| 5457 | LoadTy = 0; |
| 5458 | ActuallyDoIt = false; |
| 5459 | break; |
| 5460 | case 2: |
| 5461 | LoadVT = MVT::i16; |
| 5462 | LoadTy = Type::getInt16Ty(Size->getContext()); |
| 5463 | break; |
| 5464 | case 4: |
| 5465 | LoadVT = MVT::i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5466 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5467 | break; |
| 5468 | case 8: |
| 5469 | LoadVT = MVT::i64; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5470 | LoadTy = Type::getInt64Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5471 | break; |
| 5472 | /* |
| 5473 | case 16: |
| 5474 | LoadVT = MVT::v4i32; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5475 | LoadTy = Type::getInt32Ty(Size->getContext()); |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5476 | LoadTy = VectorType::get(LoadTy, 4); |
| 5477 | break; |
| 5478 | */ |
| 5479 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5480 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5481 | // This turns into unaligned loads. We only do this if the target natively |
| 5482 | // supports the MVT we'll be loading or if it is small enough (<= 4) that |
| 5483 | // we'll only produce a small number of byte loads. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5484 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5485 | // Require that we can find a legal MVT, and only do this if the target |
| 5486 | // supports unaligned loads of that type. Expanding into byte loads would |
| 5487 | // bloat the code. |
| 5488 | if (ActuallyDoIt && Size->getZExtValue() > 4) { |
| 5489 | // TODO: Handle 5 byte compare as 4-byte + 1 byte. |
| 5490 | // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. |
| 5491 | if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT)) |
| 5492 | ActuallyDoIt = false; |
| 5493 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5494 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5495 | if (ActuallyDoIt) { |
| 5496 | SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this); |
| 5497 | SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this); |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5498 | |
Chris Lattner | 04b091a | 2009-12-24 01:07:17 +0000 | [diff] [blame] | 5499 | SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal, |
| 5500 | ISD::SETNE); |
| 5501 | EVT CallVT = TLI.getValueType(I.getType(), true); |
| 5502 | setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT)); |
| 5503 | return true; |
| 5504 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5505 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5506 | |
| 5507 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5508 | return false; |
| 5509 | } |
| 5510 | |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5511 | /// visitUnaryFloatCall - If a call instruction is a unary floating-point |
| 5512 | /// operation (as expected), translate it to an SDNode with the specified opcode |
| 5513 | /// and return true. |
| 5514 | bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I, |
| 5515 | unsigned Opcode) { |
| 5516 | // Sanity check that it really is a unary floating-point call. |
| 5517 | if (I.getNumArgOperands() != 1 || |
| 5518 | !I.getArgOperand(0)->getType()->isFloatingPointTy() || |
| 5519 | I.getType() != I.getArgOperand(0)->getType() || |
| 5520 | !I.onlyReadsMemory()) |
| 5521 | return false; |
| 5522 | |
| 5523 | SDValue Tmp = getValue(I.getArgOperand(0)); |
| 5524 | setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), Tmp.getValueType(), Tmp)); |
| 5525 | return true; |
| 5526 | } |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5527 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5528 | void SelectionDAGBuilder::visitCall(const CallInst &I) { |
Chris Lattner | 598751e | 2010-07-05 05:36:21 +0000 | [diff] [blame] | 5529 | // Handle inline assembly differently. |
| 5530 | if (isa<InlineAsm>(I.getCalledValue())) { |
| 5531 | visitInlineAsm(&I); |
| 5532 | return; |
| 5533 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5534 | |
Michael J. Spencer | 391b43b | 2010-10-21 20:49:23 +0000 | [diff] [blame] | 5535 | MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); |
Michael J. Spencer | c9c137b | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 5536 | ComputeUsesVAFloatArgument(I, &MMI); |
Michael J. Spencer | 391b43b | 2010-10-21 20:49:23 +0000 | [diff] [blame] | 5537 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5538 | const char *RenameFn = 0; |
| 5539 | if (Function *F = I.getCalledFunction()) { |
| 5540 | if (F->isDeclaration()) { |
Chris Lattner | 598751e | 2010-07-05 05:36:21 +0000 | [diff] [blame] | 5541 | if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 5542 | if (unsigned IID = II->getIntrinsicID(F)) { |
| 5543 | RenameFn = visitIntrinsicCall(I, IID); |
| 5544 | if (!RenameFn) |
| 5545 | return; |
| 5546 | } |
| 5547 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5548 | if (unsigned IID = F->getIntrinsicID()) { |
| 5549 | RenameFn = visitIntrinsicCall(I, IID); |
| 5550 | if (!RenameFn) |
| 5551 | return; |
| 5552 | } |
| 5553 | } |
| 5554 | |
| 5555 | // Check for well-known libc/libm calls. If the function is internal, it |
| 5556 | // can't be a library call. |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5557 | LibFunc::Func Func; |
| 5558 | if (!F->hasLocalLinkage() && F->hasName() && |
| 5559 | LibInfo->getLibFunc(F->getName(), Func) && |
| 5560 | LibInfo->hasOptimizedCodeGen(Func)) { |
| 5561 | switch (Func) { |
| 5562 | default: break; |
| 5563 | case LibFunc::copysign: |
| 5564 | case LibFunc::copysignf: |
| 5565 | case LibFunc::copysignl: |
Gabor Greif | 37387d5 | 2010-06-30 12:55:46 +0000 | [diff] [blame] | 5566 | if (I.getNumArgOperands() == 2 && // Basic sanity checks. |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5567 | I.getArgOperand(0)->getType()->isFloatingPointTy() && |
| 5568 | I.getType() == I.getArgOperand(0)->getType() && |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5569 | I.getType() == I.getArgOperand(1)->getType() && |
| 5570 | I.onlyReadsMemory()) { |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5571 | SDValue LHS = getValue(I.getArgOperand(0)); |
| 5572 | SDValue RHS = getValue(I.getArgOperand(1)); |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 5573 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(), |
| 5574 | LHS.getValueType(), LHS, RHS)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5575 | return; |
| 5576 | } |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5577 | break; |
| 5578 | case LibFunc::fabs: |
| 5579 | case LibFunc::fabsf: |
| 5580 | case LibFunc::fabsl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5581 | if (visitUnaryFloatCall(I, ISD::FABS)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5582 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5583 | break; |
| 5584 | case LibFunc::sin: |
| 5585 | case LibFunc::sinf: |
| 5586 | case LibFunc::sinl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5587 | if (visitUnaryFloatCall(I, ISD::FSIN)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5588 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5589 | break; |
| 5590 | case LibFunc::cos: |
| 5591 | case LibFunc::cosf: |
| 5592 | case LibFunc::cosl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5593 | if (visitUnaryFloatCall(I, ISD::FCOS)) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5594 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5595 | break; |
| 5596 | case LibFunc::sqrt: |
| 5597 | case LibFunc::sqrtf: |
| 5598 | case LibFunc::sqrtl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5599 | if (visitUnaryFloatCall(I, ISD::FSQRT)) |
Dale Johannesen | 52fb79b | 2009-09-25 17:23:22 +0000 | [diff] [blame] | 5600 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5601 | break; |
| 5602 | case LibFunc::floor: |
| 5603 | case LibFunc::floorf: |
| 5604 | case LibFunc::floorl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5605 | if (visitUnaryFloatCall(I, ISD::FFLOOR)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5606 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5607 | break; |
| 5608 | case LibFunc::nearbyint: |
| 5609 | case LibFunc::nearbyintf: |
| 5610 | case LibFunc::nearbyintl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5611 | if (visitUnaryFloatCall(I, ISD::FNEARBYINT)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5612 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5613 | break; |
| 5614 | case LibFunc::ceil: |
| 5615 | case LibFunc::ceilf: |
| 5616 | case LibFunc::ceill: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5617 | if (visitUnaryFloatCall(I, ISD::FCEIL)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5618 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5619 | break; |
| 5620 | case LibFunc::rint: |
| 5621 | case LibFunc::rintf: |
| 5622 | case LibFunc::rintl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5623 | if (visitUnaryFloatCall(I, ISD::FRINT)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5624 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5625 | break; |
| 5626 | case LibFunc::trunc: |
| 5627 | case LibFunc::truncf: |
| 5628 | case LibFunc::truncl: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5629 | if (visitUnaryFloatCall(I, ISD::FTRUNC)) |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 5630 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5631 | break; |
| 5632 | case LibFunc::log2: |
| 5633 | case LibFunc::log2f: |
| 5634 | case LibFunc::log2l: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5635 | if (visitUnaryFloatCall(I, ISD::FLOG2)) |
Owen Anderson | 4e0adfa | 2011-12-15 00:54:12 +0000 | [diff] [blame] | 5636 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5637 | break; |
| 5638 | case LibFunc::exp2: |
| 5639 | case LibFunc::exp2f: |
| 5640 | case LibFunc::exp2l: |
Bob Wilson | 53624a2 | 2012-08-03 23:29:17 +0000 | [diff] [blame] | 5641 | if (visitUnaryFloatCall(I, ISD::FEXP2)) |
Owen Anderson | 4e0adfa | 2011-12-15 00:54:12 +0000 | [diff] [blame] | 5642 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5643 | break; |
| 5644 | case LibFunc::memcmp: |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 5645 | if (visitMemCmpCall(I)) |
| 5646 | return; |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 5647 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5648 | } |
| 5649 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5650 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5651 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5652 | SDValue Callee; |
| 5653 | if (!RenameFn) |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 5654 | Callee = getValue(I.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5655 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 5656 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5657 | |
Bill Wendling | 0d58013 | 2009-12-23 01:28:19 +0000 | [diff] [blame] | 5658 | // Check if we can potentially perform a tail call. More detailed checking is |
| 5659 | // be done within LowerCallTo, after more information about the call is known. |
Evan Cheng | 11e6793 | 2010-01-26 23:13:04 +0000 | [diff] [blame] | 5660 | LowerCallTo(&I, Callee, I.isTailCall()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5661 | } |
| 5662 | |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5663 | namespace { |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5664 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5665 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 5666 | /// lowering. |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5667 | class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo { |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 5668 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5669 | /// CallOperand - If this is the result output operand or a clobber |
| 5670 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 5671 | /// This gets modified as the asm is processed. |
| 5672 | SDValue CallOperand; |
| 5673 | |
| 5674 | /// AssignedRegs - If this is a register or register class operand, this |
| 5675 | /// contains the set of register corresponding to the operand. |
| 5676 | RegsForValue AssignedRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5677 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5678 | explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5679 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 5680 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5681 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5682 | /// getCallOperandValEVT - Return the EVT of the Value* that this operand |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5683 | /// corresponds to. If there is no Value* for this operand, it returns |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5684 | /// MVT::Other. |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5685 | EVT getCallOperandValEVT(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5686 | const TargetLowering &TLI, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5687 | const DataLayout *TD) const { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5688 | if (CallOperandVal == 0) return MVT::Other; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5689 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5690 | if (isa<BasicBlock>(CallOperandVal)) |
| 5691 | return TLI.getPointerTy(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5692 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5693 | llvm::Type *OpTy = CallOperandVal->getType(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5694 | |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5695 | // FIXME: code duplicated from TargetLowering::ParseConstraints(). |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5696 | // If this is an indirect operand, the operand is a pointer to the |
| 5697 | // accessed type. |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5698 | if (isIndirect) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5699 | llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5700 | if (!PtrTy) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 5701 | report_fatal_error("Indirect operand for inline asm not a pointer!"); |
Bob Wilson | e261b0c | 2009-12-22 18:34:19 +0000 | [diff] [blame] | 5702 | OpTy = PtrTy->getElementType(); |
| 5703 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5704 | |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5705 | // Look for vector wrapped in a struct. e.g. { <16 x i8> }. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5706 | if (StructType *STy = dyn_cast<StructType>(OpTy)) |
Eric Christopher | cef81b7 | 2011-05-09 20:04:43 +0000 | [diff] [blame] | 5707 | if (STy->getNumElements() == 1) |
| 5708 | OpTy = STy->getElementType(0); |
| 5709 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5710 | // If OpTy is not a single value, it may be a struct/union that we |
| 5711 | // can tile with integers. |
| 5712 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 5713 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 5714 | switch (BitSize) { |
| 5715 | default: break; |
| 5716 | case 1: |
| 5717 | case 8: |
| 5718 | case 16: |
| 5719 | case 32: |
| 5720 | case 64: |
Chris Lattner | cfc14c1 | 2008-10-17 19:59:51 +0000 | [diff] [blame] | 5721 | case 128: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 5722 | OpTy = IntegerType::get(Context, BitSize); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5723 | break; |
| 5724 | } |
| 5725 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5726 | |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5727 | return TLI.getValueType(OpTy, true); |
| 5728 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5729 | }; |
Dan Gohman | 462f6b5 | 2010-05-29 17:53:24 +0000 | [diff] [blame] | 5730 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 5731 | typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector; |
| 5732 | |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5733 | } // end anonymous namespace |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5734 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5735 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 5736 | /// specified operand. We prefer to assign virtual registers, to allow the |
Bob Wilson | 266d945 | 2009-12-17 05:07:36 +0000 | [diff] [blame] | 5737 | /// register allocator to handle the assignment process. However, if the asm |
| 5738 | /// 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] | 5739 | /// allocation. This produces generally horrible, but correct, code. |
| 5740 | /// |
| 5741 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5742 | /// |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5743 | static void GetRegistersForValue(SelectionDAG &DAG, |
| 5744 | const TargetLowering &TLI, |
| 5745 | DebugLoc DL, |
Benjamin Kramer | 8b93ff2 | 2012-02-24 14:01:17 +0000 | [diff] [blame] | 5746 | SDISelAsmOperandInfo &OpInfo) { |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5747 | LLVMContext &Context = *DAG.getContext(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5748 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5749 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5750 | SmallVector<unsigned, 4> Regs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5751 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5752 | // If this is a constraint for a single physreg, or a constraint for a |
| 5753 | // register class, find it. |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5754 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5755 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 5756 | OpInfo.ConstraintVT); |
| 5757 | |
| 5758 | unsigned NumRegs = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5759 | if (OpInfo.ConstraintVT != MVT::Other) { |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5760 | // If this is a FP input in an integer register (or visa versa) insert a bit |
| 5761 | // cast of the input value. More generally, handle any case where the input |
| 5762 | // value disagrees with the register class we plan to stick this in. |
| 5763 | if (OpInfo.Type == InlineAsm::isInput && |
| 5764 | PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5765 | // 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] | 5766 | // types are identical size, use a bitcast to convert (e.g. two differing |
| 5767 | // vector types). |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5768 | MVT RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5769 | if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5770 | OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5771 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5772 | OpInfo.ConstraintVT = RegVT; |
| 5773 | } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { |
| 5774 | // If the input is a FP value and we want it in FP registers, do a |
| 5775 | // bitcast to the corresponding integer type. This turns an f64 value |
| 5776 | // into i64, which can be passed with two i32 values on a 32-bit |
| 5777 | // machine. |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5778 | RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits()); |
Benjamin Kramer | 7d706ed | 2011-03-26 16:35:10 +0000 | [diff] [blame] | 5779 | OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL, |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 5780 | RegVT, OpInfo.CallOperand); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5781 | OpInfo.ConstraintVT = RegVT; |
| 5782 | } |
| 5783 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5784 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5785 | NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT); |
Chris Lattner | 01426e1 | 2008-10-21 00:45:36 +0000 | [diff] [blame] | 5786 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5787 | |
Patrik Hagglund | 8963fec | 2012-12-19 12:23:01 +0000 | [diff] [blame] | 5788 | MVT RegVT; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5789 | EVT ValueVT = OpInfo.ConstraintVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5790 | |
| 5791 | // If this is a constraint for a specific physical register, like {r17}, |
| 5792 | // assign it now. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5793 | if (unsigned AssignedReg = PhysReg.first) { |
| 5794 | const TargetRegisterClass *RC = PhysReg.second; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5795 | if (OpInfo.ConstraintVT == MVT::Other) |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5796 | ValueVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5797 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5798 | // Get the actual register value type. This is important, because the user |
| 5799 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 5800 | // remember that AX is actually i16 to get the right extension. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5801 | RegVT = *RC->vt_begin(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5802 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5803 | // This is a explicit reference to a physical register. |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5804 | Regs.push_back(AssignedReg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5805 | |
| 5806 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 5807 | if (NumRegs != 1) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5808 | TargetRegisterClass::iterator I = RC->begin(); |
| 5809 | for (; *I != AssignedReg; ++I) |
| 5810 | assert(I != RC->end() && "Didn't find reg!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5811 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5812 | // Already added the first reg. |
| 5813 | --NumRegs; ++I; |
| 5814 | for (; NumRegs; --NumRegs, ++I) { |
Chris Lattner | e2f7bf8 | 2009-03-24 15:27:37 +0000 | [diff] [blame] | 5815 | assert(I != RC->end() && "Ran out of registers to allocate!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5816 | Regs.push_back(*I); |
| 5817 | } |
| 5818 | } |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 5819 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5820 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5821 | return; |
| 5822 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5823 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5824 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 5825 | // for this reference. |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5826 | if (const TargetRegisterClass *RC = PhysReg.second) { |
| 5827 | RegVT = *RC->vt_begin(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5828 | if (OpInfo.ConstraintVT == MVT::Other) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5829 | ValueVT = RegVT; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5830 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5831 | // Create the appropriate number of virtual registers. |
| 5832 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 5833 | for (; NumRegs; --NumRegs) |
Chris Lattner | b3b4484 | 2009-03-24 15:25:07 +0000 | [diff] [blame] | 5834 | Regs.push_back(RegInfo.createVirtualRegister(RC)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5835 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 5836 | OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 5837 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5838 | } |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 5839 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5840 | // Otherwise, we couldn't allocate enough registers for this. |
| 5841 | } |
| 5842 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5843 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 5844 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5845 | void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) { |
| 5846 | const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5847 | |
| 5848 | /// ConstraintOperands - Information about all of the constraints. |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 5849 | SDISelAsmOperandInfoVector ConstraintOperands; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5850 | |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5851 | TargetLowering::AsmOperandInfoVector |
| 5852 | TargetConstraints = TLI.ParseConstraints(CS); |
| 5853 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5854 | bool hasMemory = false; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5855 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5856 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 5857 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5858 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 5859 | ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i])); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5860 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5861 | |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5862 | MVT OpVT = MVT::Other; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5863 | |
| 5864 | // Compute the value type for each operand. |
| 5865 | switch (OpInfo.Type) { |
| 5866 | case InlineAsm::isOutput: |
| 5867 | // Indirect outputs just consume an argument. |
| 5868 | if (OpInfo.isIndirect) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5869 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5870 | break; |
| 5871 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5872 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5873 | // The return value of the call is this value. As such, there is no |
| 5874 | // corresponding argument. |
Nick Lewycky | 8de3400 | 2011-09-30 22:19:53 +0000 | [diff] [blame] | 5875 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5876 | if (StructType *STy = dyn_cast<StructType>(CS.getType())) { |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5877 | OpVT = TLI.getSimpleValueType(STy->getElementType(ResNo)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5878 | } else { |
| 5879 | assert(ResNo == 0 && "Asm only has one result!"); |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5880 | OpVT = TLI.getSimpleValueType(CS.getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5881 | } |
| 5882 | ++ResNo; |
| 5883 | break; |
| 5884 | case InlineAsm::isInput: |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5885 | OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5886 | break; |
| 5887 | case InlineAsm::isClobber: |
| 5888 | // Nothing to do. |
| 5889 | break; |
| 5890 | } |
| 5891 | |
| 5892 | // If this is an input or an indirect output, process the call argument. |
| 5893 | // BasicBlocks are labels, currently appearing only in asm's. |
| 5894 | if (OpInfo.CallOperandVal) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5895 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5896 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Chris Lattner | 81249c9 | 2008-10-17 17:05:25 +0000 | [diff] [blame] | 5897 | } else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5898 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5899 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5900 | |
Patrik Hagglund | c698d3a | 2012-12-19 15:19:11 +0000 | [diff] [blame] | 5901 | OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD). |
| 5902 | getSimpleVT(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5903 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5904 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5905 | OpInfo.ConstraintVT = OpVT; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5906 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5907 | // Indirect operand accesses access memory. |
| 5908 | if (OpInfo.isIndirect) |
| 5909 | hasMemory = true; |
| 5910 | else { |
| 5911 | for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) { |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5912 | TargetLowering::ConstraintType |
| 5913 | CType = TLI.getConstraintType(OpInfo.Codes[j]); |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5914 | if (CType == TargetLowering::C_Memory) { |
| 5915 | hasMemory = true; |
| 5916 | break; |
| 5917 | } |
| 5918 | } |
| 5919 | } |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5920 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5921 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5922 | SDValue Chain, Flag; |
| 5923 | |
| 5924 | // We won't need to flush pending loads if this asm doesn't touch |
| 5925 | // memory and is nonvolatile. |
| 5926 | if (hasMemory || IA->hasSideEffects()) |
| 5927 | Chain = getRoot(); |
| 5928 | else |
| 5929 | Chain = DAG.getRoot(); |
| 5930 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5931 | // Second pass over the constraints: compute which constraint option to use |
| 5932 | // and assign registers to constraints that want a specific physreg. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 5933 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 5934 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5935 | |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5936 | // If this is an output operand with a matching input operand, look up the |
| 5937 | // matching input. If their types mismatch, e.g. one is an integer, the |
| 5938 | // other is floating point, or their sizes are different, flag it as an |
| 5939 | // error. |
| 5940 | if (OpInfo.hasMatchingInput()) { |
| 5941 | SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 5942 | |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5943 | if (OpInfo.ConstraintVT != Input.ConstraintVT) { |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 5944 | std::pair<unsigned, const TargetRegisterClass*> MatchRC = |
| 5945 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
Evan Cheng | 1dafa70 | 2011-08-23 19:17:21 +0000 | [diff] [blame] | 5946 | OpInfo.ConstraintVT); |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 5947 | std::pair<unsigned, const TargetRegisterClass*> InputRC = |
| 5948 | TLI.getRegForInlineAsmConstraint(Input.ConstraintCode, |
Evan Cheng | 1dafa70 | 2011-08-23 19:17:21 +0000 | [diff] [blame] | 5949 | Input.ConstraintVT); |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5950 | if ((OpInfo.ConstraintVT.isInteger() != |
| 5951 | Input.ConstraintVT.isInteger()) || |
Eric Christopher | 5427ede | 2011-07-14 20:13:52 +0000 | [diff] [blame] | 5952 | (MatchRC.second != InputRC.second)) { |
John Thompson | 5458474 | 2010-09-24 22:24:05 +0000 | [diff] [blame] | 5953 | report_fatal_error("Unsupported asm: input constraint" |
| 5954 | " with a matching output constraint of" |
| 5955 | " incompatible type!"); |
| 5956 | } |
| 5957 | Input.ConstraintVT = OpInfo.ConstraintVT; |
| 5958 | } |
| 5959 | } |
| 5960 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5961 | // Compute the constraint code and ConstraintType to use. |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 5962 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5963 | |
Eric Christopher | fffe363 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 5964 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5965 | OpInfo.Type == InlineAsm::isClobber) |
| 5966 | continue; |
| 5967 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5968 | // If this is a memory input, and if the operand is not indirect, do what we |
| 5969 | // need to to provide an address for the memory input. |
| 5970 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 5971 | !OpInfo.isIndirect) { |
Evan Cheng | ce1cdac | 2011-05-06 20:52:23 +0000 | [diff] [blame] | 5972 | assert((OpInfo.isMultipleAlternative || |
| 5973 | (OpInfo.Type == InlineAsm::isInput)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5974 | "Can only indirectify direct input operands!"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 5975 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5976 | // Memory operands really want the address of the value. If we don't have |
| 5977 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 5978 | // it to a stack slot. |
Eric Christopher | e0b42c0 | 2011-06-03 17:21:23 +0000 | [diff] [blame] | 5979 | // TODO: This isn't quite right. We need to handle these according to |
| 5980 | // the addressing mode that the constraint wants. Also, this may take |
| 5981 | // an additional register for the computation and we don't want that |
| 5982 | // either. |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 5983 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5984 | // If the operand is a float, integer, or vector constant, spill to a |
| 5985 | // constant pool entry to get its address. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5986 | const Value *OpVal = OpInfo.CallOperandVal; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5987 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 5988 | isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5989 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 5990 | TLI.getPointerTy()); |
| 5991 | } else { |
| 5992 | // Otherwise, create a stack slot and emit a store to it before the |
| 5993 | // asm. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 5994 | Type *Ty = OpVal->getType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5995 | uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty); |
| 5996 | unsigned Align = TLI.getDataLayout()->getPrefTypeAlignment(Ty); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5997 | MachineFunction &MF = DAG.getMachineFunction(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 5998 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5999 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 6000 | Chain = DAG.getStore(Chain, getCurDebugLoc(), |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 6001 | OpInfo.CallOperand, StackSlot, |
| 6002 | MachinePointerInfo::getFixedStack(SSFI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6003 | false, false, 0); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6004 | OpInfo.CallOperand = StackSlot; |
| 6005 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6006 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6007 | // There is no longer a Value* corresponding to this operand. |
| 6008 | OpInfo.CallOperandVal = 0; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6009 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6010 | // It is now an indirect operand. |
| 6011 | OpInfo.isIndirect = true; |
| 6012 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6013 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6014 | // If this constraint is for a specific register, allocate it before |
| 6015 | // anything else. |
| 6016 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Benjamin Kramer | 8b93ff2 | 2012-02-24 14:01:17 +0000 | [diff] [blame] | 6017 | GetRegistersForValue(DAG, TLI, getCurDebugLoc(), OpInfo); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6018 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6019 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6020 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 6021 | // to register class operands. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6022 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 6023 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6024 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6025 | // C_Register operands have already been allocated, Other/Memory don't need |
| 6026 | // to be. |
| 6027 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Benjamin Kramer | 8b93ff2 | 2012-02-24 14:01:17 +0000 | [diff] [blame] | 6028 | GetRegistersForValue(DAG, TLI, getCurDebugLoc(), OpInfo); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6029 | } |
| 6030 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6031 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 6032 | std::vector<SDValue> AsmNodeOperands; |
| 6033 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 6034 | AsmNodeOperands.push_back( |
Dan Gohman | f2d7fb3 | 2010-01-04 21:00:54 +0000 | [diff] [blame] | 6035 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 6036 | TLI.getPointerTy())); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6037 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6038 | // If we have a !srcloc metadata node associated with it, we want to attach |
| 6039 | // this to the ultimately generated inline asm machineinstr. To do this, we |
| 6040 | // pass in the third operand as this (potentially null) inline asm MDNode. |
| 6041 | const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc"); |
| 6042 | AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6043 | |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6044 | // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore |
| 6045 | // bits as operand 3. |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 6046 | unsigned ExtraInfo = 0; |
| 6047 | if (IA->hasSideEffects()) |
| 6048 | ExtraInfo |= InlineAsm::Extra_HasSideEffects; |
| 6049 | if (IA->isAlignStack()) |
| 6050 | ExtraInfo |= InlineAsm::Extra_IsAlignStack; |
Chad Rosier | 77fffa6 | 2012-09-05 22:17:43 +0000 | [diff] [blame] | 6051 | // Set the asm dialect. |
Chad Rosier | 2f1d815 | 2012-09-05 22:40:13 +0000 | [diff] [blame] | 6052 | ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect; |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6053 | |
| 6054 | // Determine if this InlineAsm MayLoad or MayStore based on the constraints. |
| 6055 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 6056 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
| 6057 | |
| 6058 | // Compute the constraint code and ConstraintType to use. |
| 6059 | TLI.ComputeConstraintToUse(OpInfo, SDValue()); |
| 6060 | |
Chad Rosier | dfa4cec | 2012-10-30 20:01:12 +0000 | [diff] [blame] | 6061 | // Ideally, we would only check against memory constraints. However, the |
| 6062 | // meaning of an other constraint can be target-specific and we can't easily |
| 6063 | // reason about it. Therefore, be conservative and set MayLoad/MayStore |
| 6064 | // for other constriants as well. |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6065 | if (OpInfo.ConstraintType == TargetLowering::C_Memory || |
| 6066 | OpInfo.ConstraintType == TargetLowering::C_Other) { |
| 6067 | if (OpInfo.Type == InlineAsm::isInput) |
| 6068 | ExtraInfo |= InlineAsm::Extra_MayLoad; |
| 6069 | else if (OpInfo.Type == InlineAsm::isOutput) |
| 6070 | ExtraInfo |= InlineAsm::Extra_MayStore; |
Eric Christopher | fffe363 | 2013-01-11 18:12:39 +0000 | [diff] [blame] | 6071 | else if (OpInfo.Type == InlineAsm::isClobber) |
| 6072 | ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore); |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 6073 | } |
| 6074 | } |
| 6075 | |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 6076 | AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo, |
| 6077 | TLI.getPointerTy())); |
Dale Johannesen | f1e309e | 2010-07-02 20:16:09 +0000 | [diff] [blame] | 6078 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6079 | // Loop over all of the inputs, copying the operand values into the |
| 6080 | // appropriate registers and processing the output regs. |
| 6081 | RegsForValue RetValRegs; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6082 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6083 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 6084 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6085 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6086 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 6087 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 6088 | |
| 6089 | switch (OpInfo.Type) { |
| 6090 | case InlineAsm::isOutput: { |
| 6091 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 6092 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 6093 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 6094 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 6095 | |
| 6096 | // Add information to the INLINEASM node to know about this output. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6097 | unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
| 6098 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6099 | TLI.getPointerTy())); |
| 6100 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 6101 | break; |
| 6102 | } |
| 6103 | |
| 6104 | // Otherwise, this is a register or register class output. |
| 6105 | |
| 6106 | // Copy the output from the appropriate register. Find a register that |
| 6107 | // we can use. |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6108 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 6109 | LLVMContext &Ctx = *DAG.getContext(); |
| 6110 | Ctx.emitError(CS.getInstruction(), |
| 6111 | "couldn't allocate output register for constraint '" + |
| 6112 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6113 | break; |
| 6114 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6115 | |
| 6116 | // If this is an indirect operand, store through the pointer after the |
| 6117 | // asm. |
| 6118 | if (OpInfo.isIndirect) { |
| 6119 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 6120 | OpInfo.CallOperandVal)); |
| 6121 | } else { |
| 6122 | // This is the result value of the call. |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 6123 | assert(!CS.getType()->isVoidTy() && "Bad inline asm!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6124 | // Concatenate this output onto the outputs list. |
| 6125 | RetValRegs.append(OpInfo.AssignedRegs); |
| 6126 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6127 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6128 | // Add information to the INLINEASM node to know that this register is |
| 6129 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 6130 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6131 | InlineAsm::Kind_RegDefEarlyClobber : |
| 6132 | InlineAsm::Kind_RegDef, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6133 | false, |
| 6134 | 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6135 | DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6136 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6137 | break; |
| 6138 | } |
| 6139 | case InlineAsm::isInput: { |
| 6140 | SDValue InOperandVal = OpInfo.CallOperand; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6141 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 6142 | if (OpInfo.isMatchingInputConstraint()) { // Matching constraint? |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6143 | // If this is required to match an output register we have already set, |
| 6144 | // just use its register. |
Chris Lattner | 58f15c4 | 2008-10-17 16:21:11 +0000 | [diff] [blame] | 6145 | unsigned OperandNo = OpInfo.getMatchedOperand(); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6146 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6147 | // Scan until we find the definition we already emitted of this operand. |
| 6148 | // When we find it, create a RegsForValue operand. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6149 | unsigned CurOp = InlineAsm::Op_FirstOperand; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6150 | for (; OperandNo; --OperandNo) { |
| 6151 | // Advance to the next operand. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6152 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6153 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6154 | assert((InlineAsm::isRegDefKind(OpFlag) || |
| 6155 | InlineAsm::isRegDefEarlyClobberKind(OpFlag) || |
| 6156 | InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?"); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6157 | CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6160 | unsigned OpFlag = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6161 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6162 | if (InlineAsm::isRegDefKind(OpFlag) || |
| 6163 | InlineAsm::isRegDefEarlyClobberKind(OpFlag)) { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6164 | // Add (OpFlag&0xffff)>>3 registers to MatchedRegs. |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6165 | if (OpInfo.isIndirect) { |
| 6166 | // This happens on gcc/testsuite/gcc.dg/pr8788-1.c |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 6167 | LLVMContext &Ctx = *DAG.getContext(); |
Chris Lattner | 6129c37 | 2010-04-08 00:09:16 +0000 | [diff] [blame] | 6168 | Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:" |
| 6169 | " don't know how to handle tied " |
| 6170 | "indirect register inputs"); |
| 6171 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6172 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6173 | RegsForValue MatchedRegs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6174 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 6175 | MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType(); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6176 | MatchedRegs.RegVTs.push_back(RegVT); |
| 6177 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 6178 | for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6179 | i != e; ++i) |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6180 | MatchedRegs.Regs.push_back |
| 6181 | (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT))); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6182 | |
| 6183 | // Use the produced MatchedRegs object to |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 6184 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6185 | Chain, &Flag, CS.getInstruction()); |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6186 | MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 6187 | true, OpInfo.getMatchedOperand(), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6188 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6189 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6190 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6191 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6192 | assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!"); |
| 6193 | assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 && |
| 6194 | "Unexpected number of operands"); |
| 6195 | // Add information to the INLINEASM node to know about this input. |
| 6196 | // See InlineAsm.h isUseOperandTiedToDef. |
| 6197 | OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag, |
| 6198 | OpInfo.getMatchedOperand()); |
| 6199 | AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag, |
| 6200 | TLI.getPointerTy())); |
| 6201 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 6202 | break; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6203 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6204 | |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6205 | // Treat indirect 'X' constraint as memory. |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6206 | if (OpInfo.ConstraintType == TargetLowering::C_Other && |
| 6207 | OpInfo.isIndirect) |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6208 | OpInfo.ConstraintType = TargetLowering::C_Memory; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6209 | |
Dale Johannesen | b5611a6 | 2010-07-13 20:17:05 +0000 | [diff] [blame] | 6210 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6211 | std::vector<SDValue> Ops; |
Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 6212 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode, |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 6213 | Ops, DAG); |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6214 | if (Ops.empty()) { |
| 6215 | LLVMContext &Ctx = *DAG.getContext(); |
| 6216 | Ctx.emitError(CS.getInstruction(), |
| 6217 | "invalid operand for inline asm constraint '" + |
| 6218 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6219 | break; |
| 6220 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6221 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6222 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6223 | unsigned ResOpType = |
| 6224 | InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6225 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6226 | TLI.getPointerTy())); |
| 6227 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 6228 | break; |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6229 | } |
Michael J. Spencer | e70c526 | 2010-10-16 08:25:21 +0000 | [diff] [blame] | 6230 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6231 | if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6232 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 6233 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 6234 | "Memory operands expect pointer values"); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6235 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6236 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6237 | unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 6238 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6239 | TLI.getPointerTy())); |
| 6240 | AsmNodeOperands.push_back(InOperandVal); |
| 6241 | break; |
| 6242 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6243 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6244 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 6245 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 6246 | "Unknown constraint type!"); |
Eric Christopher | 9eb4f8a | 2012-07-02 21:16:43 +0000 | [diff] [blame] | 6247 | |
| 6248 | // TODO: Support this. |
| 6249 | if (OpInfo.isIndirect) { |
| 6250 | LLVMContext &Ctx = *DAG.getContext(); |
| 6251 | Ctx.emitError(CS.getInstruction(), |
| 6252 | "Don't know how to handle indirect register inputs yet " |
| 6253 | "for constraint '" + Twine(OpInfo.ConstraintCode) + "'"); |
| 6254 | break; |
| 6255 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6256 | |
| 6257 | // Copy the input into the appropriate registers. |
Chris Lattner | fcd7090 | 2012-01-03 23:51:01 +0000 | [diff] [blame] | 6258 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 6259 | LLVMContext &Ctx = *DAG.getContext(); |
| 6260 | Ctx.emitError(CS.getInstruction(), |
| 6261 | "couldn't allocate input reg for constraint '" + |
| 6262 | Twine(OpInfo.ConstraintCode) + "'"); |
| 6263 | break; |
| 6264 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6265 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 6266 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(), |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6267 | Chain, &Flag, CS.getInstruction()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6268 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6269 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6270 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6271 | break; |
| 6272 | } |
| 6273 | case InlineAsm::isClobber: { |
| 6274 | // Add the clobbered value to the operand list, so that the register |
| 6275 | // allocator is aware that the physreg got clobbered. |
| 6276 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Jakob Stoklund Olesen | f792fa9 | 2011-06-27 04:08:33 +0000 | [diff] [blame] | 6277 | OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6278 | false, 0, DAG, |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6279 | AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6280 | break; |
| 6281 | } |
| 6282 | } |
| 6283 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6284 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 6285 | // 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] | 6286 | AsmNodeOperands[InlineAsm::Op_InputChain] = Chain; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6287 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6288 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 6289 | Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(), |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 6290 | DAG.getVTList(MVT::Other, MVT::Glue), |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6291 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 6292 | Flag = Chain.getValue(1); |
| 6293 | |
| 6294 | // If this asm returns a register value, copy the result from that register |
| 6295 | // and set it as the value of the call. |
| 6296 | if (!RetValRegs.Regs.empty()) { |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6297 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6298 | Chain, &Flag, CS.getInstruction()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6299 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6300 | // FIXME: Why don't we do this for inline asms with MRVs? |
| 6301 | if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6302 | EVT ResultType = TLI.getValueType(CS.getType()); |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6303 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6304 | // If any of the results of the inline asm is a vector, it may have the |
| 6305 | // wrong width/num elts. This can happen for register classes that can |
| 6306 | // contain multiple different value types. The preg or vreg allocated may |
| 6307 | // not have the same VT as was expected. Convert it to the right type |
| 6308 | // with bit_convert. |
| 6309 | if (ResultType != Val.getValueType() && Val.getValueType().isVector()) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6310 | Val = DAG.getNode(ISD::BITCAST, getCurDebugLoc(), |
Dale Johannesen | fa42dea | 2009-01-30 01:34:22 +0000 | [diff] [blame] | 6311 | ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6312 | |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6313 | } else if (ResultType != Val.getValueType() && |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6314 | ResultType.isInteger() && Val.getValueType().isInteger()) { |
| 6315 | // If a result value was tied to an input value, the computed result may |
| 6316 | // have a wider width than the expected result. Extract the relevant |
| 6317 | // portion. |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 6318 | Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val); |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6319 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6320 | |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6321 | assert(ResultType == Val.getValueType() && "Asm result value mismatch!"); |
Chris Lattner | 0c52644 | 2008-10-17 17:52:49 +0000 | [diff] [blame] | 6322 | } |
Dan Gohman | 9591573 | 2008-10-18 01:03:45 +0000 | [diff] [blame] | 6323 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6324 | setValue(CS.getInstruction(), Val); |
Dale Johannesen | ec65a7d | 2009-04-14 00:56:56 +0000 | [diff] [blame] | 6325 | // Don't need to use this as a chain in this case. |
| 6326 | if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty()) |
| 6327 | return; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6328 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6329 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6330 | std::vector<std::pair<SDValue, const Value *> > StoresToEmit; |
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 | // Process indirect outputs, first output all of the flagged copies out of |
| 6333 | // physregs. |
| 6334 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 6335 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6336 | const Value *Ptr = IndirectStoresToEmit[i].second; |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6337 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6338 | Chain, &Flag, IA); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6339 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
| 6340 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6341 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6342 | // Emit the non-flagged stores from the physregs. |
| 6343 | SmallVector<SDValue, 8> OutChains; |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6344 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) { |
| 6345 | SDValue Val = DAG.getStore(Chain, getCurDebugLoc(), |
| 6346 | StoresToEmit[i].first, |
| 6347 | getValue(StoresToEmit[i].second), |
Chris Lattner | 84bd98a | 2010-09-21 18:58:22 +0000 | [diff] [blame] | 6348 | MachinePointerInfo(StoresToEmit[i].second), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6349 | false, false, 0); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6350 | OutChains.push_back(Val); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6351 | } |
| 6352 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6353 | if (!OutChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6354 | Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6355 | &OutChains[0], OutChains.size()); |
Bill Wendling | 651ad13 | 2009-12-22 01:25:10 +0000 | [diff] [blame] | 6356 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6357 | DAG.setRoot(Chain); |
| 6358 | } |
| 6359 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6360 | void SelectionDAGBuilder::visitVAStart(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6361 | DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(), |
| 6362 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6363 | getValue(I.getArgOperand(0)), |
| 6364 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6365 | } |
| 6366 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6367 | void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6368 | const DataLayout &TD = *TLI.getDataLayout(); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 6369 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(), |
| 6370 | getRoot(), getValue(I.getOperand(0)), |
Rafael Espindola | cbeeae2 | 2010-07-11 04:01:49 +0000 | [diff] [blame] | 6371 | DAG.getSrcValue(I.getOperand(0)), |
Rafael Espindola | 9d544d0 | 2010-07-12 18:11:17 +0000 | [diff] [blame] | 6372 | TD.getABITypeAlignment(I.getType())); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6373 | setValue(&I, V); |
| 6374 | DAG.setRoot(V.getValue(1)); |
| 6375 | } |
| 6376 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6377 | void SelectionDAGBuilder::visitVAEnd(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6378 | DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(), |
| 6379 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6380 | getValue(I.getArgOperand(0)), |
| 6381 | DAG.getSrcValue(I.getArgOperand(0)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6382 | } |
| 6383 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6384 | void SelectionDAGBuilder::visitVACopy(const CallInst &I) { |
Bill Wendling | c1d3c94 | 2009-12-23 00:44:51 +0000 | [diff] [blame] | 6385 | DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(), |
| 6386 | MVT::Other, getRoot(), |
Gabor Greif | 0635f35 | 2010-06-25 09:38:13 +0000 | [diff] [blame] | 6387 | getValue(I.getArgOperand(0)), |
| 6388 | getValue(I.getArgOperand(1)), |
| 6389 | DAG.getSrcValue(I.getArgOperand(0)), |
| 6390 | DAG.getSrcValue(I.getArgOperand(1)))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6391 | } |
| 6392 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6393 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6394 | /// implementation, which just calls LowerCall. |
| 6395 | /// FIXME: When all targets are |
| 6396 | /// migrated to using LowerCall, this hook should be integrated into SDISel. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6397 | std::pair<SDValue, SDValue> |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6398 | TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6399 | // Handle all of the outgoing arguments. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6400 | CLI.Outs.clear(); |
| 6401 | CLI.OutVals.clear(); |
| 6402 | ArgListTy &Args = CLI.Args; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6403 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6404 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6405 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 6406 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 6407 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6408 | EVT VT = ValueVTs[Value]; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6409 | Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext()); |
Chris Lattner | 2a0b96c | 2008-10-18 18:49:30 +0000 | [diff] [blame] | 6410 | SDValue Op = SDValue(Args[i].Node.getNode(), |
| 6411 | Args[i].Node.getResNo() + Value); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6412 | ISD::ArgFlagsTy Flags; |
| 6413 | unsigned OriginalAlignment = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6414 | getDataLayout()->getABITypeAlignment(ArgTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6415 | |
| 6416 | if (Args[i].isZExt) |
| 6417 | Flags.setZExt(); |
| 6418 | if (Args[i].isSExt) |
| 6419 | Flags.setSExt(); |
| 6420 | if (Args[i].isInReg) |
| 6421 | Flags.setInReg(); |
| 6422 | if (Args[i].isSRet) |
| 6423 | Flags.setSRet(); |
| 6424 | if (Args[i].isByVal) { |
| 6425 | Flags.setByVal(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6426 | PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 6427 | Type *ElementTy = Ty->getElementType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6428 | Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6429 | // For ByVal, alignment should come from FE. BE will guess if this |
| 6430 | // info is not there but there are cases it cannot get right. |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6431 | unsigned FrameAlign; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6432 | if (Args[i].Alignment) |
| 6433 | FrameAlign = Args[i].Alignment; |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6434 | else |
| 6435 | FrameAlign = getByValTypeAlignment(ElementTy); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6436 | Flags.setByValAlign(FrameAlign); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6437 | } |
| 6438 | if (Args[i].isNest) |
| 6439 | Flags.setNest(); |
| 6440 | Flags.setOrigAlign(OriginalAlignment); |
| 6441 | |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6442 | MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6443 | unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6444 | SmallVector<SDValue, 4> Parts(NumParts); |
| 6445 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 6446 | |
| 6447 | if (Args[i].isSExt) |
| 6448 | ExtendKind = ISD::SIGN_EXTEND; |
| 6449 | else if (Args[i].isZExt) |
| 6450 | ExtendKind = ISD::ZERO_EXTEND; |
| 6451 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6452 | getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6453 | PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6454 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6455 | for (unsigned j = 0; j != NumParts; ++j) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6456 | // if it isn't first piece, alignment must be 1 |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 6457 | ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(), |
Manman Ren | 0a1544d | 2012-11-01 23:49:58 +0000 | [diff] [blame] | 6458 | i < CLI.NumFixedArgs, |
| 6459 | i, j*Parts[j].getValueType().getStoreSize()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6460 | if (NumParts > 1 && j == 0) |
| 6461 | MyFlags.Flags.setSplit(); |
| 6462 | else if (j != 0) |
| 6463 | MyFlags.Flags.setOrigAlign(1); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6464 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6465 | CLI.Outs.push_back(MyFlags); |
| 6466 | CLI.OutVals.push_back(Parts[j]); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6467 | } |
| 6468 | } |
| 6469 | } |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6470 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6471 | // Handle the incoming return values from the call. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6472 | CLI.Ins.clear(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6473 | SmallVector<EVT, 4> RetTys; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6474 | ComputeValueVTs(*this, CLI.RetTy, RetTys); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6475 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6476 | EVT VT = RetTys[I]; |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6477 | MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6478 | unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6479 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 6480 | ISD::InputArg MyFlags; |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6481 | MyFlags.VT = RegisterVT; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6482 | MyFlags.Used = CLI.IsReturnValueUsed; |
| 6483 | if (CLI.RetSExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6484 | MyFlags.Flags.setSExt(); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6485 | if (CLI.RetZExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6486 | MyFlags.Flags.setZExt(); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6487 | if (CLI.IsInReg) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6488 | MyFlags.Flags.setInReg(); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6489 | CLI.Ins.push_back(MyFlags); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6490 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6491 | } |
| 6492 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6493 | SmallVector<SDValue, 4> InVals; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6494 | CLI.Chain = LowerCall(CLI, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6495 | |
| 6496 | // Verify that the target's LowerCall behaved as expected. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6497 | assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6498 | "LowerCall didn't return a valid chain!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6499 | assert((!CLI.IsTailCall || InVals.empty()) && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6500 | "LowerCall emitted a return value for a tail call!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6501 | assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6502 | "LowerCall didn't emit the correct number of values!"); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6503 | |
| 6504 | // For a tail call, the return value is merely live-out and there aren't |
| 6505 | // any nodes in the DAG representing it. Return a special value to |
| 6506 | // indicate that a tail call has been emitted and no more Instructions |
| 6507 | // should be processed in the current block. |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6508 | if (CLI.IsTailCall) { |
| 6509 | CLI.DAG.setRoot(CLI.Chain); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6510 | return std::make_pair(SDValue(), SDValue()); |
| 6511 | } |
| 6512 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6513 | DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) { |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 6514 | assert(InVals[i].getNode() && |
| 6515 | "LowerCall emitted a null value!"); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6516 | assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() && |
Evan Cheng | af1871f | 2010-03-11 19:38:18 +0000 | [diff] [blame] | 6517 | "LowerCall emitted a value with the wrong type!"); |
| 6518 | }); |
| 6519 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6520 | // Collect the legal value parts into potentially illegal values |
| 6521 | // that correspond to the original function's return values. |
| 6522 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6523 | if (CLI.RetSExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6524 | AssertOp = ISD::AssertSext; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6525 | else if (CLI.RetZExt) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6526 | AssertOp = ISD::AssertZext; |
| 6527 | SmallVector<SDValue, 4> ReturnValues; |
| 6528 | unsigned CurReg = 0; |
| 6529 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6530 | EVT VT = RetTys[I]; |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6531 | MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6532 | unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6533 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6534 | ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg], |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6535 | NumRegs, RegisterVT, VT, NULL, |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 6536 | AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6537 | CurReg += NumRegs; |
| 6538 | } |
| 6539 | |
| 6540 | // For a function returning void, there is no return value. We can't create |
| 6541 | // 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] | 6542 | // that case, nothing will actually look at the value. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6543 | if (ReturnValues.empty()) |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6544 | return std::make_pair(SDValue(), CLI.Chain); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6545 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6546 | SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL, |
| 6547 | CLI.DAG.getVTList(&RetTys[0], RetTys.size()), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6548 | &ReturnValues[0], ReturnValues.size()); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 6549 | return std::make_pair(Res, CLI.Chain); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 6552 | void TargetLowering::LowerOperationWrapper(SDNode *N, |
| 6553 | SmallVectorImpl<SDValue> &Results, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6554 | SelectionDAG &DAG) const { |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 6555 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
Sanjiv Gupta | bb326bb | 2009-01-21 04:48:39 +0000 | [diff] [blame] | 6556 | if (Res.getNode()) |
| 6557 | Results.push_back(Res); |
| 6558 | } |
| 6559 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6560 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6561 | llvm_unreachable("LowerOperation not implemented for this target!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6562 | } |
| 6563 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6564 | void |
| 6565 | SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) { |
Dan Gohman | 28a1735 | 2010-07-01 01:59:43 +0000 | [diff] [blame] | 6566 | SDValue Op = getNonRegisterValue(V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6567 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 6568 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 6569 | "Copy from a reg to the same reg!"); |
| 6570 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 6571 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6572 | RegsForValue RFV(V->getContext(), TLI, Reg, V->getType()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6573 | SDValue Chain = DAG.getEntryNode(); |
Bill Wendling | f18eb58 | 2012-09-26 06:16:18 +0000 | [diff] [blame] | 6574 | RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0, V); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6575 | PendingExports.push_back(Chain); |
| 6576 | } |
| 6577 | |
| 6578 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 6579 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6580 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 6581 | /// entry block, return true. This includes arguments used by switches, since |
| 6582 | /// the switch may expand into multiple basic blocks. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6583 | static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6584 | // With FastISel active, we may be splitting blocks, so force creation |
| 6585 | // of virtual registers for all non-dead arguments. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6586 | if (FastISel) |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6587 | return A->use_empty(); |
| 6588 | |
| 6589 | const BasicBlock *Entry = A->getParent()->begin(); |
| 6590 | for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end(); |
| 6591 | UI != E; ++UI) { |
| 6592 | const User *U = *UI; |
| 6593 | if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U)) |
| 6594 | return false; // Use not in entry block. |
| 6595 | } |
| 6596 | return true; |
| 6597 | } |
| 6598 | |
Eli Bendersky | 6437d38 | 2013-02-28 23:09:18 +0000 | [diff] [blame^] | 6599 | void SelectionDAGISel::LowerArguments(const Function &F) { |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6600 | SelectionDAG &DAG = SDB->DAG; |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6601 | DebugLoc dl = SDB->getCurDebugLoc(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6602 | const DataLayout *TD = TLI.getDataLayout(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6603 | SmallVector<ISD::InputArg, 16> Ins; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6604 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6605 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6606 | // Put in an sret pointer parameter before all the other parameters. |
| 6607 | SmallVector<EVT, 1> ValueVTs; |
| 6608 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
| 6609 | |
| 6610 | // NOTE: Assuming that a pointer will never break down to more than one VT |
| 6611 | // or one register. |
| 6612 | ISD::ArgFlagsTy Flags; |
| 6613 | Flags.setSRet(); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6614 | MVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]); |
Stepan Dyatkovskiy | 661afe7 | 2012-10-10 11:37:36 +0000 | [diff] [blame] | 6615 | ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6616 | Ins.push_back(RetArg); |
| 6617 | } |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 6618 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6619 | // Set up the incoming argument description vector. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6620 | unsigned Idx = 1; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6621 | 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] | 6622 | I != E; ++I, ++Idx) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6623 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6624 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
| 6625 | bool isArgValueUsed = !I->use_empty(); |
| 6626 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 6627 | Value != NumValues; ++Value) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6628 | EVT VT = ValueVTs[Value]; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6629 | Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6630 | ISD::ArgFlagsTy Flags; |
| 6631 | unsigned OriginalAlignment = |
| 6632 | TD->getABITypeAlignment(ArgTy); |
| 6633 | |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6634 | if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6635 | Flags.setZExt(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6636 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6637 | Flags.setSExt(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6638 | if (F.getAttributes().hasAttribute(Idx, Attribute::InReg)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6639 | Flags.setInReg(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6640 | if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6641 | Flags.setSRet(); |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6642 | if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6643 | Flags.setByVal(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6644 | PointerType *Ty = cast<PointerType>(I->getType()); |
| 6645 | Type *ElementTy = Ty->getElementType(); |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6646 | Flags.setByValSize(TD->getTypeAllocSize(ElementTy)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6647 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 6648 | // 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] | 6649 | unsigned FrameAlign; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6650 | if (F.getParamAlignment(Idx)) |
| 6651 | FrameAlign = F.getParamAlignment(Idx); |
Chris Lattner | 9db20f3 | 2011-05-22 23:23:02 +0000 | [diff] [blame] | 6652 | else |
| 6653 | FrameAlign = TLI.getByValTypeAlignment(ElementTy); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6654 | Flags.setByValAlign(FrameAlign); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6655 | } |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6656 | if (F.getAttributes().hasAttribute(Idx, Attribute::Nest)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6657 | Flags.setNest(); |
| 6658 | Flags.setOrigAlign(OriginalAlignment); |
| 6659 | |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6660 | MVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6661 | unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6662 | for (unsigned i = 0; i != NumRegs; ++i) { |
Stepan Dyatkovskiy | 661afe7 | 2012-10-10 11:37:36 +0000 | [diff] [blame] | 6663 | ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed, |
| 6664 | Idx-1, i*RegisterVT.getStoreSize()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6665 | if (NumRegs > 1 && i == 0) |
| 6666 | MyFlags.Flags.setSplit(); |
| 6667 | // if it isn't first piece, alignment must be 1 |
| 6668 | else if (i > 0) |
| 6669 | MyFlags.Flags.setOrigAlign(1); |
| 6670 | Ins.push_back(MyFlags); |
| 6671 | } |
| 6672 | } |
| 6673 | } |
| 6674 | |
| 6675 | // Call the target to set up the argument values. |
| 6676 | SmallVector<SDValue, 8> InVals; |
| 6677 | SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(), |
| 6678 | F.isVarArg(), Ins, |
| 6679 | dl, DAG, InVals); |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6680 | |
| 6681 | // Verify that the target's LowerFormalArguments behaved as expected. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6682 | assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6683 | "LowerFormalArguments didn't return a valid chain!"); |
| 6684 | assert(InVals.size() == Ins.size() && |
| 6685 | "LowerFormalArguments didn't emit the correct number of values!"); |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 6686 | DEBUG({ |
| 6687 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 6688 | assert(InVals[i].getNode() && |
| 6689 | "LowerFormalArguments emitted a null value!"); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 6690 | assert(EVT(Ins[i].VT) == InVals[i].getValueType() && |
Bill Wendling | 3ea58b6 | 2009-12-22 21:35:02 +0000 | [diff] [blame] | 6691 | "LowerFormalArguments emitted a value with the wrong type!"); |
| 6692 | } |
| 6693 | }); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6694 | |
Dan Gohman | 5e86606 | 2009-08-06 15:37:27 +0000 | [diff] [blame] | 6695 | // Update the DAG with the new chain value resulting from argument lowering. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6696 | DAG.setRoot(NewRoot); |
| 6697 | |
| 6698 | // Set up the argument values. |
| 6699 | unsigned i = 0; |
| 6700 | Idx = 1; |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6701 | if (!FuncInfo->CanLowerReturn) { |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6702 | // Create a virtual register for the sret pointer, and put in a copy |
| 6703 | // from the sret argument into it. |
| 6704 | SmallVector<EVT, 1> ValueVTs; |
| 6705 | ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 6706 | MVT VT = ValueVTs[0].getSimpleVT(); |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6707 | MVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6708 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6709 | SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6710 | RegVT, VT, NULL, AssertOp); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6711 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6712 | MachineFunction& MF = SDB->DAG.getMachineFunction(); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6713 | MachineRegisterInfo& RegInfo = MF.getRegInfo(); |
| 6714 | unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)); |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 6715 | FuncInfo->DemoteRegister = SRetReg; |
Mikhail Glushenkov | b3c0199 | 2010-01-01 04:41:22 +0000 | [diff] [blame] | 6716 | NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), |
| 6717 | SRetReg, ArgValue); |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6718 | DAG.setRoot(NewRoot); |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6719 | |
Kenneth Uildriks | c158dde | 2009-11-11 19:59:24 +0000 | [diff] [blame] | 6720 | // i indexes lowered arguments. Bump it past the hidden sret argument. |
| 6721 | // Idx indexes LLVM arguments. Don't touch it. |
| 6722 | ++i; |
| 6723 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6724 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6725 | 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] | 6726 | ++I, ++Idx) { |
| 6727 | SmallVector<SDValue, 4> ArgValues; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6728 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6729 | ComputeValueVTs(TLI, I->getType(), ValueVTs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6730 | unsigned NumValues = ValueVTs.size(); |
Devang Patel | 9126c0d | 2010-06-01 19:59:01 +0000 | [diff] [blame] | 6731 | |
| 6732 | // If this argument is unused then remember its value. It is used to generate |
| 6733 | // debugging information. |
| 6734 | if (I->use_empty() && NumValues) |
| 6735 | SDB->setUnusedArgValue(I, InVals[i]); |
| 6736 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6737 | for (unsigned Val = 0; Val != NumValues; ++Val) { |
| 6738 | EVT VT = ValueVTs[Val]; |
Patrik Hagglund | dfcf33a | 2012-12-19 11:48:16 +0000 | [diff] [blame] | 6739 | MVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6740 | unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6741 | |
| 6742 | if (!I->use_empty()) { |
| 6743 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6744 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6745 | AssertOp = ISD::AssertSext; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 6746 | else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt)) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6747 | AssertOp = ISD::AssertZext; |
| 6748 | |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 6749 | ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6750 | NumParts, PartVT, VT, |
Bill Wendling | 1293130 | 2012-09-26 04:04:19 +0000 | [diff] [blame] | 6751 | NULL, AssertOp)); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6752 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6753 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6754 | i += NumParts; |
| 6755 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6756 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6757 | // We don't need to do anything else for unused arguments. |
| 6758 | if (ArgValues.empty()) |
| 6759 | continue; |
| 6760 | |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6761 | // Note down frame index. |
| 6762 | if (FrameIndexSDNode *FI = |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 6763 | dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode())) |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6764 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 6765 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6766 | SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues, |
| 6767 | SDB->getCurDebugLoc()); |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6768 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6769 | SDB->setValue(I, Res); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6770 | if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) { |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 6771 | if (LoadSDNode *LNode = |
| 6772 | dyn_cast<LoadSDNode>(Res.getOperand(0).getNode())) |
| 6773 | if (FrameIndexSDNode *FI = |
| 6774 | dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) |
| 6775 | FuncInfo->setArgumentFrameIndex(I, FI->getIndex()); |
| 6776 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6777 | |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6778 | // If this argument is live outside of the entry block, insert a copy from |
| 6779 | // 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] | 6780 | if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6781 | // If we can, though, try to skip creating an unnecessary vreg. |
| 6782 | // 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] | 6783 | // general. It's also subtly incompatible with the hacks FastISel |
| 6784 | // uses with vregs. |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6785 | unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg(); |
| 6786 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 6787 | FuncInfo->ValueMap[I] = Reg; |
| 6788 | continue; |
| 6789 | } |
| 6790 | } |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6791 | if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) { |
Eli Friedman | 23d3243 | 2011-05-05 16:53:34 +0000 | [diff] [blame] | 6792 | FuncInfo->InitializeRegForValue(I); |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 6793 | SDB->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6794 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6795 | } |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 6796 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6797 | assert(i == InVals.size() && "Argument register count mismatch!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6798 | |
| 6799 | // Finally, if the target has anything special to do, allow it to do so. |
| 6800 | // FIXME: this should insert code into the DAG! |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 6801 | EmitFunctionEntryCode(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
| 6804 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 6805 | /// ensure constants are generated when needed. Remember the virtual registers |
| 6806 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 6807 | /// directly add them, because expansion might result in multiple MBB's for one |
| 6808 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 6809 | /// the end. |
| 6810 | /// |
| 6811 | void |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6812 | SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6813 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6814 | |
| 6815 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 6816 | |
| 6817 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 6818 | // from this block. |
| 6819 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6820 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6821 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6822 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6823 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6824 | // If this terminator has multiple identical successors (common for |
| 6825 | // switches), only handle each succ once. |
| 6826 | if (!SuccsHandled.insert(SuccMBB)) continue; |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 6827 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6828 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6829 | |
| 6830 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 6831 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 6832 | // emitted yet. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6833 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 6834 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6835 | // Ignore dead phi's. |
| 6836 | if (PN->use_empty()) continue; |
| 6837 | |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 6838 | // Skip empty types |
| 6839 | if (PN->getType()->isEmptyTy()) |
| 6840 | continue; |
| 6841 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6842 | unsigned Reg; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6843 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6844 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6845 | if (const Constant *C = dyn_cast<Constant>(PHIOp)) { |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6846 | unsigned &RegOut = ConstantsOut[C]; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6847 | if (RegOut == 0) { |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 6848 | RegOut = FuncInfo.CreateRegs(C->getType()); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6849 | CopyValueToVirtualRegister(C, RegOut); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6850 | } |
| 6851 | Reg = RegOut; |
| 6852 | } else { |
Dan Gohman | c25ad63 | 2010-07-01 01:33:21 +0000 | [diff] [blame] | 6853 | DenseMap<const Value *, unsigned>::iterator I = |
| 6854 | FuncInfo.ValueMap.find(PHIOp); |
| 6855 | if (I != FuncInfo.ValueMap.end()) |
| 6856 | Reg = I->second; |
| 6857 | else { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6858 | assert(isa<AllocaInst>(PHIOp) && |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6859 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6860 | "Didn't codegen value into a register!??"); |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 6861 | Reg = FuncInfo.CreateRegs(PHIOp->getType()); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6862 | CopyValueToVirtualRegister(PHIOp, Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6863 | } |
| 6864 | } |
| 6865 | |
| 6866 | // Remember that this register needs to added to the machine PHI node as |
| 6867 | // the input for this MBB. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6868 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6869 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 6870 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6871 | EVT VT = ValueVTs[vti]; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6872 | unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6873 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6874 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 6875 | Reg += NumRegisters; |
| 6876 | } |
| 6877 | } |
| 6878 | } |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 6879 | ConstantsOut.clear(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 6880 | } |