blob: c6dad81d83835767be8b72ad9b1e2fcbc71eaade [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Devang Patel00190342010-03-15 19:15:44 +000015#include "SDNodeDbgValue.h"
Dan Gohman2048b852009-11-23 18:04:58 +000016#include "SelectionDAGBuilder.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000018#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000019#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000020#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000021#include "llvm/Constants.h"
22#include "llvm/CallingConv.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Function.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/InlineAsm.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/IntrinsicInst.h"
Chris Lattner6129c372010-04-08 00:09:16 +000030#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000031#include "llvm/Module.h"
Dan Gohman5eb6d652010-04-21 01:22:34 +000032#include "llvm/CodeGen/Analysis.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000033#include "llvm/CodeGen/FastISel.h"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000034#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000035#include "llvm/CodeGen/GCStrategy.h"
36#include "llvm/CodeGen/GCMetadata.h"
37#include "llvm/CodeGen/MachineFunction.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
40#include "llvm/CodeGen/MachineJumpTableInfo.h"
41#include "llvm/CodeGen/MachineModuleInfo.h"
42#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000043#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000045#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000046#include "llvm/Target/TargetRegisterInfo.h"
47#include "llvm/Target/TargetData.h"
48#include "llvm/Target/TargetFrameInfo.h"
49#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000050#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000052#include "llvm/Target/TargetOptions.h"
53#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000054#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000056#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000058#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059#include <algorithm>
60using namespace llvm;
61
Dale Johannesen601d3c02008-09-05 01:48:15 +000062/// LimitFloatPrecision - Generate low-precision inline sequences for
63/// some float libcalls (6, 8 or 12 bits).
64static unsigned LimitFloatPrecision;
65
66static cl::opt<unsigned, true>
67LimitFPPrecision("limit-float-precision",
68 cl::desc("Generate low-precision inline sequences "
69 "for some float libcalls"),
70 cl::location(LimitFloatPrecision),
71 cl::init(0));
72
Chris Lattner3ac18842010-08-24 23:20:40 +000073static SDValue getCopyFromPartsVector(SelectionDAG &DAG, DebugLoc DL,
74 const SDValue *Parts, unsigned NumParts,
75 EVT PartVT, EVT ValueVT);
76
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000077/// getCopyFromParts - Create a value that contains the specified legal parts
78/// combined into the value they represent. If the parts combine to a type
79/// larger then ValueVT then AssertOp can be used to specify whether the extra
80/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
81/// (ISD::AssertSext).
Chris Lattner3ac18842010-08-24 23:20:40 +000082static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc DL,
Dale Johannesen66978ee2009-01-31 02:22:37 +000083 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +000084 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +000085 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Chris Lattner3ac18842010-08-24 23:20:40 +000086 if (ValueVT.isVector())
87 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT);
88
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000089 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +000090 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000091 SDValue Val = Parts[0];
92
93 if (NumParts > 1) {
94 // Assemble the value from multiple parts.
Chris Lattner3ac18842010-08-24 23:20:40 +000095 if (ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000096 unsigned PartBits = PartVT.getSizeInBits();
97 unsigned ValueBits = ValueVT.getSizeInBits();
98
99 // Assemble the power of 2 part.
100 unsigned RoundParts = NumParts & (NumParts - 1) ?
101 1 << Log2_32(NumParts) : NumParts;
102 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000103 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000104 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000105 SDValue Lo, Hi;
106
Owen Anderson23b9b192009-08-12 00:36:31 +0000107 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000109 if (RoundParts > 2) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000110 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 PartVT, HalfVT);
Chris Lattner3ac18842010-08-24 23:20:40 +0000112 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000113 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000114 } else {
Chris Lattner3ac18842010-08-24 23:20:40 +0000115 Lo = DAG.getNode(ISD::BIT_CONVERT, DL, HalfVT, Parts[0]);
116 Hi = DAG.getNode(ISD::BIT_CONVERT, DL, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000117 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000118
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000119 if (TLI.isBigEndian())
120 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000121
Chris Lattner3ac18842010-08-24 23:20:40 +0000122 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000123
124 if (RoundParts < NumParts) {
125 // Assemble the trailing non-power-of-2 part.
126 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000127 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000128 Hi = getCopyFromParts(DAG, DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000129 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000130
131 // Combine the round and odd parts.
132 Lo = Val;
133 if (TLI.isBigEndian())
134 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000135 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000136 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
137 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000138 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000139 TLI.getPointerTy()));
Chris Lattner3ac18842010-08-24 23:20:40 +0000140 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
141 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000142 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000143 } else if (PartVT.isFloatingPoint()) {
144 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000146 "Unexpected split");
147 SDValue Lo, Hi;
Chris Lattner3ac18842010-08-24 23:20:40 +0000148 Lo = DAG.getNode(ISD::BIT_CONVERT, DL, EVT(MVT::f64), Parts[0]);
149 Hi = DAG.getNode(ISD::BIT_CONVERT, DL, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000150 if (TLI.isBigEndian())
151 std::swap(Lo, Hi);
Chris Lattner3ac18842010-08-24 23:20:40 +0000152 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000153 } else {
154 // FP split into integer parts (soft fp)
155 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
156 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000157 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Chris Lattner3ac18842010-08-24 23:20:40 +0000158 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000159 }
160 }
161
162 // There is now one part, held in Val. Correct it to match ValueVT.
163 PartVT = Val.getValueType();
164
165 if (PartVT == ValueVT)
166 return Val;
167
Chris Lattner3ac18842010-08-24 23:20:40 +0000168 if (PartVT.isInteger() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000169 if (ValueVT.bitsLT(PartVT)) {
170 // For a truncate, see if we have any information to
171 // indicate whether the truncated bits will always be
172 // zero or sign-extension.
173 if (AssertOp != ISD::DELETED_NODE)
Chris Lattner3ac18842010-08-24 23:20:40 +0000174 Val = DAG.getNode(AssertOp, DL, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000175 DAG.getValueType(ValueVT));
Chris Lattner3ac18842010-08-24 23:20:40 +0000176 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000177 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000178 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000179 }
180
181 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000182 // FP_ROUND's are always exact here.
183 if (ValueVT.bitsLT(Val.getValueType()))
184 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val,
Bill Wendling4533cac2010-01-28 21:51:40 +0000185 DAG.getIntPtrConstant(1));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000186
Chris Lattner3ac18842010-08-24 23:20:40 +0000187 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000188 }
189
Bill Wendling4533cac2010-01-28 21:51:40 +0000190 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Chris Lattner3ac18842010-08-24 23:20:40 +0000191 return DAG.getNode(ISD::BIT_CONVERT, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000192
Torok Edwinc23197a2009-07-14 16:55:14 +0000193 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000194 return SDValue();
195}
196
Chris Lattner3ac18842010-08-24 23:20:40 +0000197/// getCopyFromParts - Create a value that contains the specified legal parts
198/// combined into the value they represent. If the parts combine to a type
199/// larger then ValueVT then AssertOp can be used to specify whether the extra
200/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
201/// (ISD::AssertSext).
202static SDValue getCopyFromPartsVector(SelectionDAG &DAG, DebugLoc DL,
203 const SDValue *Parts, unsigned NumParts,
204 EVT PartVT, EVT ValueVT) {
205 assert(ValueVT.isVector() && "Not a vector value");
206 assert(NumParts > 0 && "No parts to assemble!");
207 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
208 SDValue Val = Parts[0];
209
210 // Handle a multi-element vector.
211 if (NumParts > 1) {
212 EVT IntermediateVT, RegisterVT;
213 unsigned NumIntermediates;
214 unsigned NumRegs =
215 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
216 NumIntermediates, RegisterVT);
217 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
218 NumParts = NumRegs; // Silence a compiler warning.
219 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
220 assert(RegisterVT == Parts[0].getValueType() &&
221 "Part type doesn't match part!");
222
223 // Assemble the parts into intermediate operands.
224 SmallVector<SDValue, 8> Ops(NumIntermediates);
225 if (NumIntermediates == NumParts) {
226 // If the register was not expanded, truncate or copy the value,
227 // as appropriate.
228 for (unsigned i = 0; i != NumParts; ++i)
229 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
230 PartVT, IntermediateVT);
231 } else if (NumParts > 0) {
232 // If the intermediate type was expanded, build the intermediate
233 // operands from the parts.
234 assert(NumParts % NumIntermediates == 0 &&
235 "Must expand into a divisible number of parts!");
236 unsigned Factor = NumParts / NumIntermediates;
237 for (unsigned i = 0; i != NumIntermediates; ++i)
238 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
239 PartVT, IntermediateVT);
240 }
241
242 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
243 // intermediate operands.
244 Val = DAG.getNode(IntermediateVT.isVector() ?
245 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL,
246 ValueVT, &Ops[0], NumIntermediates);
247 }
248
249 // There is now one part, held in Val. Correct it to match ValueVT.
250 PartVT = Val.getValueType();
251
252 if (PartVT == ValueVT)
253 return Val;
254
Chris Lattnere6f7c262010-08-25 22:49:25 +0000255 if (PartVT.isVector()) {
256 // If the element type of the source/dest vectors are the same, but the
257 // parts vector has more elements than the value vector, then we have a
258 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
259 // elements we want.
260 if (PartVT.getVectorElementType() == ValueVT.getVectorElementType()) {
261 assert(PartVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
262 "Cannot narrow, it would be a lossy transformation");
263 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
264 DAG.getIntPtrConstant(0));
265 }
266
267 // Vector/Vector bitcast.
Chris Lattner3ac18842010-08-24 23:20:40 +0000268 return DAG.getNode(ISD::BIT_CONVERT, DL, ValueVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000269 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000270
271 assert(ValueVT.getVectorElementType() == PartVT &&
272 ValueVT.getVectorNumElements() == 1 &&
273 "Only trivial scalar-to-vector conversions should get here!");
274 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
275}
276
277
278
Chris Lattnera13b8602010-08-24 23:10:06 +0000279
280static void getCopyToPartsVector(SelectionDAG &DAG, DebugLoc dl,
281 SDValue Val, SDValue *Parts, unsigned NumParts,
282 EVT PartVT);
283
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000284/// getCopyToParts - Create a series of nodes that contain the specified value
285/// split into legal parts. If the parts contain more bits than Val, then, for
286/// integers, ExtendKind can be used to specify how to generate the extra bits.
Chris Lattnera13b8602010-08-24 23:10:06 +0000287static void getCopyToParts(SelectionDAG &DAG, DebugLoc DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000288 SDValue Val, SDValue *Parts, unsigned NumParts,
289 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000290 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Owen Andersone50ed302009-08-10 22:56:29 +0000291 EVT ValueVT = Val.getValueType();
Chris Lattnera13b8602010-08-24 23:10:06 +0000292
293 // Handle the vector case separately.
294 if (ValueVT.isVector())
295 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT);
296
297 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000298 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000299 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000300 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
301
Chris Lattnera13b8602010-08-24 23:10:06 +0000302 if (NumParts == 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000303 return;
304
Chris Lattnera13b8602010-08-24 23:10:06 +0000305 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
306 if (PartVT == ValueVT) {
307 assert(NumParts == 1 && "No-op copy with multiple parts!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000308 Parts[0] = Val;
309 return;
310 }
311
Chris Lattnera13b8602010-08-24 23:10:06 +0000312 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
313 // If the parts cover more bits than the value has, promote the value.
314 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
315 assert(NumParts == 1 && "Do not know what to promote to!");
316 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
317 } else {
318 assert(PartVT.isInteger() && ValueVT.isInteger() &&
319 "Unknown mismatch!");
320 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
321 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
322 }
323 } else if (PartBits == ValueVT.getSizeInBits()) {
324 // Different types of the same size.
325 assert(NumParts == 1 && PartVT != ValueVT);
326 Val = DAG.getNode(ISD::BIT_CONVERT, DL, PartVT, Val);
327 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
328 // If the parts cover less bits than value has, truncate the value.
329 assert(PartVT.isInteger() && ValueVT.isInteger() &&
330 "Unknown mismatch!");
331 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
332 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
333 }
334
335 // The value may have changed - recompute ValueVT.
336 ValueVT = Val.getValueType();
337 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
338 "Failed to tile the value with PartVT!");
339
340 if (NumParts == 1) {
341 assert(PartVT == ValueVT && "Type conversion failed!");
342 Parts[0] = Val;
343 return;
344 }
345
346 // Expand the value into multiple parts.
347 if (NumParts & (NumParts - 1)) {
348 // The number of parts is not a power of 2. Split off and copy the tail.
349 assert(PartVT.isInteger() && ValueVT.isInteger() &&
350 "Do not know what to expand to!");
351 unsigned RoundParts = 1 << Log2_32(NumParts);
352 unsigned RoundBits = RoundParts * PartBits;
353 unsigned OddParts = NumParts - RoundParts;
354 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
355 DAG.getIntPtrConstant(RoundBits));
356 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT);
357
358 if (TLI.isBigEndian())
359 // The odd parts were reversed by getCopyToParts - unreverse them.
360 std::reverse(Parts + RoundParts, Parts + NumParts);
361
362 NumParts = RoundParts;
363 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
364 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
365 }
366
367 // The number of parts is a power of 2. Repeatedly bisect the value using
368 // EXTRACT_ELEMENT.
369 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, DL,
370 EVT::getIntegerVT(*DAG.getContext(),
371 ValueVT.getSizeInBits()),
372 Val);
373
374 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
375 for (unsigned i = 0; i < NumParts; i += StepSize) {
376 unsigned ThisBits = StepSize * PartBits / 2;
377 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
378 SDValue &Part0 = Parts[i];
379 SDValue &Part1 = Parts[i+StepSize/2];
380
381 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
382 ThisVT, Part0, DAG.getIntPtrConstant(1));
383 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
384 ThisVT, Part0, DAG.getIntPtrConstant(0));
385
386 if (ThisBits == PartBits && ThisVT != PartVT) {
387 Part0 = DAG.getNode(ISD::BIT_CONVERT, DL, PartVT, Part0);
388 Part1 = DAG.getNode(ISD::BIT_CONVERT, DL, PartVT, Part1);
389 }
390 }
391 }
392
393 if (TLI.isBigEndian())
394 std::reverse(Parts, Parts + OrigNumParts);
395}
396
397
398/// getCopyToPartsVector - Create a series of nodes that contain the specified
399/// value split into legal parts.
400static void getCopyToPartsVector(SelectionDAG &DAG, DebugLoc DL,
401 SDValue Val, SDValue *Parts, unsigned NumParts,
402 EVT PartVT) {
403 EVT ValueVT = Val.getValueType();
404 assert(ValueVT.isVector() && "Not a vector");
405 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
406
407 if (NumParts == 1) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000408 if (PartVT == ValueVT) {
409 // Nothing to do.
410 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
411 // Bitconvert vector->vector case.
412 Val = DAG.getNode(ISD::BIT_CONVERT, DL, PartVT, Val);
413 } else if (PartVT.isVector() &&
414 PartVT.getVectorElementType() == ValueVT.getVectorElementType()&&
415 PartVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
416 EVT ElementVT = PartVT.getVectorElementType();
417 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
418 // undef elements.
419 SmallVector<SDValue, 16> Ops;
420 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
421 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
422 ElementVT, Val, DAG.getIntPtrConstant(i)));
423
424 for (unsigned i = ValueVT.getVectorNumElements(),
425 e = PartVT.getVectorNumElements(); i != e; ++i)
426 Ops.push_back(DAG.getUNDEF(ElementVT));
427
428 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size());
429
430 // FIXME: Use CONCAT for 2x -> 4x.
431
432 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
433 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
434 } else {
435 // Vector -> scalar conversion.
436 assert(ValueVT.getVectorElementType() == PartVT &&
437 ValueVT.getVectorNumElements() == 1 &&
438 "Only trivial vector-to-scalar conversions should get here!");
439 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
440 PartVT, Val, DAG.getIntPtrConstant(0));
Chris Lattnera13b8602010-08-24 23:10:06 +0000441 }
442
443 Parts[0] = Val;
444 return;
445 }
446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000447 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000448 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000449 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000450 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
Devang Patel8f09bea2010-08-26 20:32:32 +0000451 IntermediateVT,
452 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000453 unsigned NumElements = ValueVT.getVectorNumElements();
Chris Lattnera13b8602010-08-24 23:10:06 +0000454
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000455 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
456 NumParts = NumRegs; // Silence a compiler warning.
457 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Chris Lattnera13b8602010-08-24 23:10:06 +0000458
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000459 // Split the vector into intermediate operands.
460 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000461 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000462 if (IntermediateVT.isVector())
Chris Lattnera13b8602010-08-24 23:10:06 +0000463 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000464 IntermediateVT, Val,
Chris Lattnera13b8602010-08-24 23:10:06 +0000465 DAG.getIntPtrConstant(i * (NumElements / NumIntermediates)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000466 else
Chris Lattnera13b8602010-08-24 23:10:06 +0000467 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Chris Lattnere6f7c262010-08-25 22:49:25 +0000468 IntermediateVT, Val, DAG.getIntPtrConstant(i));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000469 }
Chris Lattnera13b8602010-08-24 23:10:06 +0000470
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000471 // Split the intermediate operands into legal parts.
472 if (NumParts == NumIntermediates) {
473 // If the register was not expanded, promote or copy the value,
474 // as appropriate.
475 for (unsigned i = 0; i != NumParts; ++i)
Chris Lattnera13b8602010-08-24 23:10:06 +0000476 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000477 } else if (NumParts > 0) {
478 // If the intermediate type was expanded, split each the value into
479 // legal parts.
480 assert(NumParts % NumIntermediates == 0 &&
481 "Must expand into a divisible number of parts!");
482 unsigned Factor = NumParts / NumIntermediates;
483 for (unsigned i = 0; i != NumIntermediates; ++i)
Chris Lattnera13b8602010-08-24 23:10:06 +0000484 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000485 }
486}
487
Chris Lattnera13b8602010-08-24 23:10:06 +0000488
489
490
Dan Gohman462f6b52010-05-29 17:53:24 +0000491namespace {
492 /// RegsForValue - This struct represents the registers (physical or virtual)
493 /// that a particular set of values is assigned, and the type information
494 /// about the value. The most common situation is to represent one value at a
495 /// time, but struct or array values are handled element-wise as multiple
496 /// values. The splitting of aggregates is performed recursively, so that we
497 /// never have aggregate-typed registers. The values at this point do not
498 /// necessarily have legal types, so each value may require one or more
499 /// registers of some legal type.
500 ///
501 struct RegsForValue {
502 /// ValueVTs - The value types of the values, which may not be legal, and
503 /// may need be promoted or synthesized from one or more registers.
504 ///
505 SmallVector<EVT, 4> ValueVTs;
506
507 /// RegVTs - The value types of the registers. This is the same size as
508 /// ValueVTs and it records, for each value, what the type of the assigned
509 /// register or registers are. (Individual values are never synthesized
510 /// from more than one type of register.)
511 ///
512 /// With virtual registers, the contents of RegVTs is redundant with TLI's
513 /// getRegisterType member function, however when with physical registers
514 /// it is necessary to have a separate record of the types.
515 ///
516 SmallVector<EVT, 4> RegVTs;
517
518 /// Regs - This list holds the registers assigned to the values.
519 /// Each legal or promoted value requires one register, and each
520 /// expanded value requires multiple registers.
521 ///
522 SmallVector<unsigned, 4> Regs;
523
524 RegsForValue() {}
525
526 RegsForValue(const SmallVector<unsigned, 4> &regs,
527 EVT regvt, EVT valuevt)
528 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
529
Dan Gohman462f6b52010-05-29 17:53:24 +0000530 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
531 unsigned Reg, const Type *Ty) {
532 ComputeValueVTs(tli, Ty, ValueVTs);
533
534 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
535 EVT ValueVT = ValueVTs[Value];
536 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
537 EVT RegisterVT = tli.getRegisterType(Context, ValueVT);
538 for (unsigned i = 0; i != NumRegs; ++i)
539 Regs.push_back(Reg + i);
540 RegVTs.push_back(RegisterVT);
541 Reg += NumRegs;
542 }
543 }
544
545 /// areValueTypesLegal - Return true if types of all the values are legal.
546 bool areValueTypesLegal(const TargetLowering &TLI) {
547 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
548 EVT RegisterVT = RegVTs[Value];
549 if (!TLI.isTypeLegal(RegisterVT))
550 return false;
551 }
552 return true;
553 }
554
555 /// append - Add the specified values to this one.
556 void append(const RegsForValue &RHS) {
557 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
558 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
559 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
560 }
561
562 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
563 /// this value and returns the result as a ValueVTs value. This uses
564 /// Chain/Flag as the input and updates them for the output Chain/Flag.
565 /// If the Flag pointer is NULL, no flag is used.
566 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
567 DebugLoc dl,
568 SDValue &Chain, SDValue *Flag) const;
569
570 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
571 /// specified value into the registers specified by this object. This uses
572 /// Chain/Flag as the input and updates them for the output Chain/Flag.
573 /// If the Flag pointer is NULL, no flag is used.
574 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
575 SDValue &Chain, SDValue *Flag) const;
576
577 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
578 /// operand list. This adds the code marker, matching input operand index
579 /// (if applicable), and includes the number of values added into it.
580 void AddInlineAsmOperands(unsigned Kind,
581 bool HasMatching, unsigned MatchingIdx,
582 SelectionDAG &DAG,
583 std::vector<SDValue> &Ops) const;
584 };
585}
586
587/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
588/// this value and returns the result as a ValueVT value. This uses
589/// Chain/Flag as the input and updates them for the output Chain/Flag.
590/// If the Flag pointer is NULL, no flag is used.
591SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
592 FunctionLoweringInfo &FuncInfo,
593 DebugLoc dl,
594 SDValue &Chain, SDValue *Flag) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000595 // A Value with type {} or [0 x %t] needs no registers.
596 if (ValueVTs.empty())
597 return SDValue();
598
Dan Gohman462f6b52010-05-29 17:53:24 +0000599 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
600
601 // Assemble the legal parts into the final values.
602 SmallVector<SDValue, 4> Values(ValueVTs.size());
603 SmallVector<SDValue, 8> Parts;
604 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
605 // Copy the legal parts from the registers.
606 EVT ValueVT = ValueVTs[Value];
607 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
608 EVT RegisterVT = RegVTs[Value];
609
610 Parts.resize(NumRegs);
611 for (unsigned i = 0; i != NumRegs; ++i) {
612 SDValue P;
613 if (Flag == 0) {
614 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
615 } else {
616 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
617 *Flag = P.getValue(2);
618 }
619
620 Chain = P.getValue(1);
621
622 // If the source register was virtual and if we know something about it,
623 // add an assert node.
624 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
625 RegisterVT.isInteger() && !RegisterVT.isVector()) {
626 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
627 if (FuncInfo.LiveOutRegInfo.size() > SlotNo) {
628 const FunctionLoweringInfo::LiveOutInfo &LOI =
629 FuncInfo.LiveOutRegInfo[SlotNo];
630
631 unsigned RegSize = RegisterVT.getSizeInBits();
632 unsigned NumSignBits = LOI.NumSignBits;
633 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
634
635 // FIXME: We capture more information than the dag can represent. For
636 // now, just use the tightest assertzext/assertsext possible.
637 bool isSExt = true;
638 EVT FromVT(MVT::Other);
639 if (NumSignBits == RegSize)
640 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
641 else if (NumZeroBits >= RegSize-1)
642 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
643 else if (NumSignBits > RegSize-8)
644 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
645 else if (NumZeroBits >= RegSize-8)
646 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
647 else if (NumSignBits > RegSize-16)
648 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
649 else if (NumZeroBits >= RegSize-16)
650 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
651 else if (NumSignBits > RegSize-32)
652 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
653 else if (NumZeroBits >= RegSize-32)
654 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
655
656 if (FromVT != MVT::Other)
657 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
658 RegisterVT, P, DAG.getValueType(FromVT));
659 }
660 }
661
662 Parts[i] = P;
663 }
664
665 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
666 NumRegs, RegisterVT, ValueVT);
667 Part += NumRegs;
668 Parts.clear();
669 }
670
671 return DAG.getNode(ISD::MERGE_VALUES, dl,
672 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
673 &Values[0], ValueVTs.size());
674}
675
676/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
677/// specified value into the registers specified by this object. This uses
678/// Chain/Flag as the input and updates them for the output Chain/Flag.
679/// If the Flag pointer is NULL, no flag is used.
680void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
681 SDValue &Chain, SDValue *Flag) const {
682 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
683
684 // Get the list of the values's legal parts.
685 unsigned NumRegs = Regs.size();
686 SmallVector<SDValue, 8> Parts(NumRegs);
687 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
688 EVT ValueVT = ValueVTs[Value];
689 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
690 EVT RegisterVT = RegVTs[Value];
691
Chris Lattner3ac18842010-08-24 23:20:40 +0000692 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohman462f6b52010-05-29 17:53:24 +0000693 &Parts[Part], NumParts, RegisterVT);
694 Part += NumParts;
695 }
696
697 // Copy the parts into the registers.
698 SmallVector<SDValue, 8> Chains(NumRegs);
699 for (unsigned i = 0; i != NumRegs; ++i) {
700 SDValue Part;
701 if (Flag == 0) {
702 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
703 } else {
704 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
705 *Flag = Part.getValue(1);
706 }
707
708 Chains[i] = Part.getValue(0);
709 }
710
711 if (NumRegs == 1 || Flag)
712 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
713 // flagged to it. That is the CopyToReg nodes and the user are considered
714 // a single scheduling unit. If we create a TokenFactor and return it as
715 // chain, then the TokenFactor is both a predecessor (operand) of the
716 // user as well as a successor (the TF operands are flagged to the user).
717 // c1, f1 = CopyToReg
718 // c2, f2 = CopyToReg
719 // c3 = TokenFactor c1, c2
720 // ...
721 // = op c3, ..., f2
722 Chain = Chains[NumRegs-1];
723 else
724 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
725}
726
727/// AddInlineAsmOperands - Add this value to the specified inlineasm node
728/// operand list. This adds the code marker and includes the number of
729/// values added into it.
730void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
731 unsigned MatchingIdx,
732 SelectionDAG &DAG,
733 std::vector<SDValue> &Ops) const {
734 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
735
736 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
737 if (HasMatching)
738 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
739 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
740 Ops.push_back(Res);
741
742 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
743 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
744 EVT RegisterVT = RegVTs[Value];
745 for (unsigned i = 0; i != NumRegs; ++i) {
746 assert(Reg < Regs.size() && "Mismatch in # registers expected");
747 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
748 }
749 }
750}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000751
Dan Gohman2048b852009-11-23 18:04:58 +0000752void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000753 AA = &aa;
754 GFI = gfi;
755 TD = DAG.getTarget().getTargetData();
756}
757
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000758/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000759/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000760/// for a new block. This doesn't clear out information about
761/// additional blocks that are needed to complete switch lowering
762/// or PHI node updating; that information is cleared out as it is
763/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000764void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000765 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000766 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000767 PendingLoads.clear();
768 PendingExports.clear();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000769 DanglingDebugInfoMap.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000770 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000771 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000772}
773
774/// getRoot - Return the current virtual root of the Selection DAG,
775/// flushing any PendingLoad items. This must be done before emitting
776/// a store or any other node that may need to be ordered after any
777/// prior load instructions.
778///
Dan Gohman2048b852009-11-23 18:04:58 +0000779SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000780 if (PendingLoads.empty())
781 return DAG.getRoot();
782
783 if (PendingLoads.size() == 1) {
784 SDValue Root = PendingLoads[0];
785 DAG.setRoot(Root);
786 PendingLoads.clear();
787 return Root;
788 }
789
790 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000792 &PendingLoads[0], PendingLoads.size());
793 PendingLoads.clear();
794 DAG.setRoot(Root);
795 return Root;
796}
797
798/// getControlRoot - Similar to getRoot, but instead of flushing all the
799/// PendingLoad items, flush all the PendingExports items. It is necessary
800/// to do this before emitting a terminator instruction.
801///
Dan Gohman2048b852009-11-23 18:04:58 +0000802SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000803 SDValue Root = DAG.getRoot();
804
805 if (PendingExports.empty())
806 return Root;
807
808 // Turn all of the CopyToReg chains into one factored node.
809 if (Root.getOpcode() != ISD::EntryToken) {
810 unsigned i = 0, e = PendingExports.size();
811 for (; i != e; ++i) {
812 assert(PendingExports[i].getNode()->getNumOperands() > 1);
813 if (PendingExports[i].getNode()->getOperand(0) == Root)
814 break; // Don't add the root if we already indirectly depend on it.
815 }
816
817 if (i == e)
818 PendingExports.push_back(Root);
819 }
820
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000822 &PendingExports[0],
823 PendingExports.size());
824 PendingExports.clear();
825 DAG.setRoot(Root);
826 return Root;
827}
828
Bill Wendling4533cac2010-01-28 21:51:40 +0000829void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
830 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
831 DAG.AssignOrdering(Node, SDNodeOrder);
832
833 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
834 AssignOrderingToNode(Node->getOperand(I).getNode());
835}
836
Dan Gohman46510a72010-04-15 01:51:59 +0000837void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000838 // Set up outgoing PHI node register values before emitting the terminator.
839 if (isa<TerminatorInst>(&I))
840 HandlePHINodesInSuccessorBlocks(I.getParent());
841
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000842 CurDebugLoc = I.getDebugLoc();
843
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000844 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000845
Dan Gohman92884f72010-04-20 15:03:56 +0000846 if (!isa<TerminatorInst>(&I) && !HasTailCall)
847 CopyToExportRegsIfNeeded(&I);
848
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000849 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000850}
851
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000852void SelectionDAGBuilder::visitPHI(const PHINode &) {
853 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
854}
855
Dan Gohman46510a72010-04-15 01:51:59 +0000856void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000857 // Note: this doesn't use InstVisitor, because it has to work with
858 // ConstantExpr's in addition to instructions.
859 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000860 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000861 // Build the switch statement using the Instruction.def file.
862#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000863 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000864#include "llvm/Instruction.def"
865 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000866
867 // Assign the ordering to the freshly created DAG nodes.
868 if (NodeMap.count(&I)) {
869 ++SDNodeOrder;
870 AssignOrderingToNode(getValue(&I).getNode());
871 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000872}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000873
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000874// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
875// generate the debug data structures now that we've seen its definition.
876void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
877 SDValue Val) {
878 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
Devang Patelf2ec7ae2010-08-26 20:06:46 +0000879 MDNode *Variable = NULL;
880 uint64_t Offset = 0;
881
882 if (const DbgValueInst *DI = dyn_cast_or_null<DbgValueInst>(DDI.getDI())) {
883 Variable = DI->getVariable();
884 Offset = DI->getOffset();
885 } else if (const DbgDeclareInst *DI =
886 dyn_cast_or_null<DbgDeclareInst>(DDI.getDI()))
887 Variable = DI->getVariable();
888 else {
889 assert (DDI.getDI() == NULL && "Invalid debug info intrinsic!");
890 return;
891 }
892
893 if (Variable) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000894 DebugLoc dl = DDI.getdl();
895 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000896 SDDbgValue *SDV;
897 if (Val.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +0000898 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000899 SDV = DAG.getDbgValue(Variable, Val.getNode(),
900 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
901 DAG.AddDbgValue(SDV, Val.getNode(), false);
902 }
903 } else {
904 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
905 Offset, dl, SDNodeOrder);
906 DAG.AddDbgValue(SDV, 0, false);
907 }
908 DanglingDebugInfoMap[V] = DanglingDebugInfo();
909 }
910}
911
Dan Gohman28a17352010-07-01 01:59:43 +0000912// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +0000913SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +0000914 // If we already have an SDValue for this value, use it. It's important
915 // to do this first, so that we don't create a CopyFromReg if we already
916 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000917 SDValue &N = NodeMap[V];
918 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000919
Dan Gohman28a17352010-07-01 01:59:43 +0000920 // If there's a virtual register allocated and initialized for this
921 // value, use it.
922 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
923 if (It != FuncInfo.ValueMap.end()) {
924 unsigned InReg = It->second;
925 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
926 SDValue Chain = DAG.getEntryNode();
Devang Patel9d0796a2010-08-26 18:36:14 +0000927 N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain,NULL);
928 resolveDanglingDebugInfo(V, N);
929 return N;
Dan Gohman28a17352010-07-01 01:59:43 +0000930 }
931
932 // Otherwise create a new SDValue and remember it.
933 SDValue Val = getValueImpl(V);
934 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000935 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +0000936 return Val;
937}
938
939/// getNonRegisterValue - Return an SDValue for the given Value, but
940/// don't look in FuncInfo.ValueMap for a virtual register.
941SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
942 // If we already have an SDValue for this value, use it.
943 SDValue &N = NodeMap[V];
944 if (N.getNode()) return N;
945
946 // Otherwise create a new SDValue and remember it.
947 SDValue Val = getValueImpl(V);
948 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000949 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +0000950 return Val;
951}
952
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000953/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +0000954/// Create an SDValue for the given value.
955SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Dan Gohman383b5f62010-04-17 15:32:28 +0000956 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000957 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000958
Dan Gohman383b5f62010-04-17 15:32:28 +0000959 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000960 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000961
Dan Gohman383b5f62010-04-17 15:32:28 +0000962 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Devang Patel0d881da2010-07-06 22:08:15 +0000963 return DAG.getGlobalAddress(GV, getCurDebugLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000965 if (isa<ConstantPointerNull>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000966 return DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000967
Dan Gohman383b5f62010-04-17 15:32:28 +0000968 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000969 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000970
Nate Begeman9008ca62009-04-27 18:41:29 +0000971 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +0000972 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000973
Dan Gohman383b5f62010-04-17 15:32:28 +0000974 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000975 visit(CE->getOpcode(), *CE);
976 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000977 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000978 return N1;
979 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000980
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000981 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
982 SmallVector<SDValue, 4> Constants;
983 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
984 OI != OE; ++OI) {
985 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000986 // If the operand is an empty aggregate, there are no values.
987 if (!Val) continue;
988 // Add each leaf value from the operand to the Constants list
989 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000990 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
991 Constants.push_back(SDValue(Val, i));
992 }
Bill Wendling87710f02009-12-21 23:47:40 +0000993
Bill Wendling4533cac2010-01-28 21:51:40 +0000994 return DAG.getMergeValues(&Constants[0], Constants.size(),
995 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000996 }
997
Duncan Sands1df98592010-02-16 11:11:14 +0000998 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000999 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1000 "Unknown struct or array constant!");
1001
Owen Andersone50ed302009-08-10 22:56:29 +00001002 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001003 ComputeValueVTs(TLI, C->getType(), ValueVTs);
1004 unsigned NumElts = ValueVTs.size();
1005 if (NumElts == 0)
1006 return SDValue(); // empty struct
1007 SmallVector<SDValue, 4> Constants(NumElts);
1008 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001009 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001010 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001011 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001012 else if (EltVT.isFloatingPoint())
1013 Constants[i] = DAG.getConstantFP(0, EltVT);
1014 else
1015 Constants[i] = DAG.getConstant(0, EltVT);
1016 }
Bill Wendling87710f02009-12-21 23:47:40 +00001017
Bill Wendling4533cac2010-01-28 21:51:40 +00001018 return DAG.getMergeValues(&Constants[0], NumElts,
1019 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001020 }
1021
Dan Gohman383b5f62010-04-17 15:32:28 +00001022 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001023 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001024
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001025 const VectorType *VecTy = cast<VectorType>(V->getType());
1026 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001027
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001028 // Now that we know the number and type of the elements, get that number of
1029 // elements into the Ops array based on what kind of constant it is.
1030 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +00001031 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001032 for (unsigned i = 0; i != NumElements; ++i)
1033 Ops.push_back(getValue(CP->getOperand(i)));
1034 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001035 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +00001036 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001037
1038 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001039 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001040 Op = DAG.getConstantFP(0, EltVT);
1041 else
1042 Op = DAG.getConstant(0, EltVT);
1043 Ops.assign(NumElements, Op);
1044 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001046 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +00001047 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
1048 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001049 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001051 // If this is a static alloca, generate it as the frameindex instead of
1052 // computation.
1053 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1054 DenseMap<const AllocaInst*, int>::iterator SI =
1055 FuncInfo.StaticAllocaMap.find(AI);
1056 if (SI != FuncInfo.StaticAllocaMap.end())
1057 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
1058 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001059
Dan Gohman28a17352010-07-01 01:59:43 +00001060 // If this is an instruction which fast-isel has deferred, select it now.
1061 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001062 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
1063 RegsForValue RFV(*DAG.getContext(), TLI, InReg, Inst->getType());
1064 SDValue Chain = DAG.getEntryNode();
1065 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL);
Dan Gohman28a17352010-07-01 01:59:43 +00001066 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001067
Dan Gohman28a17352010-07-01 01:59:43 +00001068 llvm_unreachable("Can't get register for value!");
1069 return SDValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001070}
1071
Dan Gohman46510a72010-04-15 01:51:59 +00001072void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001073 SDValue Chain = getControlRoot();
1074 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001075 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001076
Dan Gohman7451d3e2010-05-29 17:03:36 +00001077 if (!FuncInfo.CanLowerReturn) {
1078 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001079 const Function *F = I.getParent()->getParent();
1080
1081 // Emit a store of the return value through the virtual register.
1082 // Leave Outs empty so that LowerReturn won't try to load return
1083 // registers the usual way.
1084 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001085 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001086 PtrValueVTs);
1087
1088 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1089 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001090
Owen Andersone50ed302009-08-10 22:56:29 +00001091 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001092 SmallVector<uint64_t, 4> Offsets;
1093 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001094 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001095
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001096 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001097 for (unsigned i = 0; i != NumValues; ++i) {
Chris Lattnera13b8602010-08-24 23:10:06 +00001098 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(),
1099 RetPtr.getValueType(), RetPtr,
1100 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001101 Chains[i] =
1102 DAG.getStore(Chain, getCurDebugLoc(),
1103 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00001104 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001105 }
1106
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001107 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
1108 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001109 } else if (I.getNumOperands() != 0) {
1110 SmallVector<EVT, 4> ValueVTs;
1111 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
1112 unsigned NumValues = ValueVTs.size();
1113 if (NumValues) {
1114 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001115 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1116 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001117
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001118 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001119
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001120 const Function *F = I.getParent()->getParent();
1121 if (F->paramHasAttr(0, Attribute::SExt))
1122 ExtendKind = ISD::SIGN_EXTEND;
1123 else if (F->paramHasAttr(0, Attribute::ZExt))
1124 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001125
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001126 // FIXME: C calling convention requires the return type to be promoted
1127 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001128 // conventions. The frontend should mark functions whose return values
1129 // require promoting with signext or zeroext attributes.
1130 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
1131 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
1132 if (VT.bitsLT(MinVT))
1133 VT = MinVT;
1134 }
1135
1136 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
1137 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
1138 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +00001139 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001140 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
1141 &Parts[0], NumParts, PartVT, ExtendKind);
1142
1143 // 'inreg' on function refers to return value
1144 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1145 if (F->paramHasAttr(0, Attribute::InReg))
1146 Flags.setInReg();
1147
1148 // Propagate extension type if any
1149 if (F->paramHasAttr(0, Attribute::SExt))
1150 Flags.setSExt();
1151 else if (F->paramHasAttr(0, Attribute::ZExt))
1152 Flags.setZExt();
1153
Dan Gohmanc9403652010-07-07 15:54:55 +00001154 for (unsigned i = 0; i < NumParts; ++i) {
1155 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
1156 /*isfixed=*/true));
1157 OutVals.push_back(Parts[i]);
1158 }
Evan Cheng3927f432009-03-25 20:20:11 +00001159 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001160 }
1161 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001162
1163 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001164 CallingConv::ID CallConv =
1165 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001166 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
Dan Gohmanc9403652010-07-07 15:54:55 +00001167 Outs, OutVals, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001168
1169 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001170 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001171 "LowerReturn didn't return a valid chain!");
1172
1173 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001174 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001175}
1176
Dan Gohmanad62f532009-04-23 23:13:24 +00001177/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1178/// created for it, emit nodes to copy the value into the virtual
1179/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001180void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +00001181 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1182 if (VMI != FuncInfo.ValueMap.end()) {
1183 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1184 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001185 }
1186}
1187
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001188/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1189/// the current basic block, add it to ValueMap now so that we'll get a
1190/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001191void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001192 // No need to export constants.
1193 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001195 // Already exported?
1196 if (FuncInfo.isExportedInst(V)) return;
1197
1198 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1199 CopyValueToVirtualRegister(V, Reg);
1200}
1201
Dan Gohman46510a72010-04-15 01:51:59 +00001202bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001203 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001204 // The operands of the setcc have to be in this block. We don't know
1205 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001206 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 // Can export from current BB.
1208 if (VI->getParent() == FromBB)
1209 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001211 // Is already exported, noop.
1212 return FuncInfo.isExportedInst(V);
1213 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001214
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001215 // If this is an argument, we can export it if the BB is the entry block or
1216 // if it is already exported.
1217 if (isa<Argument>(V)) {
1218 if (FromBB == &FromBB->getParent()->getEntryBlock())
1219 return true;
1220
1221 // Otherwise, can only export this if it is already exported.
1222 return FuncInfo.isExportedInst(V);
1223 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001224
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001225 // Otherwise, constants can always be exported.
1226 return true;
1227}
1228
1229static bool InBlock(const Value *V, const BasicBlock *BB) {
1230 if (const Instruction *I = dyn_cast<Instruction>(V))
1231 return I->getParent() == BB;
1232 return true;
1233}
1234
Dan Gohmanc2277342008-10-17 21:16:08 +00001235/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1236/// This function emits a branch and is used at the leaves of an OR or an
1237/// AND operator tree.
1238///
1239void
Dan Gohman46510a72010-04-15 01:51:59 +00001240SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001241 MachineBasicBlock *TBB,
1242 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001243 MachineBasicBlock *CurBB,
1244 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001245 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001246
Dan Gohmanc2277342008-10-17 21:16:08 +00001247 // If the leaf of the tree is a comparison, merge the condition into
1248 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001249 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001250 // The operands of the cmp have to be in this block. We don't know
1251 // how to export them from some other block. If this is the first block
1252 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001253 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001254 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1255 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001256 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001257 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001258 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001259 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001260 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001261 } else {
1262 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001263 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001264 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001265
1266 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001267 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1268 SwitchCases.push_back(CB);
1269 return;
1270 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001271 }
1272
1273 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001274 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001275 NULL, TBB, FBB, CurBB);
1276 SwitchCases.push_back(CB);
1277}
1278
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001279/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001280void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001281 MachineBasicBlock *TBB,
1282 MachineBasicBlock *FBB,
1283 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001284 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001285 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001286 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001287 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001288 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001289 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1290 BOp->getParent() != CurBB->getBasicBlock() ||
1291 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1292 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001293 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001294 return;
1295 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001297 // Create TmpBB after CurBB.
1298 MachineFunction::iterator BBI = CurBB;
1299 MachineFunction &MF = DAG.getMachineFunction();
1300 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1301 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001302
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303 if (Opc == Instruction::Or) {
1304 // Codegen X | Y as:
1305 // jmp_if_X TBB
1306 // jmp TmpBB
1307 // TmpBB:
1308 // jmp_if_Y TBB
1309 // jmp FBB
1310 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001311
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001312 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001313 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001316 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001317 } else {
1318 assert(Opc == Instruction::And && "Unknown merge op!");
1319 // Codegen X & Y as:
1320 // jmp_if_X TmpBB
1321 // jmp FBB
1322 // TmpBB:
1323 // jmp_if_Y TBB
1324 // jmp FBB
1325 //
1326 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001327
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001328 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001329 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001330
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001332 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001333 }
1334}
1335
1336/// If the set of cases should be emitted as a series of branches, return true.
1337/// If we should emit this as a bunch of and/or'd together conditions, return
1338/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001339bool
Dan Gohman2048b852009-11-23 18:04:58 +00001340SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001341 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001342
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001343 // If this is two comparisons of the same values or'd or and'd together, they
1344 // will get folded into a single comparison, so don't emit two blocks.
1345 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1346 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1347 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1348 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1349 return false;
1350 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001351
Chris Lattner133ce872010-01-02 00:00:03 +00001352 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1353 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1354 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1355 Cases[0].CC == Cases[1].CC &&
1356 isa<Constant>(Cases[0].CmpRHS) &&
1357 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1358 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1359 return false;
1360 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1361 return false;
1362 }
1363
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001364 return true;
1365}
1366
Dan Gohman46510a72010-04-15 01:51:59 +00001367void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001368 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001369
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001370 // Update machine-CFG edges.
1371 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1372
1373 // Figure out which block is immediately after the current one.
1374 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001375 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001376 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377 NextBlock = BBI;
1378
1379 if (I.isUnconditional()) {
1380 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001381 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001382
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001383 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001384 if (Succ0MBB != NextBlock)
1385 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001386 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001387 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001388
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001389 return;
1390 }
1391
1392 // If this condition is one of the special cases we handle, do special stuff
1393 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001394 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001395 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1396
1397 // If this is a series of conditions that are or'd or and'd together, emit
1398 // this as a sequence of branches instead of setcc's with and/or operations.
1399 // For example, instead of something like:
1400 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001401 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001402 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001403 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001404 // or C, F
1405 // jnz foo
1406 // Emit:
1407 // cmp A, B
1408 // je foo
1409 // cmp D, E
1410 // jle foo
1411 //
Dan Gohman46510a72010-04-15 01:51:59 +00001412 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001413 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001414 (BOp->getOpcode() == Instruction::And ||
1415 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001416 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1417 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001418 // If the compares in later blocks need to use values not currently
1419 // exported from this block, export them now. This block should always
1420 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001421 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001422
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001423 // Allow some cases to be rejected.
1424 if (ShouldEmitAsBranches(SwitchCases)) {
1425 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1426 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1427 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1428 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001429
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001431 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001432 SwitchCases.erase(SwitchCases.begin());
1433 return;
1434 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001435
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436 // Okay, we decided not to do this, remove any inserted MBB's and clear
1437 // SwitchCases.
1438 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001439 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001441 SwitchCases.clear();
1442 }
1443 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001444
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001446 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001447 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001448
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449 // Use visitSwitchCase to actually insert the fast branch sequence for this
1450 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001451 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001452}
1453
1454/// visitSwitchCase - Emits the necessary code to represent a single node in
1455/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001456void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1457 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458 SDValue Cond;
1459 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001460 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001461
1462 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001463 if (CB.CmpMHS == NULL) {
1464 // Fold "(X == true)" to X and "(X == false)" to !X to
1465 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001466 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001467 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001468 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001469 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001470 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001471 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001472 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001474 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001475 } else {
1476 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1477
Anton Korobeynikov23218582008-12-23 22:25:27 +00001478 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1479 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001480
1481 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001482 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001483
1484 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001485 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001486 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001487 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001488 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001489 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001490 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001491 DAG.getConstant(High-Low, VT), ISD::SETULE);
1492 }
1493 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001494
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001495 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001496 SwitchBB->addSuccessor(CB.TrueBB);
1497 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001498
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001499 // Set NextBlock to be the MBB immediately after the current one, if any.
1500 // This is used to avoid emitting unnecessary branches to the next block.
1501 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001502 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001503 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001504 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001506 // If the lhs block is the next block, invert the condition so that we can
1507 // fall through to the lhs instead of the rhs block.
1508 if (CB.TrueBB == NextBlock) {
1509 std::swap(CB.TrueBB, CB.FalseBB);
1510 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001511 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001512 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001513
Dale Johannesenf5d97892009-02-04 01:48:28 +00001514 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001515 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001516 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001517
Dan Gohmandeca0522010-06-24 17:08:31 +00001518 // Insert the false branch.
1519 if (CB.FalseBB != NextBlock)
1520 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1521 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001522
1523 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001524}
1525
1526/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001527void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001528 // Emit the code for the jump table
1529 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001530 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001531 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1532 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001533 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001534 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1535 MVT::Other, Index.getValue(1),
1536 Table, Index);
1537 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001538}
1539
1540/// visitJumpTableHeader - This function emits necessary code to produce index
1541/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001542void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001543 JumpTableHeader &JTH,
1544 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001545 // Subtract the lowest switch case value from the value being switched on and
1546 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001547 // difference between smallest and largest cases.
1548 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001549 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001550 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001551 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001552
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001553 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001554 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001555 // can be used as an index into the jump table in a subsequent basic block.
1556 // This value may be smaller or larger than the target's pointer type, and
1557 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001558 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001559
Dan Gohman89496d02010-07-02 00:10:16 +00001560 unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001561 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1562 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001563 JT.Reg = JumpTableReg;
1564
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001565 // Emit the range check for the jump table, and branch to the default block
1566 // for the switch statement if the value being switched on exceeds the largest
1567 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001568 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001569 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001570 DAG.getConstant(JTH.Last-JTH.First,VT),
1571 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001572
1573 // Set NextBlock to be the MBB immediately after the current one, if any.
1574 // This is used to avoid emitting unnecessary branches to the next block.
1575 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001576 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001577
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001578 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001579 NextBlock = BBI;
1580
Dale Johannesen66978ee2009-01-31 02:22:37 +00001581 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001583 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001584
Bill Wendling4533cac2010-01-28 21:51:40 +00001585 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001586 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1587 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001588
Bill Wendling87710f02009-12-21 23:47:40 +00001589 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001590}
1591
1592/// visitBitTestHeader - This function emits necessary code to produce value
1593/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001594void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1595 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001596 // Subtract the minimum value
1597 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001598 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001599 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001600 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001601
1602 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001603 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001604 TLI.getSetCCResultType(Sub.getValueType()),
1605 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001606 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001607
Bill Wendling87710f02009-12-21 23:47:40 +00001608 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1609 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610
Dan Gohman89496d02010-07-02 00:10:16 +00001611 B.Reg = FuncInfo.CreateReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001612 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1613 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001614
1615 // Set NextBlock to be the MBB immediately after the current one, if any.
1616 // This is used to avoid emitting unnecessary branches to the next block.
1617 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001618 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001619 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 NextBlock = BBI;
1621
1622 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1623
Dan Gohman99be8ae2010-04-19 22:41:47 +00001624 SwitchBB->addSuccessor(B.Default);
1625 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001626
Dale Johannesen66978ee2009-01-31 02:22:37 +00001627 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001628 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001629 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001630
Bill Wendling4533cac2010-01-28 21:51:40 +00001631 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001632 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1633 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001634
Bill Wendling87710f02009-12-21 23:47:40 +00001635 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001636}
1637
1638/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001639void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1640 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001641 BitTestCase &B,
1642 MachineBasicBlock *SwitchBB) {
Dale Johannesena04b7572009-02-03 23:04:43 +00001643 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001644 TLI.getPointerTy());
Dan Gohman8e0163a2010-06-24 02:06:24 +00001645 SDValue Cmp;
1646 if (CountPopulation_64(B.Mask) == 1) {
1647 // Testing for a single bit; just compare the shift count with what it
1648 // would need to be to shift a 1 bit in that position.
1649 Cmp = DAG.getSetCC(getCurDebugLoc(),
1650 TLI.getSetCCResultType(ShiftOp.getValueType()),
1651 ShiftOp,
1652 DAG.getConstant(CountTrailingZeros_64(B.Mask),
1653 TLI.getPointerTy()),
1654 ISD::SETEQ);
1655 } else {
1656 // Make desired shift
1657 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
1658 TLI.getPointerTy(),
1659 DAG.getConstant(1, TLI.getPointerTy()),
1660 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001661
Dan Gohman8e0163a2010-06-24 02:06:24 +00001662 // Emit bit tests and jumps
1663 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
1664 TLI.getPointerTy(), SwitchVal,
1665 DAG.getConstant(B.Mask, TLI.getPointerTy()));
1666 Cmp = DAG.getSetCC(getCurDebugLoc(),
1667 TLI.getSetCCResultType(AndOp.getValueType()),
1668 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
1669 ISD::SETNE);
1670 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001671
Dan Gohman99be8ae2010-04-19 22:41:47 +00001672 SwitchBB->addSuccessor(B.TargetBB);
1673 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001674
Dale Johannesen66978ee2009-01-31 02:22:37 +00001675 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001676 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001677 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001678
1679 // Set NextBlock to be the MBB immediately after the current one, if any.
1680 // This is used to avoid emitting unnecessary branches to the next block.
1681 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001682 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001683 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001684 NextBlock = BBI;
1685
Bill Wendling4533cac2010-01-28 21:51:40 +00001686 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001687 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1688 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001689
Bill Wendling87710f02009-12-21 23:47:40 +00001690 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001691}
1692
Dan Gohman46510a72010-04-15 01:51:59 +00001693void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001694 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001695
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696 // Retrieve successors.
1697 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1698 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1699
Gabor Greifb67e6b32009-01-15 11:10:44 +00001700 const Value *Callee(I.getCalledValue());
1701 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001702 visitInlineAsm(&I);
1703 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001704 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001705
1706 // If the value of the invoke is used outside of its defining block, make it
1707 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001708 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001709
1710 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001711 InvokeMBB->addSuccessor(Return);
1712 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001713
1714 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001715 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1716 MVT::Other, getControlRoot(),
1717 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001718}
1719
Dan Gohman46510a72010-04-15 01:51:59 +00001720void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001721}
1722
1723/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1724/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001725bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1726 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001727 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001728 MachineBasicBlock *Default,
1729 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001730 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001731
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001732 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001733 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001734 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001735 return false;
1736
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737 // Get the MachineFunction which holds the current MBB. This is used when
1738 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001739 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001740
1741 // Figure out which block is immediately after the current one.
1742 MachineBasicBlock *NextBlock = 0;
1743 MachineFunction::iterator BBI = CR.CaseBB;
1744
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001745 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001746 NextBlock = BBI;
1747
1748 // TODO: If any two of the cases has the same destination, and if one value
1749 // is the same as the other, but has one bit unset that the other has set,
1750 // use bit manipulation to do two compares at once. For example:
1751 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001752
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001753 // Rearrange the case blocks so that the last one falls through if possible.
1754 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1755 // The last case block won't fall through into 'NextBlock' if we emit the
1756 // branches in this order. See if rearranging a case value would help.
1757 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1758 if (I->BB == NextBlock) {
1759 std::swap(*I, BackCase);
1760 break;
1761 }
1762 }
1763 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001764
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001765 // Create a CaseBlock record representing a conditional branch to
1766 // the Case's target mbb if the value being switched on SV is equal
1767 // to C.
1768 MachineBasicBlock *CurBlock = CR.CaseBB;
1769 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1770 MachineBasicBlock *FallThrough;
1771 if (I != E-1) {
1772 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1773 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001774
1775 // Put SV in a virtual register to make it available from the new blocks.
1776 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001777 } else {
1778 // If the last case doesn't match, go to the default block.
1779 FallThrough = Default;
1780 }
1781
Dan Gohman46510a72010-04-15 01:51:59 +00001782 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001783 ISD::CondCode CC;
1784 if (I->High == I->Low) {
1785 // This is just small small case range :) containing exactly 1 case
1786 CC = ISD::SETEQ;
1787 LHS = SV; RHS = I->High; MHS = NULL;
1788 } else {
1789 CC = ISD::SETLE;
1790 LHS = I->Low; MHS = SV; RHS = I->High;
1791 }
1792 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001793
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001794 // If emitting the first comparison, just call visitSwitchCase to emit the
1795 // code into the current block. Otherwise, push the CaseBlock onto the
1796 // vector to be later processed by SDISel, and insert the node's MBB
1797 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001798 if (CurBlock == SwitchBB)
1799 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001800 else
1801 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001802
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001803 CurBlock = FallThrough;
1804 }
1805
1806 return true;
1807}
1808
1809static inline bool areJTsAllowed(const TargetLowering &TLI) {
1810 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1812 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001813}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001814
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001815static APInt ComputeRange(const APInt &First, const APInt &Last) {
1816 APInt LastExt(Last), FirstExt(First);
1817 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1818 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1819 return (LastExt - FirstExt + 1ULL);
1820}
1821
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001822/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001823bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1824 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001825 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001826 MachineBasicBlock* Default,
1827 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001828 Case& FrontCase = *CR.Range.first;
1829 Case& BackCase = *(CR.Range.second-1);
1830
Chris Lattnere880efe2009-11-07 07:50:34 +00001831 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1832 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001833
Chris Lattnere880efe2009-11-07 07:50:34 +00001834 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001835 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1836 I!=E; ++I)
1837 TSize += I->size();
1838
Dan Gohmane0567812010-04-08 23:03:40 +00001839 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001840 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001841
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001842 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001843 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001844 if (Density < 0.4)
1845 return false;
1846
David Greene4b69d992010-01-05 01:24:57 +00001847 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001848 << "First entry: " << First << ". Last entry: " << Last << '\n'
1849 << "Range: " << Range
1850 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001851
1852 // Get the MachineFunction which holds the current MBB. This is used when
1853 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001854 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855
1856 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001857 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001858 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001859
1860 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1861
1862 // Create a new basic block to hold the code for loading the address
1863 // of the jump table, and jumping to it. Update successor information;
1864 // we will either branch to the default case for the switch, or the jump
1865 // table.
1866 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1867 CurMF->insert(BBI, JumpTableBB);
1868 CR.CaseBB->addSuccessor(Default);
1869 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001870
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001871 // Build a vector of destination BBs, corresponding to each target
1872 // of the jump table. If the value of the jump table slot corresponds to
1873 // a case statement, push the case's BB onto the vector, otherwise, push
1874 // the default BB.
1875 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001876 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001877 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001878 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1879 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001880
1881 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 DestBBs.push_back(I->BB);
1883 if (TEI==High)
1884 ++I;
1885 } else {
1886 DestBBs.push_back(Default);
1887 }
1888 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001889
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001890 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001891 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1892 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001893 E = DestBBs.end(); I != E; ++I) {
1894 if (!SuccsHandled[(*I)->getNumber()]) {
1895 SuccsHandled[(*I)->getNumber()] = true;
1896 JumpTableBB->addSuccessor(*I);
1897 }
1898 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001899
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001900 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001901 unsigned JTEncoding = TLI.getJumpTableEncoding();
1902 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001903 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001905 // Set the jump table information so that we can codegen it as a second
1906 // MachineBasicBlock
1907 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001908 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1909 if (CR.CaseBB == SwitchBB)
1910 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001911
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001912 JTCases.push_back(JumpTableBlock(JTH, JT));
1913
1914 return true;
1915}
1916
1917/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1918/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001919bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1920 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001921 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001922 MachineBasicBlock *Default,
1923 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001924 // Get the MachineFunction which holds the current MBB. This is used when
1925 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001926 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001927
1928 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001930 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001931
1932 Case& FrontCase = *CR.Range.first;
1933 Case& BackCase = *(CR.Range.second-1);
1934 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1935
1936 // Size is the number of Cases represented by this range.
1937 unsigned Size = CR.Range.second - CR.Range.first;
1938
Chris Lattnere880efe2009-11-07 07:50:34 +00001939 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1940 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941 double FMetric = 0;
1942 CaseItr Pivot = CR.Range.first + Size/2;
1943
1944 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1945 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001946 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001947 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1948 I!=E; ++I)
1949 TSize += I->size();
1950
Chris Lattnere880efe2009-11-07 07:50:34 +00001951 APInt LSize = FrontCase.size();
1952 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001953 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001954 << "First: " << First << ", Last: " << Last <<'\n'
1955 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001956 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1957 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001958 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1959 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001960 APInt Range = ComputeRange(LEnd, RBegin);
1961 assert((Range - 2ULL).isNonNegative() &&
1962 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001963 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001964 (LEnd - First + 1ULL).roundToDouble();
1965 double RDensity = (double)RSize.roundToDouble() /
1966 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001967 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001968 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001969 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001970 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1971 << "LDensity: " << LDensity
1972 << ", RDensity: " << RDensity << '\n'
1973 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001974 if (FMetric < Metric) {
1975 Pivot = J;
1976 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001977 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001978 }
1979
1980 LSize += J->size();
1981 RSize -= J->size();
1982 }
1983 if (areJTsAllowed(TLI)) {
1984 // If our case is dense we *really* should handle it earlier!
1985 assert((FMetric > 0) && "Should handle dense range earlier!");
1986 } else {
1987 Pivot = CR.Range.first + Size/2;
1988 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990 CaseRange LHSR(CR.Range.first, Pivot);
1991 CaseRange RHSR(Pivot, CR.Range.second);
1992 Constant *C = Pivot->Low;
1993 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001994
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001995 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001996 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001997 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001998 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001999 // Pivot's Value, then we can branch directly to the LHS's Target,
2000 // rather than creating a leaf node for it.
2001 if ((LHSR.second - LHSR.first) == 1 &&
2002 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002003 cast<ConstantInt>(C)->getValue() ==
2004 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002005 TrueBB = LHSR.first->BB;
2006 } else {
2007 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2008 CurMF->insert(BBI, TrueBB);
2009 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002010
2011 // Put SV in a virtual register to make it available from the new blocks.
2012 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002013 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002015 // Similar to the optimization above, if the Value being switched on is
2016 // known to be less than the Constant CR.LT, and the current Case Value
2017 // is CR.LT - 1, then we can branch directly to the target block for
2018 // the current Case Value, rather than emitting a RHS leaf node for it.
2019 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002020 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2021 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002022 FalseBB = RHSR.first->BB;
2023 } else {
2024 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2025 CurMF->insert(BBI, FalseBB);
2026 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002027
2028 // Put SV in a virtual register to make it available from the new blocks.
2029 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002030 }
2031
2032 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002033 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 // Otherwise, branch to LHS.
2035 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
2036
Dan Gohman99be8ae2010-04-19 22:41:47 +00002037 if (CR.CaseBB == SwitchBB)
2038 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039 else
2040 SwitchCases.push_back(CB);
2041
2042 return true;
2043}
2044
2045/// handleBitTestsSwitchCase - if current case range has few destination and
2046/// range span less, than machine word bitwidth, encode case range into series
2047/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002048bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2049 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002050 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002051 MachineBasicBlock* Default,
2052 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00002053 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002054 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002055
2056 Case& FrontCase = *CR.Range.first;
2057 Case& BackCase = *(CR.Range.second-1);
2058
2059 // Get the MachineFunction which holds the current MBB. This is used when
2060 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002061 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002062
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002063 // If target does not have legal shift left, do not emit bit tests at all.
2064 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
2065 return false;
2066
Anton Korobeynikov23218582008-12-23 22:25:27 +00002067 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002068 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2069 I!=E; ++I) {
2070 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002071 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002074 // Count unique destinations
2075 SmallSet<MachineBasicBlock*, 4> Dests;
2076 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2077 Dests.insert(I->BB);
2078 if (Dests.size() > 3)
2079 // Don't bother the code below, if there are too much unique destinations
2080 return false;
2081 }
David Greene4b69d992010-01-05 01:24:57 +00002082 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002083 << Dests.size() << '\n'
2084 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002086 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002087 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2088 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002089 APInt cmpRange = maxValue - minValue;
2090
David Greene4b69d992010-01-05 01:24:57 +00002091 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002092 << "Low bound: " << minValue << '\n'
2093 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002094
Dan Gohmane0567812010-04-08 23:03:40 +00002095 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002096 (!(Dests.size() == 1 && numCmps >= 3) &&
2097 !(Dests.size() == 2 && numCmps >= 5) &&
2098 !(Dests.size() >= 3 && numCmps >= 6)))
2099 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002100
David Greene4b69d992010-01-05 01:24:57 +00002101 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002102 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002104 // Optimize the case where all the case values fit in a
2105 // word without having to subtract minValue. In this case,
2106 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00002107 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002108 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002109 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002110 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002111 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002112
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002113 CaseBitsVector CasesBits;
2114 unsigned i, count = 0;
2115
2116 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2117 MachineBasicBlock* Dest = I->BB;
2118 for (i = 0; i < count; ++i)
2119 if (Dest == CasesBits[i].BB)
2120 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002121
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002122 if (i == count) {
2123 assert((count < 3) && "Too much destinations to test!");
2124 CasesBits.push_back(CaseBits(0, Dest, 0));
2125 count++;
2126 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002127
2128 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2129 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2130
2131 uint64_t lo = (lowValue - lowBound).getZExtValue();
2132 uint64_t hi = (highValue - lowBound).getZExtValue();
2133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002134 for (uint64_t j = lo; j <= hi; j++) {
2135 CasesBits[i].Mask |= 1ULL << j;
2136 CasesBits[i].Bits++;
2137 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002139 }
2140 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002141
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002142 BitTestInfo BTC;
2143
2144 // Figure out which block is immediately after the current one.
2145 MachineFunction::iterator BBI = CR.CaseBB;
2146 ++BBI;
2147
2148 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2149
David Greene4b69d992010-01-05 01:24:57 +00002150 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002152 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002153 << ", Bits: " << CasesBits[i].Bits
2154 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002155
2156 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2157 CurMF->insert(BBI, CaseBB);
2158 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2159 CaseBB,
2160 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002161
2162 // Put SV in a virtual register to make it available from the new blocks.
2163 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002164 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002165
2166 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002167 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002168 CR.CaseBB, Default, BTC);
2169
Dan Gohman99be8ae2010-04-19 22:41:47 +00002170 if (CR.CaseBB == SwitchBB)
2171 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002172
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002173 BitTestCases.push_back(BTB);
2174
2175 return true;
2176}
2177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002178/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002179size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2180 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002181 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002182
2183 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002184 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002185 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2186 Cases.push_back(Case(SI.getSuccessorValue(i),
2187 SI.getSuccessorValue(i),
2188 SMBB));
2189 }
2190 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2191
2192 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002193 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002194 // Must recompute end() each iteration because it may be
2195 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002196 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2197 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2198 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002199 MachineBasicBlock* nextBB = J->BB;
2200 MachineBasicBlock* currentBB = I->BB;
2201
2202 // If the two neighboring cases go to the same destination, merge them
2203 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002204 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205 I->High = J->High;
2206 J = Cases.erase(J);
2207 } else {
2208 I = J++;
2209 }
2210 }
2211
2212 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2213 if (I->Low != I->High)
2214 // A range counts double, since it requires two compares.
2215 ++numCmps;
2216 }
2217
2218 return numCmps;
2219}
2220
Dan Gohman46510a72010-04-15 01:51:59 +00002221void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002222 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002223
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 // Figure out which block is immediately after the current one.
2225 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2227
2228 // If there is only the default destination, branch to it if it is not the
2229 // next basic block. Otherwise, just fall through.
2230 if (SI.getNumOperands() == 2) {
2231 // Update machine-CFG edges.
2232
2233 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002234 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002235 if (Default != NextBlock)
2236 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
2237 MVT::Other, getControlRoot(),
2238 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240 return;
2241 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002242
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002243 // If there are any non-default case statements, create a vector of Cases
2244 // representing each one, and sort the vector so that we can efficiently
2245 // create a binary search tree from them.
2246 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002247 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002248 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002249 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002250 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002251
2252 // Get the Value to be switched on and default basic blocks, which will be
2253 // inserted into CaseBlock records, representing basic blocks in the binary
2254 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002255 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002256
2257 // Push the initial CaseRec onto the worklist
2258 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002259 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2260 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002261
2262 while (!WorkList.empty()) {
2263 // Grab a record representing a case range to process off the worklist
2264 CaseRec CR = WorkList.back();
2265 WorkList.pop_back();
2266
Dan Gohman99be8ae2010-04-19 22:41:47 +00002267 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002268 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002270 // If the range has few cases (two or less) emit a series of specific
2271 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002272 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002274
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002275 // If the switch has more than 5 blocks, and at least 40% dense, and the
2276 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002277 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002278 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002279 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002280
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002281 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2282 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002283 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002284 }
2285}
2286
Dan Gohman46510a72010-04-15 01:51:59 +00002287void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002288 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002289
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002290 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002291 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002292 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002293 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002294 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002295 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002296 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2297 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002298 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002299
Bill Wendling4533cac2010-01-28 21:51:40 +00002300 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2301 MVT::Other, getControlRoot(),
2302 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002303}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002304
Dan Gohman46510a72010-04-15 01:51:59 +00002305void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002306 // -0.0 - X --> fneg
2307 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002308 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002309 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2310 const VectorType *DestTy = cast<VectorType>(I.getType());
2311 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002312 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002313 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002314 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002315 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002316 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002317 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2318 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002319 return;
2320 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002321 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002322 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002323
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002324 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002325 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002326 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002327 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2328 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002329 return;
2330 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002331
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002332 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002333}
2334
Dan Gohman46510a72010-04-15 01:51:59 +00002335void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002336 SDValue Op1 = getValue(I.getOperand(0));
2337 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002338 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2339 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002340}
2341
Dan Gohman46510a72010-04-15 01:51:59 +00002342void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002343 SDValue Op1 = getValue(I.getOperand(0));
2344 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002345 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002346 Op2.getValueType() != TLI.getShiftAmountTy()) {
2347 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002348 EVT PTy = TLI.getPointerTy();
2349 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002350 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002351 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2352 TLI.getShiftAmountTy(), Op2);
2353 // If the operand is larger than the shift count type but the shift
2354 // count type has enough bits to represent any shift value, truncate
2355 // it now. This is a common case and it exposes the truncate to
2356 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002357 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002358 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2359 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2360 TLI.getShiftAmountTy(), Op2);
2361 // Otherwise we'll need to temporarily settle for some other
2362 // convenient type; type legalization will make adjustments as
2363 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002364 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002365 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002366 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002367 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002368 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002369 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002370 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002371
Bill Wendling4533cac2010-01-28 21:51:40 +00002372 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2373 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002374}
2375
Dan Gohman46510a72010-04-15 01:51:59 +00002376void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002377 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002378 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002379 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002380 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002381 predicate = ICmpInst::Predicate(IC->getPredicate());
2382 SDValue Op1 = getValue(I.getOperand(0));
2383 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002384 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002385
Owen Andersone50ed302009-08-10 22:56:29 +00002386 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002387 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002388}
2389
Dan Gohman46510a72010-04-15 01:51:59 +00002390void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002391 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002392 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002393 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002394 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002395 predicate = FCmpInst::Predicate(FC->getPredicate());
2396 SDValue Op1 = getValue(I.getOperand(0));
2397 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002398 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002400 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002401}
2402
Dan Gohman46510a72010-04-15 01:51:59 +00002403void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002404 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002405 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2406 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002407 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002408
Bill Wendling49fcff82009-12-21 22:30:11 +00002409 SmallVector<SDValue, 4> Values(NumValues);
2410 SDValue Cond = getValue(I.getOperand(0));
2411 SDValue TrueVal = getValue(I.getOperand(1));
2412 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002413
Bill Wendling4533cac2010-01-28 21:51:40 +00002414 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002415 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002416 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2417 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002418 SDValue(TrueVal.getNode(),
2419 TrueVal.getResNo() + i),
2420 SDValue(FalseVal.getNode(),
2421 FalseVal.getResNo() + i));
2422
Bill Wendling4533cac2010-01-28 21:51:40 +00002423 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2424 DAG.getVTList(&ValueVTs[0], NumValues),
2425 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002426}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002427
Dan Gohman46510a72010-04-15 01:51:59 +00002428void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002429 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2430 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002431 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002432 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002433}
2434
Dan Gohman46510a72010-04-15 01:51:59 +00002435void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002436 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2437 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2438 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002439 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002440 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002441}
2442
Dan Gohman46510a72010-04-15 01:51:59 +00002443void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002444 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2445 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2446 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002447 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002448 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002449}
2450
Dan Gohman46510a72010-04-15 01:51:59 +00002451void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002452 // FPTrunc is never a no-op cast, no need to check
2453 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002454 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002455 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2456 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002457}
2458
Dan Gohman46510a72010-04-15 01:51:59 +00002459void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002460 // FPTrunc is never a no-op cast, no need to check
2461 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002462 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002463 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002464}
2465
Dan Gohman46510a72010-04-15 01:51:59 +00002466void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002467 // FPToUI is never a no-op cast, no need to check
2468 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002469 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002470 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002471}
2472
Dan Gohman46510a72010-04-15 01:51:59 +00002473void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002474 // FPToSI is never a no-op cast, no need to check
2475 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002476 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002477 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002478}
2479
Dan Gohman46510a72010-04-15 01:51:59 +00002480void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002481 // UIToFP is never a no-op cast, no need to check
2482 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002483 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002484 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002485}
2486
Dan Gohman46510a72010-04-15 01:51:59 +00002487void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002488 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002489 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002490 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002491 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492}
2493
Dan Gohman46510a72010-04-15 01:51:59 +00002494void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002495 // What to do depends on the size of the integer and the size of the pointer.
2496 // We can either truncate, zero extend, or no-op, accordingly.
2497 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002498 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002499 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002500}
2501
Dan Gohman46510a72010-04-15 01:51:59 +00002502void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002503 // What to do depends on the size of the integer and the size of the pointer.
2504 // We can either truncate, zero extend, or no-op, accordingly.
2505 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002506 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002507 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002508}
2509
Dan Gohman46510a72010-04-15 01:51:59 +00002510void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002511 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002512 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002513
Bill Wendling49fcff82009-12-21 22:30:11 +00002514 // BitCast assures us that source and destination are the same size so this is
2515 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002516 if (DestVT != N.getValueType())
2517 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2518 DestVT, N)); // convert types.
2519 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002520 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002521}
2522
Dan Gohman46510a72010-04-15 01:51:59 +00002523void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002524 SDValue InVec = getValue(I.getOperand(0));
2525 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002526 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002527 TLI.getPointerTy(),
2528 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002529 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2530 TLI.getValueType(I.getType()),
2531 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002532}
2533
Dan Gohman46510a72010-04-15 01:51:59 +00002534void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002535 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002536 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002537 TLI.getPointerTy(),
2538 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002539 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2540 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002541}
2542
Mon P Wangaeb06d22008-11-10 04:46:22 +00002543// Utility for visitShuffleVector - Returns true if the mask is mask starting
2544// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002545static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2546 unsigned MaskNumElts = Mask.size();
2547 for (unsigned i = 0; i != MaskNumElts; ++i)
2548 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002549 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002550 return true;
2551}
2552
Dan Gohman46510a72010-04-15 01:51:59 +00002553void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002554 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002555 SDValue Src1 = getValue(I.getOperand(0));
2556 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002557
Nate Begeman9008ca62009-04-27 18:41:29 +00002558 // Convert the ConstantVector mask operand into an array of ints, with -1
2559 // representing undef values.
2560 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002561 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002562 unsigned MaskNumElts = MaskElts.size();
2563 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002564 if (isa<UndefValue>(MaskElts[i]))
2565 Mask.push_back(-1);
2566 else
2567 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2568 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002569
Owen Andersone50ed302009-08-10 22:56:29 +00002570 EVT VT = TLI.getValueType(I.getType());
2571 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002572 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002573
Mon P Wangc7849c22008-11-16 05:06:27 +00002574 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002575 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2576 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002577 return;
2578 }
2579
2580 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002581 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2582 // Mask is longer than the source vectors and is a multiple of the source
2583 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002584 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002585 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2586 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002587 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2588 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002589 return;
2590 }
2591
Mon P Wangc7849c22008-11-16 05:06:27 +00002592 // Pad both vectors with undefs to make them the same length as the mask.
2593 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002594 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2595 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002596 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002597
Nate Begeman9008ca62009-04-27 18:41:29 +00002598 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2599 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002600 MOps1[0] = Src1;
2601 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002602
2603 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2604 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002605 &MOps1[0], NumConcat);
2606 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002607 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002608 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002609
Mon P Wangaeb06d22008-11-10 04:46:22 +00002610 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002611 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002612 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002613 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002614 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002615 MappedOps.push_back(Idx);
2616 else
2617 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002618 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002619
Bill Wendling4533cac2010-01-28 21:51:40 +00002620 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2621 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002622 return;
2623 }
2624
Mon P Wangc7849c22008-11-16 05:06:27 +00002625 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002626 // Analyze the access pattern of the vector to see if we can extract
2627 // two subvectors and do the shuffle. The analysis is done by calculating
2628 // the range of elements the mask access on both vectors.
2629 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2630 int MaxRange[2] = {-1, -1};
2631
Nate Begeman5a5ca152009-04-29 05:20:52 +00002632 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002633 int Idx = Mask[i];
2634 int Input = 0;
2635 if (Idx < 0)
2636 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002637
Nate Begeman5a5ca152009-04-29 05:20:52 +00002638 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002639 Input = 1;
2640 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002641 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002642 if (Idx > MaxRange[Input])
2643 MaxRange[Input] = Idx;
2644 if (Idx < MinRange[Input])
2645 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002646 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002647
Mon P Wangc7849c22008-11-16 05:06:27 +00002648 // Check if the access is smaller than the vector size and can we find
2649 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002650 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2651 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002652 int StartIdx[2]; // StartIdx to extract from
2653 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002654 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002655 RangeUse[Input] = 0; // Unused
2656 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002657 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002658 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002659 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002660 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002661 RangeUse[Input] = 1; // Extract from beginning of the vector
2662 StartIdx[Input] = 0;
2663 } else {
2664 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002665 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002666 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002667 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002668 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002669 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002670 }
2671
Bill Wendling636e2582009-08-21 18:16:06 +00002672 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002673 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002674 return;
2675 }
2676 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2677 // Extract appropriate subvector and generate a vector shuffle
2678 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002679 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002680 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002681 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002682 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002683 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002684 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002685 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002686
Mon P Wangc7849c22008-11-16 05:06:27 +00002687 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002688 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002689 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002690 int Idx = Mask[i];
2691 if (Idx < 0)
2692 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002693 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002694 MappedOps.push_back(Idx - StartIdx[0]);
2695 else
2696 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002697 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002698
Bill Wendling4533cac2010-01-28 21:51:40 +00002699 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2700 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002701 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002702 }
2703 }
2704
Mon P Wangc7849c22008-11-16 05:06:27 +00002705 // We can't use either concat vectors or extract subvectors so fall back to
2706 // replacing the shuffle with extract and build vector.
2707 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002708 EVT EltVT = VT.getVectorElementType();
2709 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002710 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002711 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002712 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002713 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002714 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002715 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002716 SDValue Res;
2717
Nate Begeman5a5ca152009-04-29 05:20:52 +00002718 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002719 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2720 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002721 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002722 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2723 EltVT, Src2,
2724 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2725
2726 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002727 }
2728 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002729
Bill Wendling4533cac2010-01-28 21:51:40 +00002730 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2731 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002732}
2733
Dan Gohman46510a72010-04-15 01:51:59 +00002734void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002735 const Value *Op0 = I.getOperand(0);
2736 const Value *Op1 = I.getOperand(1);
2737 const Type *AggTy = I.getType();
2738 const Type *ValTy = Op1->getType();
2739 bool IntoUndef = isa<UndefValue>(Op0);
2740 bool FromUndef = isa<UndefValue>(Op1);
2741
2742 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2743 I.idx_begin(), I.idx_end());
2744
Owen Andersone50ed302009-08-10 22:56:29 +00002745 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002746 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002747 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002748 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2749
2750 unsigned NumAggValues = AggValueVTs.size();
2751 unsigned NumValValues = ValValueVTs.size();
2752 SmallVector<SDValue, 4> Values(NumAggValues);
2753
2754 SDValue Agg = getValue(Op0);
2755 SDValue Val = getValue(Op1);
2756 unsigned i = 0;
2757 // Copy the beginning value(s) from the original aggregate.
2758 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002759 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002760 SDValue(Agg.getNode(), Agg.getResNo() + i);
2761 // Copy values from the inserted value(s).
2762 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002763 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002764 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2765 // Copy remaining value(s) from the original aggregate.
2766 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002767 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002768 SDValue(Agg.getNode(), Agg.getResNo() + i);
2769
Bill Wendling4533cac2010-01-28 21:51:40 +00002770 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2771 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2772 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002773}
2774
Dan Gohman46510a72010-04-15 01:51:59 +00002775void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002776 const Value *Op0 = I.getOperand(0);
2777 const Type *AggTy = Op0->getType();
2778 const Type *ValTy = I.getType();
2779 bool OutOfUndef = isa<UndefValue>(Op0);
2780
2781 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2782 I.idx_begin(), I.idx_end());
2783
Owen Andersone50ed302009-08-10 22:56:29 +00002784 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002785 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2786
2787 unsigned NumValValues = ValValueVTs.size();
2788 SmallVector<SDValue, 4> Values(NumValValues);
2789
2790 SDValue Agg = getValue(Op0);
2791 // Copy out the selected value(s).
2792 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2793 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002794 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002795 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002796 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002797
Bill Wendling4533cac2010-01-28 21:51:40 +00002798 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2799 DAG.getVTList(&ValValueVTs[0], NumValValues),
2800 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002801}
2802
Dan Gohman46510a72010-04-15 01:51:59 +00002803void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002804 SDValue N = getValue(I.getOperand(0));
2805 const Type *Ty = I.getOperand(0)->getType();
2806
Dan Gohman46510a72010-04-15 01:51:59 +00002807 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002809 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002810 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2811 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2812 if (Field) {
2813 // N = N + Offset
2814 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002815 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002816 DAG.getIntPtrConstant(Offset));
2817 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002818
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002819 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002820 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2821 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2822
2823 // Offset canonically 0 for unions, but type changes
2824 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 } else {
2826 Ty = cast<SequentialType>(Ty)->getElementType();
2827
2828 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002829 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00002830 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002831 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002832 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002833 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002834 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002835 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002836 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002837 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2838 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002839 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002840 else
Evan Chengb1032a82009-02-09 20:54:38 +00002841 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002842
Dale Johannesen66978ee2009-01-31 02:22:37 +00002843 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002844 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002845 continue;
2846 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002847
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002849 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2850 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002851 SDValue IdxN = getValue(Idx);
2852
2853 // If the index is smaller or larger than intptr_t, truncate or extend
2854 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002855 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002856
2857 // If this is a multiply by a power of two, turn it into a shl
2858 // immediately. This is a very common case.
2859 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002860 if (ElementSize.isPowerOf2()) {
2861 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002862 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002863 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002864 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002865 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002866 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002867 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002868 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869 }
2870 }
2871
Scott Michelfdc40a02009-02-17 22:15:04 +00002872 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002873 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002874 }
2875 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002877 setValue(&I, N);
2878}
2879
Dan Gohman46510a72010-04-15 01:51:59 +00002880void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881 // If this is a fixed sized alloca in the entry block of the function,
2882 // allocate it statically on the stack.
2883 if (FuncInfo.StaticAllocaMap.count(&I))
2884 return; // getValue will auto-populate this.
2885
2886 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002887 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002888 unsigned Align =
2889 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2890 I.getAlignment());
2891
2892 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002893
Owen Andersone50ed302009-08-10 22:56:29 +00002894 EVT IntPtr = TLI.getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00002895 if (AllocSize.getValueType() != IntPtr)
2896 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
2897
2898 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr,
2899 AllocSize,
2900 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002901
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002902 // Handle alignment. If the requested alignment is less than or equal to
2903 // the stack alignment, ignore it. If the size is greater than or equal to
2904 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002905 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002906 if (Align <= StackAlign)
2907 Align = 0;
2908
2909 // Round the size of the allocation up to the stack alignment size
2910 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002911 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002912 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002913 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002915 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002916 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002917 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002918 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2919
2920 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002921 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002922 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002923 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002924 setValue(&I, DSA);
2925 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002926
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002927 // Inform the Frame Information that we have just allocated a variable-sized
2928 // object.
Eric Christopher2b8271e2010-07-17 00:28:22 +00002929 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002930}
2931
Dan Gohman46510a72010-04-15 01:51:59 +00002932void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002933 const Value *SV = I.getOperand(0);
2934 SDValue Ptr = getValue(SV);
2935
2936 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002937
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002938 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002939 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002940 unsigned Alignment = I.getAlignment();
2941
Owen Andersone50ed302009-08-10 22:56:29 +00002942 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002943 SmallVector<uint64_t, 4> Offsets;
2944 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2945 unsigned NumValues = ValueVTs.size();
2946 if (NumValues == 0)
2947 return;
2948
2949 SDValue Root;
2950 bool ConstantMemory = false;
2951 if (I.isVolatile())
2952 // Serialize volatile loads with other side effects.
2953 Root = getRoot();
2954 else if (AA->pointsToConstantMemory(SV)) {
2955 // Do not serialize (non-volatile) loads of constant memory with anything.
2956 Root = DAG.getEntryNode();
2957 ConstantMemory = true;
2958 } else {
2959 // Do not serialize non-volatile loads against each other.
2960 Root = DAG.getRoot();
2961 }
2962
2963 SmallVector<SDValue, 4> Values(NumValues);
2964 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002965 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002966 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002967 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2968 PtrVT, Ptr,
2969 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002970 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002971 A, SV, Offsets[i], isVolatile,
2972 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002973
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002974 Values[i] = L;
2975 Chains[i] = L.getValue(1);
2976 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002977
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002978 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002979 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002980 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002981 if (isVolatile)
2982 DAG.setRoot(Chain);
2983 else
2984 PendingLoads.push_back(Chain);
2985 }
2986
Bill Wendling4533cac2010-01-28 21:51:40 +00002987 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2988 DAG.getVTList(&ValueVTs[0], NumValues),
2989 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002990}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002991
Dan Gohman46510a72010-04-15 01:51:59 +00002992void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2993 const Value *SrcV = I.getOperand(0);
2994 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002995
Owen Andersone50ed302009-08-10 22:56:29 +00002996 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002997 SmallVector<uint64_t, 4> Offsets;
2998 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2999 unsigned NumValues = ValueVTs.size();
3000 if (NumValues == 0)
3001 return;
3002
3003 // Get the lowered operands. Note that we do this after
3004 // checking if NumResults is zero, because with zero results
3005 // the operands won't have values in the map.
3006 SDValue Src = getValue(SrcV);
3007 SDValue Ptr = getValue(PtrV);
3008
3009 SDValue Root = getRoot();
3010 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003011 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003012 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003013 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003014 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003015
3016 for (unsigned i = 0; i != NumValues; ++i) {
3017 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3018 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003019 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003020 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00003021 Add, PtrV, Offsets[i], isVolatile,
3022 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00003023 }
3024
Bill Wendling4533cac2010-01-28 21:51:40 +00003025 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3026 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003027}
3028
3029/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3030/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003031void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003032 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003033 bool HasChain = !I.doesNotAccessMemory();
3034 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3035
3036 // Build the operand list.
3037 SmallVector<SDValue, 8> Ops;
3038 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3039 if (OnlyLoad) {
3040 // We don't need to serialize loads against other loads.
3041 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003042 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003043 Ops.push_back(getRoot());
3044 }
3045 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003046
3047 // Info is set by getTgtMemInstrinsic
3048 TargetLowering::IntrinsicInfo Info;
3049 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3050
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003051 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003052 if (!IsTgtIntrinsic)
3053 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003054
3055 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003056 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3057 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003058 assert(TLI.isTypeLegal(Op.getValueType()) &&
3059 "Intrinsic uses a non-legal type?");
3060 Ops.push_back(Op);
3061 }
3062
Owen Andersone50ed302009-08-10 22:56:29 +00003063 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003064 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3065#ifndef NDEBUG
3066 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3067 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3068 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003069 }
Bob Wilson8d919552009-07-31 22:41:21 +00003070#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003071
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003072 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003073 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003074
Bob Wilson8d919552009-07-31 22:41:21 +00003075 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003076
3077 // Create the node.
3078 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003079 if (IsTgtIntrinsic) {
3080 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003081 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003082 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003083 Info.memVT, Info.ptrVal, Info.offset,
3084 Info.align, Info.vol,
3085 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003086 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003087 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003088 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003089 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003090 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003091 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003092 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003093 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003094 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003095 }
3096
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003097 if (HasChain) {
3098 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3099 if (OnlyLoad)
3100 PendingLoads.push_back(Chain);
3101 else
3102 DAG.setRoot(Chain);
3103 }
Bill Wendling856ff412009-12-22 00:12:37 +00003104
Benjamin Kramerf0127052010-01-05 13:12:22 +00003105 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003106 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003107 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003108 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003109 }
Bill Wendling856ff412009-12-22 00:12:37 +00003110
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003111 setValue(&I, Result);
3112 }
3113}
3114
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003115/// GetSignificand - Get the significand and build it into a floating-point
3116/// number with exponent of 1:
3117///
3118/// Op = (Op & 0x007fffff) | 0x3f800000;
3119///
3120/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003121static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00003122GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003123 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3124 DAG.getConstant(0x007fffff, MVT::i32));
3125 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3126 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003127 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003128}
3129
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003130/// GetExponent - Get the exponent:
3131///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003132/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003133///
3134/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003135static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003136GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00003137 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003138 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3139 DAG.getConstant(0x7f800000, MVT::i32));
3140 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003141 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003142 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3143 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003144 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003145}
3146
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003147/// getF32Constant - Get 32-bit floating point constant.
3148static SDValue
3149getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003151}
3152
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003153/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003154/// visitIntrinsicCall: I is a call instruction
3155/// Op is the associated NodeType for I
3156const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003157SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
3158 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003159 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003160 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003161 DAG.getAtomic(Op, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00003162 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003163 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00003164 getValue(I.getArgOperand(0)),
3165 getValue(I.getArgOperand(1)),
3166 I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003167 setValue(&I, L);
3168 DAG.setRoot(L.getValue(1));
3169 return 0;
3170}
3171
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003172// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003173const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003174SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Gabor Greif0635f352010-06-25 09:38:13 +00003175 SDValue Op1 = getValue(I.getArgOperand(0));
3176 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling74c37652008-12-09 22:08:41 +00003177
Owen Anderson825b72b2009-08-11 20:47:22 +00003178 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00003179 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003180 return 0;
3181}
Bill Wendling74c37652008-12-09 22:08:41 +00003182
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003183/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3184/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003185void
Dan Gohman46510a72010-04-15 01:51:59 +00003186SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003187 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003188 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003189
Gabor Greif0635f352010-06-25 09:38:13 +00003190 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003191 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003192 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003193
3194 // Put the exponent in the right bit position for later addition to the
3195 // final result:
3196 //
3197 // #define LOG2OFe 1.4426950f
3198 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003200 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003201 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003202
3203 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003204 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3205 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003206
3207 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003208 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003209 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003210
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003211 if (LimitFloatPrecision <= 6) {
3212 // For floating-point precision of 6:
3213 //
3214 // TwoToFractionalPartOfX =
3215 // 0.997535578f +
3216 // (0.735607626f + 0.252464424f * x) * x;
3217 //
3218 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003219 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003220 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003221 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003222 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003223 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3224 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003225 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003227
3228 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003230 TwoToFracPartOfX, IntegerPartOfX);
3231
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003233 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3234 // For floating-point precision of 12:
3235 //
3236 // TwoToFractionalPartOfX =
3237 // 0.999892986f +
3238 // (0.696457318f +
3239 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3240 //
3241 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003242 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003243 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3247 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003248 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003249 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3250 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003251 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003252 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003253
3254 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003255 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003256 TwoToFracPartOfX, IntegerPartOfX);
3257
Owen Anderson825b72b2009-08-11 20:47:22 +00003258 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003259 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3260 // For floating-point precision of 18:
3261 //
3262 // TwoToFractionalPartOfX =
3263 // 0.999999982f +
3264 // (0.693148872f +
3265 // (0.240227044f +
3266 // (0.554906021e-1f +
3267 // (0.961591928e-2f +
3268 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3269 //
3270 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003271 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003272 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003273 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003274 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3276 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003277 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003278 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3279 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003280 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003281 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3282 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003283 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003284 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3285 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003286 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003287 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3288 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003289 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003290 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003292
3293 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003295 TwoToFracPartOfX, IntegerPartOfX);
3296
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003298 }
3299 } else {
3300 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003301 result = DAG.getNode(ISD::FEXP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003302 getValue(I.getArgOperand(0)).getValueType(),
3303 getValue(I.getArgOperand(0)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003304 }
3305
Dale Johannesen59e577f2008-09-05 18:38:42 +00003306 setValue(&I, result);
3307}
3308
Bill Wendling39150252008-09-09 20:39:27 +00003309/// visitLog - Lower a log intrinsic. Handles the special sequences for
3310/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003311void
Dan Gohman46510a72010-04-15 01:51:59 +00003312SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003313 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003314 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003315
Gabor Greif0635f352010-06-25 09:38:13 +00003316 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003317 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003318 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003320
3321 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003322 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003323 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003324 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003325
3326 // Get the significand and build it into a floating-point number with
3327 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003328 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003329
3330 if (LimitFloatPrecision <= 6) {
3331 // For floating-point precision of 6:
3332 //
3333 // LogofMantissa =
3334 // -1.1609546f +
3335 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003336 //
Bill Wendling39150252008-09-09 20:39:27 +00003337 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003338 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003339 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003341 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3343 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003344 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003345
Scott Michelfdc40a02009-02-17 22:15:04 +00003346 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003348 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3349 // For floating-point precision of 12:
3350 //
3351 // LogOfMantissa =
3352 // -1.7417939f +
3353 // (2.8212026f +
3354 // (-1.4699568f +
3355 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3356 //
3357 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003359 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003361 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3363 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003364 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3366 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003367 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003368 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3369 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003370 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003371
Scott Michelfdc40a02009-02-17 22:15:04 +00003372 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003374 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3375 // For floating-point precision of 18:
3376 //
3377 // LogOfMantissa =
3378 // -2.1072184f +
3379 // (4.2372794f +
3380 // (-3.7029485f +
3381 // (2.2781945f +
3382 // (-0.87823314f +
3383 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3384 //
3385 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003386 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003387 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003388 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003389 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003390 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3391 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003392 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003393 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3394 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003395 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3397 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003398 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3400 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003401 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3403 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003404 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003405
Scott Michelfdc40a02009-02-17 22:15:04 +00003406 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003407 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003408 }
3409 } else {
3410 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003411 result = DAG.getNode(ISD::FLOG, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003412 getValue(I.getArgOperand(0)).getValueType(),
3413 getValue(I.getArgOperand(0)));
Bill Wendling39150252008-09-09 20:39:27 +00003414 }
3415
Dale Johannesen59e577f2008-09-05 18:38:42 +00003416 setValue(&I, result);
3417}
3418
Bill Wendling3eb59402008-09-09 00:28:24 +00003419/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3420/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003421void
Dan Gohman46510a72010-04-15 01:51:59 +00003422SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003423 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003424 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003425
Gabor Greif0635f352010-06-25 09:38:13 +00003426 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003427 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003428 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003429 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003430
Bill Wendling39150252008-09-09 20:39:27 +00003431 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003432 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003433
Bill Wendling3eb59402008-09-09 00:28:24 +00003434 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003435 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003436 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003437
Bill Wendling3eb59402008-09-09 00:28:24 +00003438 // Different possible minimax approximations of significand in
3439 // floating-point for various degrees of accuracy over [1,2].
3440 if (LimitFloatPrecision <= 6) {
3441 // For floating-point precision of 6:
3442 //
3443 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3444 //
3445 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003446 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003447 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003449 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3451 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003453
Scott Michelfdc40a02009-02-17 22:15:04 +00003454 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003456 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3457 // For floating-point precision of 12:
3458 //
3459 // Log2ofMantissa =
3460 // -2.51285454f +
3461 // (4.07009056f +
3462 // (-2.12067489f +
3463 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003464 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003465 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003467 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003468 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003469 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003470 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3471 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003472 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003473 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3474 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003475 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3477 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003478 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003479
Scott Michelfdc40a02009-02-17 22:15:04 +00003480 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003481 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003482 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3483 // For floating-point precision of 18:
3484 //
3485 // Log2ofMantissa =
3486 // -3.0400495f +
3487 // (6.1129976f +
3488 // (-5.3420409f +
3489 // (3.2865683f +
3490 // (-1.2669343f +
3491 // (0.27515199f -
3492 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3493 //
3494 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003496 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003498 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003499 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3500 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003501 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003502 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3503 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003504 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003505 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3506 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003507 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3509 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003510 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3512 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003513 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003514
Scott Michelfdc40a02009-02-17 22:15:04 +00003515 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003516 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003517 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003518 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003519 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003520 result = DAG.getNode(ISD::FLOG2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003521 getValue(I.getArgOperand(0)).getValueType(),
3522 getValue(I.getArgOperand(0)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003523 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003524
Dale Johannesen59e577f2008-09-05 18:38:42 +00003525 setValue(&I, result);
3526}
3527
Bill Wendling3eb59402008-09-09 00:28:24 +00003528/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3529/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003530void
Dan Gohman46510a72010-04-15 01:51:59 +00003531SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003532 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003533 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003534
Gabor Greif0635f352010-06-25 09:38:13 +00003535 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003536 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003537 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003539
Bill Wendling39150252008-09-09 20:39:27 +00003540 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003541 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003543 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003544
3545 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003546 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003547 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003548
3549 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003550 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003551 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003552 // Log10ofMantissa =
3553 // -0.50419619f +
3554 // (0.60948995f - 0.10380950f * x) * x;
3555 //
3556 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003558 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003559 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003560 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3562 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003564
Scott Michelfdc40a02009-02-17 22:15:04 +00003565 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003566 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003567 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3568 // For floating-point precision of 12:
3569 //
3570 // Log10ofMantissa =
3571 // -0.64831180f +
3572 // (0.91751397f +
3573 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3574 //
3575 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003576 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003577 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003579 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003580 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3581 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003582 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3584 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003585 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003586
Scott Michelfdc40a02009-02-17 22:15:04 +00003587 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003589 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003590 // For floating-point precision of 18:
3591 //
3592 // Log10ofMantissa =
3593 // -0.84299375f +
3594 // (1.5327582f +
3595 // (-1.0688956f +
3596 // (0.49102474f +
3597 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3598 //
3599 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003601 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003603 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3605 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003606 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003607 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3608 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003609 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3611 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003612 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3614 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003615 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003616
Scott Michelfdc40a02009-02-17 22:15:04 +00003617 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003618 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003619 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003620 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003621 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003622 result = DAG.getNode(ISD::FLOG10, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003623 getValue(I.getArgOperand(0)).getValueType(),
3624 getValue(I.getArgOperand(0)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003625 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003626
Dale Johannesen59e577f2008-09-05 18:38:42 +00003627 setValue(&I, result);
3628}
3629
Bill Wendlinge10c8142008-09-09 22:39:21 +00003630/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3631/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003632void
Dan Gohman46510a72010-04-15 01:51:59 +00003633SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003634 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003635 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003636
Gabor Greif0635f352010-06-25 09:38:13 +00003637 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003638 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003639 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003640
Owen Anderson825b72b2009-08-11 20:47:22 +00003641 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003642
3643 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003644 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3645 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003646
3647 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003648 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003649 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003650
3651 if (LimitFloatPrecision <= 6) {
3652 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003653 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003654 // TwoToFractionalPartOfX =
3655 // 0.997535578f +
3656 // (0.735607626f + 0.252464424f * x) * x;
3657 //
3658 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003659 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003660 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003662 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003663 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3664 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003665 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003666 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003667 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003669
Scott Michelfdc40a02009-02-17 22:15:04 +00003670 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003671 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003672 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3673 // For floating-point precision of 12:
3674 //
3675 // TwoToFractionalPartOfX =
3676 // 0.999892986f +
3677 // (0.696457318f +
3678 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3679 //
3680 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003681 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003682 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003684 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003685 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3686 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003687 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003688 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3689 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003690 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003691 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003692 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003693 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003694
Scott Michelfdc40a02009-02-17 22:15:04 +00003695 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003696 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003697 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3698 // For floating-point precision of 18:
3699 //
3700 // TwoToFractionalPartOfX =
3701 // 0.999999982f +
3702 // (0.693148872f +
3703 // (0.240227044f +
3704 // (0.554906021e-1f +
3705 // (0.961591928e-2f +
3706 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3707 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003708 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003709 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003710 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003711 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003712 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3713 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003714 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003715 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3716 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003717 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003718 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3719 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003720 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003721 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3722 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003723 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003724 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3725 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003726 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003728 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003729 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003730
Scott Michelfdc40a02009-02-17 22:15:04 +00003731 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003733 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003734 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003735 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003736 result = DAG.getNode(ISD::FEXP2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003737 getValue(I.getArgOperand(0)).getValueType(),
3738 getValue(I.getArgOperand(0)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003739 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003740
Dale Johannesen601d3c02008-09-05 01:48:15 +00003741 setValue(&I, result);
3742}
3743
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003744/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3745/// limited-precision mode with x == 10.0f.
3746void
Dan Gohman46510a72010-04-15 01:51:59 +00003747SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003748 SDValue result;
Gabor Greif0635f352010-06-25 09:38:13 +00003749 const Value *Val = I.getArgOperand(0);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003750 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003751 bool IsExp10 = false;
3752
Owen Anderson825b72b2009-08-11 20:47:22 +00003753 if (getValue(Val).getValueType() == MVT::f32 &&
Gabor Greif0635f352010-06-25 09:38:13 +00003754 getValue(I.getArgOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003755 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3756 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3757 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3758 APFloat Ten(10.0f);
3759 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3760 }
3761 }
3762 }
3763
3764 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003765 SDValue Op = getValue(I.getArgOperand(1));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003766
3767 // Put the exponent in the right bit position for later addition to the
3768 // final result:
3769 //
3770 // #define LOG2OF10 3.3219281f
3771 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003772 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003773 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003774 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003775
3776 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003777 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3778 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003779
3780 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003781 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003782 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003783
3784 if (LimitFloatPrecision <= 6) {
3785 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003786 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003787 // twoToFractionalPartOfX =
3788 // 0.997535578f +
3789 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003790 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003791 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003792 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003793 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003795 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3797 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003798 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003800 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003801 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003802
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003803 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003804 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003805 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3806 // For floating-point precision of 12:
3807 //
3808 // TwoToFractionalPartOfX =
3809 // 0.999892986f +
3810 // (0.696457318f +
3811 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3812 //
3813 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003815 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003817 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003818 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3819 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003820 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003821 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3822 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003823 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003824 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003825 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003826 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003827
Scott Michelfdc40a02009-02-17 22:15:04 +00003828 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003829 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003830 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3831 // For floating-point precision of 18:
3832 //
3833 // TwoToFractionalPartOfX =
3834 // 0.999999982f +
3835 // (0.693148872f +
3836 // (0.240227044f +
3837 // (0.554906021e-1f +
3838 // (0.961591928e-2f +
3839 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3840 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003841 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003842 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003843 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003844 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003845 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3846 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003847 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003848 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3849 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003850 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3852 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003853 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003854 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3855 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003856 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003857 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3858 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003859 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003860 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003861 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003862 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003863
Scott Michelfdc40a02009-02-17 22:15:04 +00003864 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003865 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003866 }
3867 } else {
3868 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003869 result = DAG.getNode(ISD::FPOW, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003870 getValue(I.getArgOperand(0)).getValueType(),
3871 getValue(I.getArgOperand(0)),
3872 getValue(I.getArgOperand(1)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003873 }
3874
3875 setValue(&I, result);
3876}
3877
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003878
3879/// ExpandPowI - Expand a llvm.powi intrinsic.
3880static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3881 SelectionDAG &DAG) {
3882 // If RHS is a constant, we can expand this out to a multiplication tree,
3883 // otherwise we end up lowering to a call to __powidf2 (for example). When
3884 // optimizing for size, we only want to do this if the expansion would produce
3885 // a small number of multiplies, otherwise we do the full expansion.
3886 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3887 // Get the exponent as a positive value.
3888 unsigned Val = RHSC->getSExtValue();
3889 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003890
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003891 // powi(x, 0) -> 1.0
3892 if (Val == 0)
3893 return DAG.getConstantFP(1.0, LHS.getValueType());
3894
Dan Gohmanae541aa2010-04-15 04:33:49 +00003895 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003896 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3897 // If optimizing for size, don't insert too many multiplies. This
3898 // inserts up to 5 multiplies.
3899 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3900 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003901 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003902 // powi(x,15) generates one more multiply than it should), but this has
3903 // the benefit of being both really simple and much better than a libcall.
3904 SDValue Res; // Logically starts equal to 1.0
3905 SDValue CurSquare = LHS;
3906 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003907 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003908 if (Res.getNode())
3909 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3910 else
3911 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003912 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003913
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003914 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3915 CurSquare, CurSquare);
3916 Val >>= 1;
3917 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003918
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003919 // If the original was negative, invert the result, producing 1/(x*x*x).
3920 if (RHSC->getSExtValue() < 0)
3921 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3922 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3923 return Res;
3924 }
3925 }
3926
3927 // Otherwise, expand to a libcall.
3928 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3929}
3930
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003931/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
3932/// argument, create the corresponding DBG_VALUE machine instruction for it now.
3933/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003934bool
Devang Patel78a06e52010-08-25 20:39:26 +00003935SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Dan Gohman5d11ea32010-05-01 00:33:16 +00003936 uint64_t Offset,
3937 const SDValue &N) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003938 if (!isa<Argument>(V))
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003939 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003940
Devang Patel719f6a92010-04-29 20:40:36 +00003941 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela83ce982010-04-29 18:50:36 +00003942 // Ignore inlined function arguments here.
3943 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00003944 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00003945 return false;
3946
Dan Gohman84023e02010-07-10 09:00:22 +00003947 MachineBasicBlock *MBB = FuncInfo.MBB;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003948 if (MBB != &MF.front())
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003949 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003950
3951 unsigned Reg = 0;
3952 if (N.getOpcode() == ISD::CopyFromReg) {
3953 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Evan Cheng1deef272010-04-29 00:59:34 +00003954 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003955 MachineRegisterInfo &RegInfo = MF.getRegInfo();
3956 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
3957 if (PR)
3958 Reg = PR;
3959 }
3960 }
3961
Evan Chenga36acad2010-04-29 06:33:38 +00003962 if (!Reg) {
3963 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
3964 if (VMI == FuncInfo.ValueMap.end())
3965 return false;
3966 Reg = VMI->second;
3967 }
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003968
3969 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
3970 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
3971 TII->get(TargetOpcode::DBG_VALUE))
Evan Chenga36acad2010-04-29 06:33:38 +00003972 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003973 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003974 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003975}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003976
Douglas Gregor7d9663c2010-05-11 06:17:44 +00003977// VisualStudio defines setjmp as _setjmp
3978#if defined(_MSC_VER) && defined(setjmp)
3979#define setjmp_undefined_for_visual_studio
3980#undef setjmp
3981#endif
3982
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003983/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3984/// we want to emit this as a call to a named external function, return the name
3985/// otherwise lower it and return null.
3986const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003987SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003988 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003989 SDValue Res;
3990
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003991 switch (Intrinsic) {
3992 default:
3993 // By default, turn this into a target intrinsic node.
3994 visitTargetIntrinsic(I, Intrinsic);
3995 return 0;
3996 case Intrinsic::vastart: visitVAStart(I); return 0;
3997 case Intrinsic::vaend: visitVAEnd(I); return 0;
3998 case Intrinsic::vacopy: visitVACopy(I); return 0;
3999 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00004000 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004001 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004002 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004003 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00004004 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004005 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004006 return 0;
4007 case Intrinsic::setjmp:
4008 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004009 case Intrinsic::longjmp:
4010 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004011 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004012 // Assert for address < 256 since we support only user defined address
4013 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004014 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004015 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004016 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004017 < 256 &&
4018 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004019 SDValue Op1 = getValue(I.getArgOperand(0));
4020 SDValue Op2 = getValue(I.getArgOperand(1));
4021 SDValue Op3 = getValue(I.getArgOperand(2));
4022 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4023 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00004024 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Gabor Greif0635f352010-06-25 09:38:13 +00004025 I.getArgOperand(0), 0, I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004026 return 0;
4027 }
Chris Lattner824b9582008-11-21 16:42:48 +00004028 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004029 // Assert for address < 256 since we support only user defined address
4030 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004031 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004032 < 256 &&
4033 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004034 SDValue Op1 = getValue(I.getArgOperand(0));
4035 SDValue Op2 = getValue(I.getArgOperand(1));
4036 SDValue Op3 = getValue(I.getArgOperand(2));
4037 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4038 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00004039 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Gabor Greif0635f352010-06-25 09:38:13 +00004040 I.getArgOperand(0), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004041 return 0;
4042 }
Chris Lattner824b9582008-11-21 16:42:48 +00004043 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004044 // Assert for address < 256 since we support only user defined address
4045 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004046 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004047 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004048 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004049 < 256 &&
4050 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004051 SDValue Op1 = getValue(I.getArgOperand(0));
4052 SDValue Op2 = getValue(I.getArgOperand(1));
4053 SDValue Op3 = getValue(I.getArgOperand(2));
4054 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4055 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004056
4057 // If the source and destination are known to not be aliases, we can
4058 // lower memmove as memcpy.
4059 uint64_t Size = -1ULL;
4060 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004061 Size = C->getZExtValue();
Gabor Greif0635f352010-06-25 09:38:13 +00004062 if (AA->alias(I.getArgOperand(0), Size, I.getArgOperand(1), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004063 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004064 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher723a05a2010-07-14 23:41:32 +00004065 false, I.getArgOperand(0), 0,
4066 I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004067 return 0;
4068 }
4069
Mon P Wang20adc9d2010-04-04 03:10:48 +00004070 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Gabor Greif0635f352010-06-25 09:38:13 +00004071 I.getArgOperand(0), 0, I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004072 return 0;
4073 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004074 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004075 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004076 if (!DIVariable(DI.getVariable()).Verify())
Devang Patel7e1e31f2009-07-02 22:43:26 +00004077 return 0;
4078
Devang Patelac1ceb32009-10-09 22:42:28 +00004079 MDNode *Variable = DI.getVariable();
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004080 // Parameters are handled specially.
Devang Patelf38c6c82010-04-28 23:24:13 +00004081 bool isParameter =
4082 DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable;
Dan Gohman46510a72010-04-15 01:51:59 +00004083 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004084 if (!Address)
4085 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00004086 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00004087 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004088 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004089
4090 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4091 // but do not always have a corresponding SDNode built. The SDNodeOrder
4092 // absolute, but not relative, values are different depending on whether
4093 // debug info exists.
4094 ++SDNodeOrder;
4095 SDValue &N = NodeMap[Address];
4096 SDDbgValue *SDV;
4097 if (N.getNode()) {
4098 if (isParameter && !AI) {
4099 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4100 if (FINode)
4101 // Byval parameter. We have a frame index at this point.
4102 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4103 0, dl, SDNodeOrder);
4104 else
4105 // Can't do anything with other non-AI cases yet. This might be a
4106 // parameter of a callee function that got inlined, for example.
4107 return 0;
4108 } else if (AI)
4109 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4110 0, dl, SDNodeOrder);
4111 else
4112 // Can't do anything with other non-AI cases yet.
4113 return 0;
4114 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4115 } else {
4116 // This isn't useful, but it shows what we're missing.
4117 SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()),
4118 0, dl, SDNodeOrder);
4119 DAG.AddDbgValue(SDV, 0, isParameter);
4120 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004121 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004122 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004123 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004124 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004125 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004126 return 0;
4127
4128 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004129 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004130 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004131 if (!V)
4132 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004133
4134 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4135 // but do not always have a corresponding SDNode built. The SDNodeOrder
4136 // absolute, but not relative, values are different depending on whether
4137 // debug info exists.
4138 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004139 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00004140 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004141 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4142 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004143 } else {
Devang Pateld47f3c82010-05-05 22:29:00 +00004144 bool createUndef = false;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004145 // Do not use getValue() in here; we don't want to generate code at
4146 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004147 SDValue N = NodeMap[V];
4148 if (!N.getNode() && isa<Argument>(V))
4149 // Check unused arguments map.
4150 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004151 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004152 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004153 SDV = DAG.getDbgValue(Variable, N.getNode(),
4154 N.getResNo(), Offset, dl, SDNodeOrder);
4155 DAG.AddDbgValue(SDV, N.getNode(), false);
4156 }
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004157 } else if (isa<PHINode>(V) && !V->use_empty() ) {
4158 // Do not call getValue(V) yet, as we don't want to generate code.
4159 // Remember it for later.
4160 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4161 DanglingDebugInfoMap[V] = DDI;
Devang Pateld47f3c82010-05-05 22:29:00 +00004162 } else
4163 createUndef = true;
4164 if (createUndef) {
Devang Patel00190342010-03-15 19:15:44 +00004165 // We may expand this to cover more cases. One case where we have no
4166 // data available is an unreferenced parameter; we need this fallback.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004167 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
4168 Offset, dl, SDNodeOrder);
4169 DAG.AddDbgValue(SDV, 0, false);
4170 }
Devang Patel00190342010-03-15 19:15:44 +00004171 }
4172
4173 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004174 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004175 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004176 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004177 // Don't handle byval struct arguments or VLAs, for example.
4178 if (!AI)
4179 return 0;
4180 DenseMap<const AllocaInst*, int>::iterator SI =
4181 FuncInfo.StaticAllocaMap.find(AI);
4182 if (SI == FuncInfo.StaticAllocaMap.end())
4183 return 0; // VLAs.
4184 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00004185
Chris Lattner512063d2010-04-05 06:19:28 +00004186 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4187 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4188 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004189 return 0;
4190 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004191 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004192 // Insert the EXCEPTIONADDR instruction.
Dan Gohman84023e02010-07-10 09:00:22 +00004193 assert(FuncInfo.MBB->isLandingPad() &&
Dan Gohman99be8ae2010-04-19 22:41:47 +00004194 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004196 SDValue Ops[1];
4197 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004198 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004199 setValue(&I, Op);
4200 DAG.setRoot(Op.getValue(1));
4201 return 0;
4202 }
4203
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004204 case Intrinsic::eh_selector: {
Dan Gohman84023e02010-07-10 09:00:22 +00004205 MachineBasicBlock *CallMBB = FuncInfo.MBB;
Chris Lattner512063d2010-04-05 06:19:28 +00004206 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00004207 if (CallMBB->isLandingPad())
4208 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004209 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004210#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004211 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004212#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004213 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4214 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman84023e02010-07-10 09:00:22 +00004215 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004216 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004217
Chris Lattner3a5815f2009-09-17 23:54:54 +00004218 // Insert the EHSELECTION instruction.
4219 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4220 SDValue Ops[2];
Gabor Greif0635f352010-06-25 09:38:13 +00004221 Ops[0] = getValue(I.getArgOperand(0));
Chris Lattner3a5815f2009-09-17 23:54:54 +00004222 Ops[1] = getRoot();
4223 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004224 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004225 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004226 return 0;
4227 }
4228
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004229 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004230 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004231 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004232 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4233 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004234 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004235 return 0;
4236 }
4237
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004238 case Intrinsic::eh_return_i32:
4239 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004240 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
4241 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
4242 MVT::Other,
4243 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004244 getValue(I.getArgOperand(0)),
4245 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004246 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004247 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004248 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004249 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004250 case Intrinsic::eh_dwarf_cfa: {
Gabor Greif0635f352010-06-25 09:38:13 +00004251 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004252 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004253 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004254 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004255 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004256 TLI.getPointerTy()),
4257 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004258 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004259 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004260 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00004261 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4262 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004263 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004264 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004265 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004266 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004267 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004268 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004269 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004270
Chris Lattner512063d2010-04-05 06:19:28 +00004271 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004272 return 0;
4273 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004274 case Intrinsic::eh_sjlj_setjmp: {
4275 setValue(&I, DAG.getNode(ISD::EH_SJLJ_SETJMP, dl, MVT::i32, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004276 getValue(I.getArgOperand(0))));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004277 return 0;
4278 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004279 case Intrinsic::eh_sjlj_longjmp: {
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004280 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, dl, MVT::Other,
4281 getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004282 getValue(I.getArgOperand(0))));
Jim Grosbach5eb19512010-05-22 01:06:18 +00004283 return 0;
4284 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004285
Mon P Wang77cdf302008-11-10 20:54:11 +00004286 case Intrinsic::convertff:
4287 case Intrinsic::convertfsi:
4288 case Intrinsic::convertfui:
4289 case Intrinsic::convertsif:
4290 case Intrinsic::convertuif:
4291 case Intrinsic::convertss:
4292 case Intrinsic::convertsu:
4293 case Intrinsic::convertus:
4294 case Intrinsic::convertuu: {
4295 ISD::CvtCode Code = ISD::CVT_INVALID;
4296 switch (Intrinsic) {
4297 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4298 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4299 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4300 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4301 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4302 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4303 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4304 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4305 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4306 }
Owen Andersone50ed302009-08-10 22:56:29 +00004307 EVT DestVT = TLI.getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004308 const Value *Op1 = I.getArgOperand(0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004309 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4310 DAG.getValueType(DestVT),
4311 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004312 getValue(I.getArgOperand(1)),
4313 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004314 Code);
4315 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004316 return 0;
4317 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004318 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004319 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004320 getValue(I.getArgOperand(0)).getValueType(),
4321 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004322 return 0;
4323 case Intrinsic::powi:
Gabor Greif0635f352010-06-25 09:38:13 +00004324 setValue(&I, ExpandPowI(dl, getValue(I.getArgOperand(0)),
4325 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004326 return 0;
4327 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004328 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004329 getValue(I.getArgOperand(0)).getValueType(),
4330 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004331 return 0;
4332 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004333 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004334 getValue(I.getArgOperand(0)).getValueType(),
4335 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004336 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004337 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004338 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004339 return 0;
4340 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004341 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004342 return 0;
4343 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004344 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004345 return 0;
4346 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004347 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004348 return 0;
4349 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004350 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004351 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004352 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004353 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004354 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004355 case Intrinsic::convert_to_fp16:
4356 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004357 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004358 return 0;
4359 case Intrinsic::convert_from_fp16:
4360 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004361 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004362 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004363 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004364 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004365 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004366 return 0;
4367 }
4368 case Intrinsic::readcyclecounter: {
4369 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004370 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4371 DAG.getVTList(MVT::i64, MVT::Other),
4372 &Op, 1);
4373 setValue(&I, Res);
4374 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004375 return 0;
4376 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004377 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004378 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004379 getValue(I.getArgOperand(0)).getValueType(),
4380 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004381 return 0;
4382 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004383 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004384 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004385 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004386 return 0;
4387 }
4388 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004389 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004390 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004391 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392 return 0;
4393 }
4394 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00004395 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004396 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004397 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004398 return 0;
4399 }
4400 case Intrinsic::stacksave: {
4401 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004402 Res = DAG.getNode(ISD::STACKSAVE, dl,
4403 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4404 setValue(&I, Res);
4405 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406 return 0;
4407 }
4408 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00004409 Res = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004410 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004411 return 0;
4412 }
Bill Wendling57344502008-11-18 11:01:33 +00004413 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004414 // Emit code into the DAG to store the stack guard onto the stack.
4415 MachineFunction &MF = DAG.getMachineFunction();
4416 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004417 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004418
Gabor Greif0635f352010-06-25 09:38:13 +00004419 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
4420 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004421
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004422 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004423 MFI->setStackProtectorIndex(FI);
4424
4425 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4426
4427 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004428 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4429 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004430 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004431 setValue(&I, Res);
4432 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004433 return 0;
4434 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004435 case Intrinsic::objectsize: {
4436 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00004437 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004438
4439 assert(CI && "Non-constant type in __builtin_object_size?");
4440
Gabor Greif0635f352010-06-25 09:38:13 +00004441 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004442 EVT Ty = Arg.getValueType();
4443
Dan Gohmane368b462010-06-18 14:22:04 +00004444 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004445 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004446 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004447 Res = DAG.getConstant(0, Ty);
4448
4449 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004450 return 0;
4451 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004452 case Intrinsic::var_annotation:
4453 // Discard annotate attributes
4454 return 0;
4455
4456 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00004457 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004458
4459 SDValue Ops[6];
4460 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004461 Ops[1] = getValue(I.getArgOperand(0));
4462 Ops[2] = getValue(I.getArgOperand(1));
4463 Ops[3] = getValue(I.getArgOperand(2));
4464 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004465 Ops[5] = DAG.getSrcValue(F);
4466
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004467 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4468 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4469 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004470
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004471 setValue(&I, Res);
4472 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004473 return 0;
4474 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004475 case Intrinsic::gcroot:
4476 if (GFI) {
Gabor Greif0635f352010-06-25 09:38:13 +00004477 const Value *Alloca = I.getArgOperand(0);
4478 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004479
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004480 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4481 GFI->addStackRoot(FI->getIndex(), TypeMap);
4482 }
4483 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004484 case Intrinsic::gcread:
4485 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004486 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004487 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004488 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004489 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004490 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004491 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004492 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004493 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004494 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004495 return implVisitAluOverflow(I, ISD::UADDO);
4496 case Intrinsic::sadd_with_overflow:
4497 return implVisitAluOverflow(I, ISD::SADDO);
4498 case Intrinsic::usub_with_overflow:
4499 return implVisitAluOverflow(I, ISD::USUBO);
4500 case Intrinsic::ssub_with_overflow:
4501 return implVisitAluOverflow(I, ISD::SSUBO);
4502 case Intrinsic::umul_with_overflow:
4503 return implVisitAluOverflow(I, ISD::UMULO);
4504 case Intrinsic::smul_with_overflow:
4505 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004506
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004507 case Intrinsic::prefetch: {
4508 SDValue Ops[4];
4509 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004510 Ops[1] = getValue(I.getArgOperand(0));
4511 Ops[2] = getValue(I.getArgOperand(1));
4512 Ops[3] = getValue(I.getArgOperand(2));
Bill Wendling4533cac2010-01-28 21:51:40 +00004513 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004514 return 0;
4515 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004516
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004517 case Intrinsic::memory_barrier: {
4518 SDValue Ops[6];
4519 Ops[0] = getRoot();
4520 for (int x = 1; x < 6; ++x)
Gabor Greif0635f352010-06-25 09:38:13 +00004521 Ops[x] = getValue(I.getArgOperand(x - 1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004522
Bill Wendling4533cac2010-01-28 21:51:40 +00004523 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004524 return 0;
4525 }
4526 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004527 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004528 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004529 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00004530 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004531 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00004532 getValue(I.getArgOperand(0)),
4533 getValue(I.getArgOperand(1)),
4534 getValue(I.getArgOperand(2)),
4535 I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004536 setValue(&I, L);
4537 DAG.setRoot(L.getValue(1));
4538 return 0;
4539 }
4540 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004541 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004542 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004543 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004544 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004545 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004546 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004547 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004548 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004549 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004550 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004551 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004552 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004553 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004554 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004555 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004556 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004557 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004558 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004559 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004560 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004561 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004562
4563 case Intrinsic::invariant_start:
4564 case Intrinsic::lifetime_start:
4565 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004566 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004567 return 0;
4568 case Intrinsic::invariant_end:
4569 case Intrinsic::lifetime_end:
4570 // Discard region information.
4571 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004572 }
4573}
4574
Dan Gohman46510a72010-04-15 01:51:59 +00004575void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004576 bool isTailCall,
4577 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004578 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4579 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004580 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004581 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004582 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004583
4584 TargetLowering::ArgListTy Args;
4585 TargetLowering::ArgListEntry Entry;
4586 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004587
4588 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00004589 SmallVector<ISD::OutputArg, 4> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004590 SmallVector<uint64_t, 4> Offsets;
Dan Gohman84023e02010-07-10 09:00:22 +00004591 GetReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4592 Outs, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004593
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004594 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Dan Gohman84023e02010-07-10 09:00:22 +00004595 FTy->isVarArg(), Outs, FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004596
4597 SDValue DemoteStackSlot;
4598
4599 if (!CanLowerReturn) {
4600 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4601 FTy->getReturnType());
4602 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4603 FTy->getReturnType());
4604 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004605 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004606 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4607
4608 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4609 Entry.Node = DemoteStackSlot;
4610 Entry.Ty = StackSlotPtrType;
4611 Entry.isSExt = false;
4612 Entry.isZExt = false;
4613 Entry.isInReg = false;
4614 Entry.isSRet = true;
4615 Entry.isNest = false;
4616 Entry.isByVal = false;
4617 Entry.Alignment = Align;
4618 Args.push_back(Entry);
4619 RetTy = Type::getVoidTy(FTy->getContext());
4620 }
4621
Dan Gohman46510a72010-04-15 01:51:59 +00004622 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004623 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624 SDValue ArgNode = getValue(*i);
4625 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4626
4627 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004628 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4629 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4630 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4631 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4632 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4633 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004634 Entry.Alignment = CS.getParamAlignment(attrInd);
4635 Args.push_back(Entry);
4636 }
4637
Chris Lattner512063d2010-04-05 06:19:28 +00004638 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004639 // Insert a label before the invoke call to mark the try range. This can be
4640 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004641 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004642
Jim Grosbachca752c92010-01-28 01:45:32 +00004643 // For SjLj, keep track of which landing pads go with which invokes
4644 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004645 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004646 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004647 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004648 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004649 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004650 }
4651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004652 // Both PendingLoads and PendingExports must be flushed here;
4653 // this call might not return.
4654 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004655 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 }
4657
Dan Gohman98ca4f22009-08-05 01:29:28 +00004658 // Check if target-independent constraints permit a tail call here.
4659 // Target-dependent constraints are checked within TLI.LowerCallTo.
4660 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004661 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004662 isTailCall = false;
4663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004664 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004665 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004666 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004667 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004668 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004669 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004670 isTailCall,
4671 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004672 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004673 assert((isTailCall || Result.second.getNode()) &&
4674 "Non-null chain expected with non-tail call!");
4675 assert((Result.second.getNode() || !Result.first.getNode()) &&
4676 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004677 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004678 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004679 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004680 // The instruction result is the result of loading from the
4681 // hidden sret parameter.
4682 SmallVector<EVT, 1> PVTs;
4683 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4684
4685 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4686 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4687 EVT PtrVT = PVTs[0];
Dan Gohman84023e02010-07-10 09:00:22 +00004688 unsigned NumValues = Outs.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004689 SmallVector<SDValue, 4> Values(NumValues);
4690 SmallVector<SDValue, 4> Chains(NumValues);
4691
4692 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004693 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4694 DemoteStackSlot,
4695 DAG.getConstant(Offsets[i], PtrVT));
Dan Gohman84023e02010-07-10 09:00:22 +00004696 SDValue L = DAG.getLoad(Outs[i].VT, getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004697 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004698 Values[i] = L;
4699 Chains[i] = L.getValue(1);
4700 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004701
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004702 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4703 MVT::Other, &Chains[0], NumValues);
4704 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004705
4706 // Collect the legal value parts into potentially illegal values
4707 // that correspond to the original function's return values.
4708 SmallVector<EVT, 4> RetTys;
4709 RetTy = FTy->getReturnType();
4710 ComputeValueVTs(TLI, RetTy, RetTys);
4711 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4712 SmallVector<SDValue, 4> ReturnValues;
4713 unsigned CurReg = 0;
4714 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4715 EVT VT = RetTys[I];
4716 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4717 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4718
4719 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004720 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004721 RegisterVT, VT, AssertOp);
4722 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004723 CurReg += NumRegs;
4724 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004725
Bill Wendling4533cac2010-01-28 21:51:40 +00004726 setValue(CS.getInstruction(),
4727 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4728 DAG.getVTList(&RetTys[0], RetTys.size()),
4729 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004730
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004731 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004732
4733 // As a special case, a null chain means that a tail call has been emitted and
4734 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004735 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004736 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004737 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004738 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004739
Chris Lattner512063d2010-04-05 06:19:28 +00004740 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004741 // Insert a label at the end of the invoke call to mark the try range. This
4742 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004743 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004744 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004745
4746 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004747 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004748 }
4749}
4750
Chris Lattner8047d9a2009-12-24 00:37:38 +00004751/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4752/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004753static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4754 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004755 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004756 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004757 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004758 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004759 if (C->isNullValue())
4760 continue;
4761 // Unknown instruction.
4762 return false;
4763 }
4764 return true;
4765}
4766
Dan Gohman46510a72010-04-15 01:51:59 +00004767static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4768 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004769 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004770
Chris Lattner8047d9a2009-12-24 00:37:38 +00004771 // Check to see if this load can be trivially constant folded, e.g. if the
4772 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004773 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004774 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004775 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004776 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004777
Dan Gohman46510a72010-04-15 01:51:59 +00004778 if (const Constant *LoadCst =
4779 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4780 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004781 return Builder.getValue(LoadCst);
4782 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004783
Chris Lattner8047d9a2009-12-24 00:37:38 +00004784 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4785 // still constant memory, the input chain can be the entry node.
4786 SDValue Root;
4787 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004788
Chris Lattner8047d9a2009-12-24 00:37:38 +00004789 // Do not serialize (non-volatile) loads of constant memory with anything.
4790 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4791 Root = Builder.DAG.getEntryNode();
4792 ConstantMemory = true;
4793 } else {
4794 // Do not serialize non-volatile loads against each other.
4795 Root = Builder.DAG.getRoot();
4796 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004797
Chris Lattner8047d9a2009-12-24 00:37:38 +00004798 SDValue Ptr = Builder.getValue(PtrVal);
4799 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4800 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004801 false /*volatile*/,
4802 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004803
Chris Lattner8047d9a2009-12-24 00:37:38 +00004804 if (!ConstantMemory)
4805 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4806 return LoadVal;
4807}
4808
4809
4810/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4811/// If so, return true and lower it, otherwise return false and it will be
4812/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004813bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004814 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00004815 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00004816 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004817
Gabor Greif0635f352010-06-25 09:38:13 +00004818 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00004819 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00004820 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004821 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004822 return false;
4823
Gabor Greif0635f352010-06-25 09:38:13 +00004824 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004825
Chris Lattner8047d9a2009-12-24 00:37:38 +00004826 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4827 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004828 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4829 bool ActuallyDoIt = true;
4830 MVT LoadVT;
4831 const Type *LoadTy;
4832 switch (Size->getZExtValue()) {
4833 default:
4834 LoadVT = MVT::Other;
4835 LoadTy = 0;
4836 ActuallyDoIt = false;
4837 break;
4838 case 2:
4839 LoadVT = MVT::i16;
4840 LoadTy = Type::getInt16Ty(Size->getContext());
4841 break;
4842 case 4:
4843 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004844 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004845 break;
4846 case 8:
4847 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004848 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004849 break;
4850 /*
4851 case 16:
4852 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004853 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004854 LoadTy = VectorType::get(LoadTy, 4);
4855 break;
4856 */
4857 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004858
Chris Lattner04b091a2009-12-24 01:07:17 +00004859 // This turns into unaligned loads. We only do this if the target natively
4860 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4861 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004862
Chris Lattner04b091a2009-12-24 01:07:17 +00004863 // Require that we can find a legal MVT, and only do this if the target
4864 // supports unaligned loads of that type. Expanding into byte loads would
4865 // bloat the code.
4866 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4867 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4868 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4869 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4870 ActuallyDoIt = false;
4871 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004872
Chris Lattner04b091a2009-12-24 01:07:17 +00004873 if (ActuallyDoIt) {
4874 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4875 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004876
Chris Lattner04b091a2009-12-24 01:07:17 +00004877 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4878 ISD::SETNE);
4879 EVT CallVT = TLI.getValueType(I.getType(), true);
4880 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4881 return true;
4882 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004883 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004884
4885
Chris Lattner8047d9a2009-12-24 00:37:38 +00004886 return false;
4887}
4888
4889
Dan Gohman46510a72010-04-15 01:51:59 +00004890void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00004891 // Handle inline assembly differently.
4892 if (isa<InlineAsm>(I.getCalledValue())) {
4893 visitInlineAsm(&I);
4894 return;
4895 }
4896
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004897 const char *RenameFn = 0;
4898 if (Function *F = I.getCalledFunction()) {
4899 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00004900 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004901 if (unsigned IID = II->getIntrinsicID(F)) {
4902 RenameFn = visitIntrinsicCall(I, IID);
4903 if (!RenameFn)
4904 return;
4905 }
4906 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004907 if (unsigned IID = F->getIntrinsicID()) {
4908 RenameFn = visitIntrinsicCall(I, IID);
4909 if (!RenameFn)
4910 return;
4911 }
4912 }
4913
4914 // Check for well-known libc/libm calls. If the function is internal, it
4915 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004916 if (!F->hasLocalLinkage() && F->hasName()) {
4917 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004918 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004919 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004920 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4921 I.getType() == I.getArgOperand(0)->getType() &&
4922 I.getType() == I.getArgOperand(1)->getType()) {
4923 SDValue LHS = getValue(I.getArgOperand(0));
4924 SDValue RHS = getValue(I.getArgOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004925 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4926 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004927 return;
4928 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004929 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004930 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004931 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4932 I.getType() == I.getArgOperand(0)->getType()) {
4933 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004934 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4935 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004936 return;
4937 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004938 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004939 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004940 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4941 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004942 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004943 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004944 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4945 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004946 return;
4947 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004948 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004949 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004950 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4951 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004952 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004953 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004954 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4955 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004956 return;
4957 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004958 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004959 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004960 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4961 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004962 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004963 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004964 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4965 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004966 return;
4967 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004968 } else if (Name == "memcmp") {
4969 if (visitMemCmpCall(I))
4970 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004971 }
4972 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004973 }
Chris Lattner598751e2010-07-05 05:36:21 +00004974
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004975 SDValue Callee;
4976 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00004977 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004978 else
Bill Wendling056292f2008-09-16 21:48:12 +00004979 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004980
Bill Wendling0d580132009-12-23 01:28:19 +00004981 // Check if we can potentially perform a tail call. More detailed checking is
4982 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004983 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004984}
4985
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004986namespace llvm {
Dan Gohman462f6b52010-05-29 17:53:24 +00004987
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004988/// AsmOperandInfo - This contains information for each constraint that we are
4989/// lowering.
Duncan Sands16d8f8b2010-05-11 20:16:09 +00004990class LLVM_LIBRARY_VISIBILITY SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004991 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004992public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004993 /// CallOperand - If this is the result output operand or a clobber
4994 /// this is null, otherwise it is the incoming operand to the CallInst.
4995 /// This gets modified as the asm is processed.
4996 SDValue CallOperand;
4997
4998 /// AssignedRegs - If this is a register or register class operand, this
4999 /// contains the set of register corresponding to the operand.
5000 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005001
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005002 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5003 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5004 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005005
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005006 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5007 /// busy in OutputRegs/InputRegs.
5008 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005009 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005010 std::set<unsigned> &InputRegs,
5011 const TargetRegisterInfo &TRI) const {
5012 if (isOutReg) {
5013 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5014 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5015 }
5016 if (isInReg) {
5017 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5018 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5019 }
5020 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005021
Owen Andersone50ed302009-08-10 22:56:29 +00005022 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005023 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005024 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005025 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005026 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005027 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005028 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005029
Chris Lattner81249c92008-10-17 17:05:25 +00005030 if (isa<BasicBlock>(CallOperandVal))
5031 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005032
Chris Lattner81249c92008-10-17 17:05:25 +00005033 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005034
Chris Lattner81249c92008-10-17 17:05:25 +00005035 // If this is an indirect operand, the operand is a pointer to the
5036 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005037 if (isIndirect) {
5038 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5039 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005040 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005041 OpTy = PtrTy->getElementType();
5042 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005043
Chris Lattner81249c92008-10-17 17:05:25 +00005044 // If OpTy is not a single value, it may be a struct/union that we
5045 // can tile with integers.
5046 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5047 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5048 switch (BitSize) {
5049 default: break;
5050 case 1:
5051 case 8:
5052 case 16:
5053 case 32:
5054 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005055 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005056 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005057 break;
5058 }
5059 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005060
Chris Lattner81249c92008-10-17 17:05:25 +00005061 return TLI.getValueType(OpTy, true);
5062 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005063
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005064private:
5065 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5066 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005067 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005068 const TargetRegisterInfo &TRI) {
5069 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5070 Regs.insert(Reg);
5071 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5072 for (; *Aliases; ++Aliases)
5073 Regs.insert(*Aliases);
5074 }
5075};
Dan Gohman462f6b52010-05-29 17:53:24 +00005076
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005077} // end llvm namespace.
5078
Dan Gohman462f6b52010-05-29 17:53:24 +00005079/// isAllocatableRegister - If the specified register is safe to allocate,
5080/// i.e. it isn't a stack pointer or some other special register, return the
5081/// register class for the register. Otherwise, return null.
5082static const TargetRegisterClass *
5083isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5084 const TargetLowering &TLI,
5085 const TargetRegisterInfo *TRI) {
5086 EVT FoundVT = MVT::Other;
5087 const TargetRegisterClass *FoundRC = 0;
5088 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5089 E = TRI->regclass_end(); RCI != E; ++RCI) {
5090 EVT ThisVT = MVT::Other;
5091
5092 const TargetRegisterClass *RC = *RCI;
5093 // If none of the value types for this register class are valid, we
5094 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5095 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5096 I != E; ++I) {
5097 if (TLI.isTypeLegal(*I)) {
5098 // If we have already found this register in a different register class,
5099 // choose the one with the largest VT specified. For example, on
5100 // PowerPC, we favor f64 register classes over f32.
5101 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
5102 ThisVT = *I;
5103 break;
5104 }
5105 }
5106 }
5107
5108 if (ThisVT == MVT::Other) continue;
5109
5110 // NOTE: This isn't ideal. In particular, this might allocate the
5111 // frame pointer in functions that need it (due to them not being taken
5112 // out of allocation, because a variable sized allocation hasn't been seen
5113 // yet). This is a slight code pessimization, but should still work.
5114 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5115 E = RC->allocation_order_end(MF); I != E; ++I)
5116 if (*I == Reg) {
5117 // We found a matching register class. Keep looking at others in case
5118 // we find one with larger registers that this physreg is also in.
5119 FoundRC = RC;
5120 FoundVT = ThisVT;
5121 break;
5122 }
5123 }
5124 return FoundRC;
5125}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005126
5127/// GetRegistersForValue - Assign registers (virtual or physical) for the
5128/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005129/// register allocator to handle the assignment process. However, if the asm
5130/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005131/// allocation. This produces generally horrible, but correct, code.
5132///
5133/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005134/// Input and OutputRegs are the set of already allocated physical registers.
5135///
Dan Gohman2048b852009-11-23 18:04:58 +00005136void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005137GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005138 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005139 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005140 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005141
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005142 // Compute whether this value requires an input register, an output register,
5143 // or both.
5144 bool isOutReg = false;
5145 bool isInReg = false;
5146 switch (OpInfo.Type) {
5147 case InlineAsm::isOutput:
5148 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005149
5150 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005151 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005152 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005153 break;
5154 case InlineAsm::isInput:
5155 isInReg = true;
5156 isOutReg = false;
5157 break;
5158 case InlineAsm::isClobber:
5159 isOutReg = true;
5160 isInReg = true;
5161 break;
5162 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005163
5164
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005165 MachineFunction &MF = DAG.getMachineFunction();
5166 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005167
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005168 // If this is a constraint for a single physreg, or a constraint for a
5169 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005170 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005171 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5172 OpInfo.ConstraintVT);
5173
5174 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005175 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005176 // If this is a FP input in an integer register (or visa versa) insert a bit
5177 // cast of the input value. More generally, handle any case where the input
5178 // value disagrees with the register class we plan to stick this in.
5179 if (OpInfo.Type == InlineAsm::isInput &&
5180 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005181 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005182 // types are identical size, use a bitcast to convert (e.g. two differing
5183 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005184 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005185 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005186 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005187 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005188 OpInfo.ConstraintVT = RegVT;
5189 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5190 // If the input is a FP value and we want it in FP registers, do a
5191 // bitcast to the corresponding integer type. This turns an f64 value
5192 // into i64, which can be passed with two i32 values on a 32-bit
5193 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005194 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005195 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005196 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005197 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005198 OpInfo.ConstraintVT = RegVT;
5199 }
5200 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005201
Owen Anderson23b9b192009-08-12 00:36:31 +00005202 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005203 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005204
Owen Andersone50ed302009-08-10 22:56:29 +00005205 EVT RegVT;
5206 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005207
5208 // If this is a constraint for a specific physical register, like {r17},
5209 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005210 if (unsigned AssignedReg = PhysReg.first) {
5211 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005212 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005213 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005214
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005215 // Get the actual register value type. This is important, because the user
5216 // may have asked for (e.g.) the AX register in i32 type. We need to
5217 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005218 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005221 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005222
5223 // If this is an expanded reference, add the rest of the regs to Regs.
5224 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005225 TargetRegisterClass::iterator I = RC->begin();
5226 for (; *I != AssignedReg; ++I)
5227 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005228
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005229 // Already added the first reg.
5230 --NumRegs; ++I;
5231 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005232 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005233 Regs.push_back(*I);
5234 }
5235 }
Bill Wendling651ad132009-12-22 01:25:10 +00005236
Dan Gohman7451d3e2010-05-29 17:03:36 +00005237 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005238 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5239 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5240 return;
5241 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005242
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005243 // Otherwise, if this was a reference to an LLVM register class, create vregs
5244 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005245 if (const TargetRegisterClass *RC = PhysReg.second) {
5246 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005247 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005248 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005249
Evan Chengfb112882009-03-23 08:01:15 +00005250 // Create the appropriate number of virtual registers.
5251 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5252 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005253 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005254
Dan Gohman7451d3e2010-05-29 17:03:36 +00005255 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005256 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005257 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005258
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005259 // This is a reference to a register class that doesn't directly correspond
5260 // to an LLVM register class. Allocate NumRegs consecutive, available,
5261 // registers from the class.
5262 std::vector<unsigned> RegClassRegs
5263 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5264 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005266 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5267 unsigned NumAllocated = 0;
5268 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5269 unsigned Reg = RegClassRegs[i];
5270 // See if this register is available.
5271 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5272 (isInReg && InputRegs.count(Reg))) { // Already used.
5273 // Make sure we find consecutive registers.
5274 NumAllocated = 0;
5275 continue;
5276 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005278 // Check to see if this register is allocatable (i.e. don't give out the
5279 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005280 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5281 if (!RC) { // Couldn't allocate this register.
5282 // Reset NumAllocated to make sure we return consecutive registers.
5283 NumAllocated = 0;
5284 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005285 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005286
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005287 // Okay, this register is good, we can use it.
5288 ++NumAllocated;
5289
5290 // If we allocated enough consecutive registers, succeed.
5291 if (NumAllocated == NumRegs) {
5292 unsigned RegStart = (i-NumAllocated)+1;
5293 unsigned RegEnd = i+1;
5294 // Mark all of the allocated registers used.
5295 for (unsigned i = RegStart; i != RegEnd; ++i)
5296 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005297
Dan Gohman7451d3e2010-05-29 17:03:36 +00005298 OpInfo.AssignedRegs = RegsForValue(Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005299 OpInfo.ConstraintVT);
5300 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5301 return;
5302 }
5303 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005304
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 // Otherwise, we couldn't allocate enough registers for this.
5306}
5307
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005308/// visitInlineAsm - Handle a call to an InlineAsm object.
5309///
Dan Gohman46510a72010-04-15 01:51:59 +00005310void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5311 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312
5313 /// ConstraintOperands - Information about all of the constraints.
5314 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005315
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005316 std::set<unsigned> OutputRegs, InputRegs;
5317
5318 // Do a prepass over the constraints, canonicalizing them, and building up the
5319 // ConstraintOperands list.
5320 std::vector<InlineAsm::ConstraintInfo>
5321 ConstraintInfos = IA->ParseConstraints();
5322
Evan Chengda43bcf2008-09-24 00:05:32 +00005323 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005324
Chris Lattner6c147292009-04-30 00:48:50 +00005325 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005326
Chris Lattner6c147292009-04-30 00:48:50 +00005327 // We won't need to flush pending loads if this asm doesn't touch
5328 // memory and is nonvolatile.
5329 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005330 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005331 else
5332 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005334 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5335 unsigned ResNo = 0; // ResNo - The result number of the next output.
5336 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5337 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5338 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005339
Owen Anderson825b72b2009-08-11 20:47:22 +00005340 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005341
5342 // Compute the value type for each operand.
5343 switch (OpInfo.Type) {
5344 case InlineAsm::isOutput:
5345 // Indirect outputs just consume an argument.
5346 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005347 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005348 break;
5349 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005350
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005351 // The return value of the call is this value. As such, there is no
5352 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005353 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005354 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005355 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5356 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5357 } else {
5358 assert(ResNo == 0 && "Asm only has one result!");
5359 OpVT = TLI.getValueType(CS.getType());
5360 }
5361 ++ResNo;
5362 break;
5363 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005364 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005365 break;
5366 case InlineAsm::isClobber:
5367 // Nothing to do.
5368 break;
5369 }
5370
5371 // If this is an input or an indirect output, process the call argument.
5372 // BasicBlocks are labels, currently appearing only in asm's.
5373 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005374 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005375 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5376
Dan Gohman46510a72010-04-15 01:51:59 +00005377 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005378 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005379 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005380 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005381 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005382
Owen Anderson1d0be152009-08-13 21:58:54 +00005383 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005384 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005385
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005386 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005387 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005388
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005389 // Second pass over the constraints: compute which constraint option to use
5390 // and assign registers to constraints that want a specific physreg.
5391 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5392 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005393
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005394 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005395 // matching input. If their types mismatch, e.g. one is an integer, the
5396 // other is floating point, or their sizes are different, flag it as an
5397 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005398 if (OpInfo.hasMatchingInput()) {
5399 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005400
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005401 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005402 if ((OpInfo.ConstraintVT.isInteger() !=
5403 Input.ConstraintVT.isInteger()) ||
5404 (OpInfo.ConstraintVT.getSizeInBits() !=
5405 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005406 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005407 " with a matching output constraint of"
5408 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005409 }
5410 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005411 }
5412 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005413
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005414 // Compute the constraint code and ConstraintType to use.
Dale Johannesen1784d162010-06-25 21:55:36 +00005415 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005416
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417 // If this is a memory input, and if the operand is not indirect, do what we
5418 // need to to provide an address for the memory input.
5419 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5420 !OpInfo.isIndirect) {
5421 assert(OpInfo.Type == InlineAsm::isInput &&
5422 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005423
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005424 // Memory operands really want the address of the value. If we don't have
5425 // an indirect input, put it in the constpool if we can, otherwise spill
5426 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005427
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005428 // If the operand is a float, integer, or vector constant, spill to a
5429 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005430 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005431 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5432 isa<ConstantVector>(OpVal)) {
5433 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5434 TLI.getPointerTy());
5435 } else {
5436 // Otherwise, create a stack slot and emit a store to it before the
5437 // asm.
5438 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005439 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005440 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5441 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005442 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005443 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005444 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005445 OpInfo.CallOperand, StackSlot, NULL, 0,
5446 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005447 OpInfo.CallOperand = StackSlot;
5448 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005449
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005450 // There is no longer a Value* corresponding to this operand.
5451 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005452
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005453 // It is now an indirect operand.
5454 OpInfo.isIndirect = true;
5455 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005457 // If this constraint is for a specific register, allocate it before
5458 // anything else.
5459 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005460 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005461 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005462
Bill Wendling651ad132009-12-22 01:25:10 +00005463 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005464
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005465 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005466 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005467 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5468 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005469
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005470 // C_Register operands have already been allocated, Other/Memory don't need
5471 // to be.
5472 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005473 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005474 }
5475
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005476 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5477 std::vector<SDValue> AsmNodeOperands;
5478 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5479 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005480 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5481 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005482
Chris Lattnerdecc2672010-04-07 05:20:54 +00005483 // If we have a !srcloc metadata node associated with it, we want to attach
5484 // this to the ultimately generated inline asm machineinstr. To do this, we
5485 // pass in the third operand as this (potentially null) inline asm MDNode.
5486 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5487 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005488
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005489 // Remember the AlignStack bit as operand 3.
5490 AsmNodeOperands.push_back(DAG.getTargetConstant(IA->isAlignStack() ? 1 : 0,
5491 MVT::i1));
5492
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005493 // Loop over all of the inputs, copying the operand values into the
5494 // appropriate registers and processing the output regs.
5495 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005496
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005497 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5498 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005499
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005500 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5501 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5502
5503 switch (OpInfo.Type) {
5504 case InlineAsm::isOutput: {
5505 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5506 OpInfo.ConstraintType != TargetLowering::C_Register) {
5507 // Memory output, or 'other' output (e.g. 'X' constraint).
5508 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5509
5510 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005511 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5512 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005513 TLI.getPointerTy()));
5514 AsmNodeOperands.push_back(OpInfo.CallOperand);
5515 break;
5516 }
5517
5518 // Otherwise, this is a register or register class output.
5519
5520 // Copy the output from the appropriate register. Find a register that
5521 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005522 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005523 report_fatal_error("Couldn't allocate output reg for constraint '" +
5524 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005525
5526 // If this is an indirect operand, store through the pointer after the
5527 // asm.
5528 if (OpInfo.isIndirect) {
5529 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5530 OpInfo.CallOperandVal));
5531 } else {
5532 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005533 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005534 // Concatenate this output onto the outputs list.
5535 RetValRegs.append(OpInfo.AssignedRegs);
5536 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005537
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005538 // Add information to the INLINEASM node to know that this register is
5539 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005540 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005541 InlineAsm::Kind_RegDefEarlyClobber :
5542 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005543 false,
5544 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005545 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005546 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005547 break;
5548 }
5549 case InlineAsm::isInput: {
5550 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005551
Chris Lattner6bdcda32008-10-17 16:47:46 +00005552 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005553 // If this is required to match an output register we have already set,
5554 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005555 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005556
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005557 // Scan until we find the definition we already emitted of this operand.
5558 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005559 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005560 for (; OperandNo; --OperandNo) {
5561 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005562 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005563 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005564 assert((InlineAsm::isRegDefKind(OpFlag) ||
5565 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5566 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005567 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005568 }
5569
Evan Cheng697cbbf2009-03-20 18:03:34 +00005570 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005571 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005572 if (InlineAsm::isRegDefKind(OpFlag) ||
5573 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005574 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005575 if (OpInfo.isIndirect) {
5576 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005577 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005578 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5579 " don't know how to handle tied "
5580 "indirect register inputs");
5581 }
5582
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005583 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005584 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005585 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005586 MatchedRegs.RegVTs.push_back(RegVT);
5587 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005588 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005589 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005590 MatchedRegs.Regs.push_back
5591 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005592
5593 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005594 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005595 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005596 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005597 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005598 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005599 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005600 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005601
5602 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5603 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5604 "Unexpected number of operands");
5605 // Add information to the INLINEASM node to know about this input.
5606 // See InlineAsm.h isUseOperandTiedToDef.
5607 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5608 OpInfo.getMatchedOperand());
5609 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5610 TLI.getPointerTy()));
5611 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5612 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005613 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005614
Dale Johannesenb5611a62010-07-13 20:17:05 +00005615 // Treat indirect 'X' constraint as memory.
5616 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
5617 OpInfo.isIndirect)
5618 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005619
Dale Johannesenb5611a62010-07-13 20:17:05 +00005620 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005621 std::vector<SDValue> Ops;
5622 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Dale Johannesen1784d162010-06-25 21:55:36 +00005623 Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005624 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005625 report_fatal_error("Invalid operand for inline asm constraint '" +
5626 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005627
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005628 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005629 unsigned ResOpType =
5630 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005631 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005632 TLI.getPointerTy()));
5633 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5634 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005635 }
5636
5637 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005638 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5639 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5640 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005641
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005642 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005643 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005644 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005645 TLI.getPointerTy()));
5646 AsmNodeOperands.push_back(InOperandVal);
5647 break;
5648 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005649
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005650 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5651 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5652 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005653 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 "Don't know how to handle indirect register inputs yet!");
5655
5656 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005657 if (OpInfo.AssignedRegs.Regs.empty() ||
Dan Gohman7451d3e2010-05-29 17:03:36 +00005658 !OpInfo.AssignedRegs.areValueTypesLegal(TLI))
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005659 report_fatal_error("Couldn't allocate input reg for constraint '" +
5660 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005661
Dale Johannesen66978ee2009-01-31 02:22:37 +00005662 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005663 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005664
Chris Lattnerdecc2672010-04-07 05:20:54 +00005665 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005666 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005667 break;
5668 }
5669 case InlineAsm::isClobber: {
5670 // Add the clobbered value to the operand list, so that the register
5671 // allocator is aware that the physreg got clobbered.
5672 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005673 OpInfo.AssignedRegs.AddInlineAsmOperands(
5674 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005675 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005676 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 break;
5678 }
5679 }
5680 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005681
Chris Lattnerdecc2672010-04-07 05:20:54 +00005682 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005683 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005684 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005685
Dale Johannesen66978ee2009-01-31 02:22:37 +00005686 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005687 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005688 &AsmNodeOperands[0], AsmNodeOperands.size());
5689 Flag = Chain.getValue(1);
5690
5691 // If this asm returns a register value, copy the result from that register
5692 // and set it as the value of the call.
5693 if (!RetValRegs.Regs.empty()) {
Dan Gohman7451d3e2010-05-29 17:03:36 +00005694 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005695 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005696
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005697 // FIXME: Why don't we do this for inline asms with MRVs?
5698 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005699 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005700
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005701 // If any of the results of the inline asm is a vector, it may have the
5702 // wrong width/num elts. This can happen for register classes that can
5703 // contain multiple different value types. The preg or vreg allocated may
5704 // not have the same VT as was expected. Convert it to the right type
5705 // with bit_convert.
5706 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005707 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005708 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005709
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005710 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005711 ResultType.isInteger() && Val.getValueType().isInteger()) {
5712 // If a result value was tied to an input value, the computed result may
5713 // have a wider width than the expected result. Extract the relevant
5714 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005715 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005716 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005717
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005718 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005719 }
Dan Gohman95915732008-10-18 01:03:45 +00005720
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005721 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005722 // Don't need to use this as a chain in this case.
5723 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5724 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005725 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005726
Dan Gohman46510a72010-04-15 01:51:59 +00005727 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005728
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005729 // Process indirect outputs, first output all of the flagged copies out of
5730 // physregs.
5731 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5732 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005733 const Value *Ptr = IndirectStoresToEmit[i].second;
Dan Gohman7451d3e2010-05-29 17:03:36 +00005734 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005735 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005736 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5737 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005738
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005739 // Emit the non-flagged stores from the physregs.
5740 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005741 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5742 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5743 StoresToEmit[i].first,
5744 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005745 StoresToEmit[i].second, 0,
5746 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005747 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005748 }
5749
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005750 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005751 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005752 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005753
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005754 DAG.setRoot(Chain);
5755}
5756
Dan Gohman46510a72010-04-15 01:51:59 +00005757void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005758 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5759 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005760 getValue(I.getArgOperand(0)),
5761 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005762}
5763
Dan Gohman46510a72010-04-15 01:51:59 +00005764void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Rafael Espindola9d544d02010-07-12 18:11:17 +00005765 const TargetData &TD = *TLI.getTargetData();
Dale Johannesena04b7572009-02-03 23:04:43 +00005766 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5767 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00005768 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00005769 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005770 setValue(&I, V);
5771 DAG.setRoot(V.getValue(1));
5772}
5773
Dan Gohman46510a72010-04-15 01:51:59 +00005774void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005775 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5776 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005777 getValue(I.getArgOperand(0)),
5778 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005779}
5780
Dan Gohman46510a72010-04-15 01:51:59 +00005781void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005782 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5783 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005784 getValue(I.getArgOperand(0)),
5785 getValue(I.getArgOperand(1)),
5786 DAG.getSrcValue(I.getArgOperand(0)),
5787 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005788}
5789
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005790/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005791/// implementation, which just calls LowerCall.
5792/// FIXME: When all targets are
5793/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005794std::pair<SDValue, SDValue>
5795TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5796 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005797 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005798 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005799 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005800 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005801 ArgListTy &Args, SelectionDAG &DAG,
5802 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005803 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005804 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00005805 SmallVector<SDValue, 32> OutVals;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005806 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005807 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5809 for (unsigned Value = 0, NumValues = ValueVTs.size();
5810 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005811 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005812 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005813 SDValue Op = SDValue(Args[i].Node.getNode(),
5814 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005815 ISD::ArgFlagsTy Flags;
5816 unsigned OriginalAlignment =
5817 getTargetData()->getABITypeAlignment(ArgTy);
5818
5819 if (Args[i].isZExt)
5820 Flags.setZExt();
5821 if (Args[i].isSExt)
5822 Flags.setSExt();
5823 if (Args[i].isInReg)
5824 Flags.setInReg();
5825 if (Args[i].isSRet)
5826 Flags.setSRet();
5827 if (Args[i].isByVal) {
5828 Flags.setByVal();
5829 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5830 const Type *ElementTy = Ty->getElementType();
5831 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005832 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005833 // For ByVal, alignment should come from FE. BE will guess if this
5834 // info is not there but there are cases it cannot get right.
5835 if (Args[i].Alignment)
5836 FrameAlign = Args[i].Alignment;
5837 Flags.setByValAlign(FrameAlign);
5838 Flags.setByValSize(FrameSize);
5839 }
5840 if (Args[i].isNest)
5841 Flags.setNest();
5842 Flags.setOrigAlign(OriginalAlignment);
5843
Owen Anderson23b9b192009-08-12 00:36:31 +00005844 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5845 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005846 SmallVector<SDValue, 4> Parts(NumParts);
5847 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5848
5849 if (Args[i].isSExt)
5850 ExtendKind = ISD::SIGN_EXTEND;
5851 else if (Args[i].isZExt)
5852 ExtendKind = ISD::ZERO_EXTEND;
5853
Bill Wendling46ada192010-03-02 01:55:18 +00005854 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005855 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005856
Dan Gohman98ca4f22009-08-05 01:29:28 +00005857 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005858 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00005859 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
5860 i < NumFixedArgs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005861 if (NumParts > 1 && j == 0)
5862 MyFlags.Flags.setSplit();
5863 else if (j != 0)
5864 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005865
Dan Gohman98ca4f22009-08-05 01:29:28 +00005866 Outs.push_back(MyFlags);
Dan Gohmanc9403652010-07-07 15:54:55 +00005867 OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005868 }
5869 }
5870 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005871
Dan Gohman98ca4f22009-08-05 01:29:28 +00005872 // Handle the incoming return values from the call.
5873 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005874 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005875 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005876 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005877 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005878 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5879 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005880 for (unsigned i = 0; i != NumRegs; ++i) {
5881 ISD::InputArg MyFlags;
5882 MyFlags.VT = RegisterVT;
5883 MyFlags.Used = isReturnValueUsed;
5884 if (RetSExt)
5885 MyFlags.Flags.setSExt();
5886 if (RetZExt)
5887 MyFlags.Flags.setZExt();
5888 if (isInreg)
5889 MyFlags.Flags.setInReg();
5890 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005891 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005892 }
5893
Dan Gohman98ca4f22009-08-05 01:29:28 +00005894 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005895 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +00005896 Outs, OutVals, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005897
5898 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005899 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005900 "LowerCall didn't return a valid chain!");
5901 assert((!isTailCall || InVals.empty()) &&
5902 "LowerCall emitted a return value for a tail call!");
5903 assert((isTailCall || InVals.size() == Ins.size()) &&
5904 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005905
5906 // For a tail call, the return value is merely live-out and there aren't
5907 // any nodes in the DAG representing it. Return a special value to
5908 // indicate that a tail call has been emitted and no more Instructions
5909 // should be processed in the current block.
5910 if (isTailCall) {
5911 DAG.setRoot(Chain);
5912 return std::make_pair(SDValue(), SDValue());
5913 }
5914
Evan Chengaf1871f2010-03-11 19:38:18 +00005915 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5916 assert(InVals[i].getNode() &&
5917 "LowerCall emitted a null value!");
5918 assert(Ins[i].VT == InVals[i].getValueType() &&
5919 "LowerCall emitted a value with the wrong type!");
5920 });
5921
Dan Gohman98ca4f22009-08-05 01:29:28 +00005922 // Collect the legal value parts into potentially illegal values
5923 // that correspond to the original function's return values.
5924 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5925 if (RetSExt)
5926 AssertOp = ISD::AssertSext;
5927 else if (RetZExt)
5928 AssertOp = ISD::AssertZext;
5929 SmallVector<SDValue, 4> ReturnValues;
5930 unsigned CurReg = 0;
5931 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005932 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005933 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5934 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005935
Bill Wendling46ada192010-03-02 01:55:18 +00005936 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005937 NumRegs, RegisterVT, VT,
5938 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005939 CurReg += NumRegs;
5940 }
5941
5942 // For a function returning void, there is no return value. We can't create
5943 // such a node, so we just return a null return value in that case. In
5944 // that case, nothing will actualy look at the value.
5945 if (ReturnValues.empty())
5946 return std::make_pair(SDValue(), Chain);
5947
5948 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5949 DAG.getVTList(&RetTys[0], RetTys.size()),
5950 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005951 return std::make_pair(Res, Chain);
5952}
5953
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005954void TargetLowering::LowerOperationWrapper(SDNode *N,
5955 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005956 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005957 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005958 if (Res.getNode())
5959 Results.push_back(Res);
5960}
5961
Dan Gohmand858e902010-04-17 15:26:15 +00005962SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005963 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005964 return SDValue();
5965}
5966
Dan Gohman46510a72010-04-15 01:51:59 +00005967void
5968SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00005969 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005970 assert((Op.getOpcode() != ISD::CopyFromReg ||
5971 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5972 "Copy from a reg to the same reg!");
5973 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5974
Owen Anderson23b9b192009-08-12 00:36:31 +00005975 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005976 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005977 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005978 PendingExports.push_back(Chain);
5979}
5980
5981#include "llvm/CodeGen/SelectionDAGISel.h"
5982
Dan Gohman46510a72010-04-15 01:51:59 +00005983void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005984 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005985 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005986 SelectionDAG &DAG = SDB->DAG;
Dan Gohman2048b852009-11-23 18:04:58 +00005987 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005988 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005989 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005990
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005991 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005992 SmallVector<ISD::OutputArg, 4> Outs;
5993 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
5994 Outs, TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005995
Dan Gohman7451d3e2010-05-29 17:03:36 +00005996 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005997 // Put in an sret pointer parameter before all the other parameters.
5998 SmallVector<EVT, 1> ValueVTs;
5999 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6000
6001 // NOTE: Assuming that a pointer will never break down to more than one VT
6002 // or one register.
6003 ISD::ArgFlagsTy Flags;
6004 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00006005 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006006 ISD::InputArg RetArg(Flags, RegisterVT, true);
6007 Ins.push_back(RetArg);
6008 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006009
Dan Gohman98ca4f22009-08-05 01:29:28 +00006010 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006011 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006012 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006013 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006014 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006015 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6016 bool isArgValueUsed = !I->use_empty();
6017 for (unsigned Value = 0, NumValues = ValueVTs.size();
6018 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006019 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006020 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006021 ISD::ArgFlagsTy Flags;
6022 unsigned OriginalAlignment =
6023 TD->getABITypeAlignment(ArgTy);
6024
6025 if (F.paramHasAttr(Idx, Attribute::ZExt))
6026 Flags.setZExt();
6027 if (F.paramHasAttr(Idx, Attribute::SExt))
6028 Flags.setSExt();
6029 if (F.paramHasAttr(Idx, Attribute::InReg))
6030 Flags.setInReg();
6031 if (F.paramHasAttr(Idx, Attribute::StructRet))
6032 Flags.setSRet();
6033 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6034 Flags.setByVal();
6035 const PointerType *Ty = cast<PointerType>(I->getType());
6036 const Type *ElementTy = Ty->getElementType();
6037 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6038 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6039 // For ByVal, alignment should be passed from FE. BE will guess if
6040 // this info is not there but there are cases it cannot get right.
6041 if (F.getParamAlignment(Idx))
6042 FrameAlign = F.getParamAlignment(Idx);
6043 Flags.setByValAlign(FrameAlign);
6044 Flags.setByValSize(FrameSize);
6045 }
6046 if (F.paramHasAttr(Idx, Attribute::Nest))
6047 Flags.setNest();
6048 Flags.setOrigAlign(OriginalAlignment);
6049
Owen Anderson23b9b192009-08-12 00:36:31 +00006050 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6051 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006052 for (unsigned i = 0; i != NumRegs; ++i) {
6053 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6054 if (NumRegs > 1 && i == 0)
6055 MyFlags.Flags.setSplit();
6056 // if it isn't first piece, alignment must be 1
6057 else if (i > 0)
6058 MyFlags.Flags.setOrigAlign(1);
6059 Ins.push_back(MyFlags);
6060 }
6061 }
6062 }
6063
6064 // Call the target to set up the argument values.
6065 SmallVector<SDValue, 8> InVals;
6066 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6067 F.isVarArg(), Ins,
6068 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006069
6070 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006071 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006072 "LowerFormalArguments didn't return a valid chain!");
6073 assert(InVals.size() == Ins.size() &&
6074 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006075 DEBUG({
6076 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6077 assert(InVals[i].getNode() &&
6078 "LowerFormalArguments emitted a null value!");
6079 assert(Ins[i].VT == InVals[i].getValueType() &&
6080 "LowerFormalArguments emitted a value with the wrong type!");
6081 }
6082 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006083
Dan Gohman5e866062009-08-06 15:37:27 +00006084 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006085 DAG.setRoot(NewRoot);
6086
6087 // Set up the argument values.
6088 unsigned i = 0;
6089 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006090 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006091 // Create a virtual register for the sret pointer, and put in a copy
6092 // from the sret argument into it.
6093 SmallVector<EVT, 1> ValueVTs;
6094 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6095 EVT VT = ValueVTs[0];
6096 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6097 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006098 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006099 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006100
Dan Gohman2048b852009-11-23 18:04:58 +00006101 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006102 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6103 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006104 FuncInfo->DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006105 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6106 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006107 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006108
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006109 // i indexes lowered arguments. Bump it past the hidden sret argument.
6110 // Idx indexes LLVM arguments. Don't touch it.
6111 ++i;
6112 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006113
Dan Gohman46510a72010-04-15 01:51:59 +00006114 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006115 ++I, ++Idx) {
6116 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006117 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006118 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006119 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006120
6121 // If this argument is unused then remember its value. It is used to generate
6122 // debugging information.
6123 if (I->use_empty() && NumValues)
6124 SDB->setUnusedArgValue(I, InVals[i]);
6125
Dan Gohman98ca4f22009-08-05 01:29:28 +00006126 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006127 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006128 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6129 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006130
6131 if (!I->use_empty()) {
6132 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6133 if (F.paramHasAttr(Idx, Attribute::SExt))
6134 AssertOp = ISD::AssertSext;
6135 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6136 AssertOp = ISD::AssertZext;
6137
Bill Wendling46ada192010-03-02 01:55:18 +00006138 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006139 NumParts, PartVT, VT,
6140 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006141 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006142
Dan Gohman98ca4f22009-08-05 01:29:28 +00006143 i += NumParts;
6144 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006145
Dan Gohman98ca4f22009-08-05 01:29:28 +00006146 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00006147 SDValue Res;
6148 if (!ArgValues.empty())
6149 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6150 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006151 SDB->setValue(I, Res);
6152
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006153 // If this argument is live outside of the entry block, insert a copy from
6154 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006155 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006156 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006157 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006158
Dan Gohman98ca4f22009-08-05 01:29:28 +00006159 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006160
6161 // Finally, if the target has anything special to do, allow it to do so.
6162 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006163 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006164}
6165
6166/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6167/// ensure constants are generated when needed. Remember the virtual registers
6168/// that need to be added to the Machine PHI nodes as input. We cannot just
6169/// directly add them, because expansion might result in multiple MBB's for one
6170/// BB. As such, the start of the BB might correspond to a different MBB than
6171/// the end.
6172///
6173void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006174SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006175 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006176
6177 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6178
6179 // Check successor nodes' PHI nodes that expect a constant to be available
6180 // from this block.
6181 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006182 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006183 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006184 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006186 // If this terminator has multiple identical successors (common for
6187 // switches), only handle each succ once.
6188 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006189
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006190 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006191
6192 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6193 // nodes and Machine PHI nodes, but the incoming operands have not been
6194 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006195 for (BasicBlock::const_iterator I = SuccBB->begin();
6196 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006197 // Ignore dead phi's.
6198 if (PN->use_empty()) continue;
6199
6200 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006201 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006202
Dan Gohman46510a72010-04-15 01:51:59 +00006203 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006204 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006205 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00006206 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006207 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006208 }
6209 Reg = RegOut;
6210 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00006211 DenseMap<const Value *, unsigned>::iterator I =
6212 FuncInfo.ValueMap.find(PHIOp);
6213 if (I != FuncInfo.ValueMap.end())
6214 Reg = I->second;
6215 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006216 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006217 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006218 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00006219 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006220 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006221 }
6222 }
6223
6224 // Remember that this register needs to added to the machine PHI node as
6225 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006226 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006227 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6228 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006229 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006230 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006231 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006232 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006233 Reg += NumRegisters;
6234 }
6235 }
6236 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006237 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006238}