blob: 3aff0ad5195df763268ed3863a93793e2492efc6 [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"
Michael J. Spencer84ac4d52010-10-16 08:25:41 +000018#include "llvm/ADT/PostOrderIterator.h"
Dan Gohman5b229802008-09-04 20:49:27 +000019#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000021#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000022#include "llvm/Constants.h"
23#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Function.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/InlineAsm.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/IntrinsicInst.h"
Chris Lattner6129c372010-04-08 00:09:16 +000031#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000032#include "llvm/Module.h"
Dan Gohman5eb6d652010-04-21 01:22:34 +000033#include "llvm/CodeGen/Analysis.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000034#include "llvm/CodeGen/FastISel.h"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000035#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000036#include "llvm/CodeGen/GCStrategy.h"
37#include "llvm/CodeGen/GCMetadata.h"
38#include "llvm/CodeGen/MachineFunction.h"
39#include "llvm/CodeGen/MachineFrameInfo.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
41#include "llvm/CodeGen/MachineJumpTableInfo.h"
42#include "llvm/CodeGen/MachineModuleInfo.h"
43#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000044#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000045#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000046#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000047#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000048#include "llvm/Target/TargetFrameLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000049#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"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000053#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000054#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000055#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000056#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000057#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000058#include <algorithm>
59using namespace llvm;
60
Dale Johannesen601d3c02008-09-05 01:48:15 +000061/// LimitFloatPrecision - Generate low-precision inline sequences for
62/// some float libcalls (6, 8 or 12 bits).
63static unsigned LimitFloatPrecision;
64
65static cl::opt<unsigned, true>
66LimitFPPrecision("limit-float-precision",
67 cl::desc("Generate low-precision inline sequences "
68 "for some float libcalls"),
69 cl::location(LimitFloatPrecision),
70 cl::init(0));
71
Andrew Trickde91f3c2010-11-12 17:50:46 +000072// Limit the width of DAG chains. This is important in general to prevent
73// prevent DAG-based analysis from blowing up. For example, alias analysis and
74// load clustering may not complete in reasonable time. It is difficult to
75// recognize and avoid this situation within each individual analysis, and
76// future analyses are likely to have the same behavior. Limiting DAG width is
Andrew Trickb9e6fe12010-11-20 07:26:51 +000077// the safe approach, and will be especially important with global DAGs.
Andrew Trickde91f3c2010-11-12 17:50:46 +000078//
79// MaxParallelChains default is arbitrarily high to avoid affecting
80// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
Andrew Trickb9e6fe12010-11-20 07:26:51 +000081// sequence over this should have been converted to llvm.memcpy by the
82// frontend. It easy to induce this behavior with .ll code such as:
83// %buffer = alloca [4096 x i8]
84// %data = load [4096 x i8]* %argPtr
85// store [4096 x i8] %data, [4096 x i8]* %buffer
Andrew Trick778583a2011-03-11 17:46:59 +000086static const unsigned MaxParallelChains = 64;
Andrew Trickde91f3c2010-11-12 17:50:46 +000087
Chris Lattner3ac18842010-08-24 23:20:40 +000088static SDValue getCopyFromPartsVector(SelectionDAG &DAG, DebugLoc DL,
89 const SDValue *Parts, unsigned NumParts,
90 EVT PartVT, EVT ValueVT);
Michael J. Spencere70c5262010-10-16 08:25:21 +000091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000092/// getCopyFromParts - Create a value that contains the specified legal parts
93/// combined into the value they represent. If the parts combine to a type
94/// larger then ValueVT then AssertOp can be used to specify whether the extra
95/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
96/// (ISD::AssertSext).
Chris Lattner3ac18842010-08-24 23:20:40 +000097static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc DL,
Dale Johannesen66978ee2009-01-31 02:22:37 +000098 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +000099 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000100 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000101 if (ValueVT.isVector())
102 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000104 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000105 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000106 SDValue Val = Parts[0];
107
108 if (NumParts > 1) {
109 // Assemble the value from multiple parts.
Chris Lattner3ac18842010-08-24 23:20:40 +0000110 if (ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 unsigned PartBits = PartVT.getSizeInBits();
112 unsigned ValueBits = ValueVT.getSizeInBits();
113
114 // Assemble the power of 2 part.
115 unsigned RoundParts = NumParts & (NumParts - 1) ?
116 1 << Log2_32(NumParts) : NumParts;
117 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000118 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000119 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000120 SDValue Lo, Hi;
121
Owen Anderson23b9b192009-08-12 00:36:31 +0000122 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000124 if (RoundParts > 2) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000125 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000126 PartVT, HalfVT);
Chris Lattner3ac18842010-08-24 23:20:40 +0000127 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000128 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000129 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000130 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
131 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000132 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 if (TLI.isBigEndian())
135 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000136
Chris Lattner3ac18842010-08-24 23:20:40 +0000137 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000138
139 if (RoundParts < NumParts) {
140 // Assemble the trailing non-power-of-2 part.
141 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000142 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000143 Hi = getCopyFromParts(DAG, DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000144 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000145
146 // Combine the round and odd parts.
147 Lo = Val;
148 if (TLI.isBigEndian())
149 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000150 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000151 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
152 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000153 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000154 TLI.getPointerTy()));
Chris Lattner3ac18842010-08-24 23:20:40 +0000155 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
156 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000158 } else if (PartVT.isFloatingPoint()) {
159 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000161 "Unexpected split");
162 SDValue Lo, Hi;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000163 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
164 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000165 if (TLI.isBigEndian())
166 std::swap(Lo, Hi);
Chris Lattner3ac18842010-08-24 23:20:40 +0000167 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000168 } else {
169 // FP split into integer parts (soft fp)
170 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
171 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000172 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Chris Lattner3ac18842010-08-24 23:20:40 +0000173 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000174 }
175 }
176
177 // There is now one part, held in Val. Correct it to match ValueVT.
178 PartVT = Val.getValueType();
179
180 if (PartVT == ValueVT)
181 return Val;
182
Chris Lattner3ac18842010-08-24 23:20:40 +0000183 if (PartVT.isInteger() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000184 if (ValueVT.bitsLT(PartVT)) {
185 // For a truncate, see if we have any information to
186 // indicate whether the truncated bits will always be
187 // zero or sign-extension.
188 if (AssertOp != ISD::DELETED_NODE)
Chris Lattner3ac18842010-08-24 23:20:40 +0000189 Val = DAG.getNode(AssertOp, DL, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000190 DAG.getValueType(ValueVT));
Chris Lattner3ac18842010-08-24 23:20:40 +0000191 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000192 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000193 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000194 }
195
196 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000197 // FP_ROUND's are always exact here.
198 if (ValueVT.bitsLT(Val.getValueType()))
199 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val,
Bill Wendling4533cac2010-01-28 21:51:40 +0000200 DAG.getIntPtrConstant(1));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000201
Chris Lattner3ac18842010-08-24 23:20:40 +0000202 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000203 }
204
Bill Wendling4533cac2010-01-28 21:51:40 +0000205 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000206 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000207
Torok Edwinc23197a2009-07-14 16:55:14 +0000208 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000209 return SDValue();
210}
211
Chris Lattner3ac18842010-08-24 23:20:40 +0000212/// getCopyFromParts - Create a value that contains the specified legal parts
213/// combined into the value they represent. If the parts combine to a type
214/// larger then ValueVT then AssertOp can be used to specify whether the extra
215/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
216/// (ISD::AssertSext).
217static SDValue getCopyFromPartsVector(SelectionDAG &DAG, DebugLoc DL,
218 const SDValue *Parts, unsigned NumParts,
219 EVT PartVT, EVT ValueVT) {
220 assert(ValueVT.isVector() && "Not a vector value");
221 assert(NumParts > 0 && "No parts to assemble!");
222 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
223 SDValue Val = Parts[0];
Michael J. Spencere70c5262010-10-16 08:25:21 +0000224
Chris Lattner3ac18842010-08-24 23:20:40 +0000225 // Handle a multi-element vector.
226 if (NumParts > 1) {
227 EVT IntermediateVT, RegisterVT;
228 unsigned NumIntermediates;
229 unsigned NumRegs =
230 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
231 NumIntermediates, RegisterVT);
232 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
233 NumParts = NumRegs; // Silence a compiler warning.
234 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
235 assert(RegisterVT == Parts[0].getValueType() &&
236 "Part type doesn't match part!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000237
Chris Lattner3ac18842010-08-24 23:20:40 +0000238 // Assemble the parts into intermediate operands.
239 SmallVector<SDValue, 8> Ops(NumIntermediates);
240 if (NumIntermediates == NumParts) {
241 // If the register was not expanded, truncate or copy the value,
242 // as appropriate.
243 for (unsigned i = 0; i != NumParts; ++i)
244 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
245 PartVT, IntermediateVT);
246 } else if (NumParts > 0) {
247 // If the intermediate type was expanded, build the intermediate
248 // operands from the parts.
249 assert(NumParts % NumIntermediates == 0 &&
250 "Must expand into a divisible number of parts!");
251 unsigned Factor = NumParts / NumIntermediates;
252 for (unsigned i = 0; i != NumIntermediates; ++i)
253 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
254 PartVT, IntermediateVT);
255 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000256
Chris Lattner3ac18842010-08-24 23:20:40 +0000257 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
258 // intermediate operands.
259 Val = DAG.getNode(IntermediateVT.isVector() ?
260 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL,
261 ValueVT, &Ops[0], NumIntermediates);
262 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000263
Chris Lattner3ac18842010-08-24 23:20:40 +0000264 // There is now one part, held in Val. Correct it to match ValueVT.
265 PartVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000266
Chris Lattner3ac18842010-08-24 23:20:40 +0000267 if (PartVT == ValueVT)
268 return Val;
Michael J. Spencere70c5262010-10-16 08:25:21 +0000269
Chris Lattnere6f7c262010-08-25 22:49:25 +0000270 if (PartVT.isVector()) {
271 // If the element type of the source/dest vectors are the same, but the
272 // parts vector has more elements than the value vector, then we have a
273 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
274 // elements we want.
275 if (PartVT.getVectorElementType() == ValueVT.getVectorElementType()) {
276 assert(PartVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
277 "Cannot narrow, it would be a lossy transformation");
278 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
279 DAG.getIntPtrConstant(0));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000280 }
281
Chris Lattnere6f7c262010-08-25 22:49:25 +0000282 // Vector/Vector bitcast.
Nadav Rotem0b666362011-06-04 20:58:08 +0000283 if (ValueVT.getSizeInBits() == PartVT.getSizeInBits())
284 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
285
286 assert(PartVT.getVectorNumElements() == ValueVT.getVectorNumElements() &&
287 "Cannot handle this kind of promotion");
288 // Promoted vector extract
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000289 bool Smaller = ValueVT.bitsLE(PartVT);
290 return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
291 DL, ValueVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000292
Chris Lattnere6f7c262010-08-25 22:49:25 +0000293 }
Eric Christopher471e4222011-06-08 23:55:35 +0000294
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000295 // Trivial bitcast if the types are the same size and the destination
296 // vector type is legal.
297 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits() &&
298 TLI.isTypeLegal(ValueVT))
299 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000300
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000301 // Handle cases such as i8 -> <1 x i1>
302 assert(ValueVT.getVectorNumElements() == 1 &&
Chris Lattner3ac18842010-08-24 23:20:40 +0000303 "Only trivial scalar-to-vector conversions should get here!");
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000304
305 if (ValueVT.getVectorNumElements() == 1 &&
306 ValueVT.getVectorElementType() != PartVT) {
307 bool Smaller = ValueVT.bitsLE(PartVT);
308 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
309 DL, ValueVT.getScalarType(), Val);
310 }
311
Chris Lattner3ac18842010-08-24 23:20:40 +0000312 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
313}
314
315
316
Chris Lattnera13b8602010-08-24 23:10:06 +0000317
318static void getCopyToPartsVector(SelectionDAG &DAG, DebugLoc dl,
319 SDValue Val, SDValue *Parts, unsigned NumParts,
320 EVT PartVT);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000321
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000322/// getCopyToParts - Create a series of nodes that contain the specified value
323/// split into legal parts. If the parts contain more bits than Val, then, for
324/// integers, ExtendKind can be used to specify how to generate the extra bits.
Chris Lattnera13b8602010-08-24 23:10:06 +0000325static void getCopyToParts(SelectionDAG &DAG, DebugLoc DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000326 SDValue Val, SDValue *Parts, unsigned NumParts,
327 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000328 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Owen Andersone50ed302009-08-10 22:56:29 +0000329 EVT ValueVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000330
Chris Lattnera13b8602010-08-24 23:10:06 +0000331 // Handle the vector case separately.
332 if (ValueVT.isVector())
333 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000334
Chris Lattnera13b8602010-08-24 23:10:06 +0000335 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000336 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000337 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000338 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
339
Chris Lattnera13b8602010-08-24 23:10:06 +0000340 if (NumParts == 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 return;
342
Chris Lattnera13b8602010-08-24 23:10:06 +0000343 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
344 if (PartVT == ValueVT) {
345 assert(NumParts == 1 && "No-op copy with multiple parts!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000346 Parts[0] = Val;
347 return;
348 }
349
Chris Lattnera13b8602010-08-24 23:10:06 +0000350 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
351 // If the parts cover more bits than the value has, promote the value.
352 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
353 assert(NumParts == 1 && "Do not know what to promote to!");
354 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
355 } else {
356 assert(PartVT.isInteger() && ValueVT.isInteger() &&
Michael J. Spencere70c5262010-10-16 08:25:21 +0000357 "Unknown mismatch!");
Chris Lattnera13b8602010-08-24 23:10:06 +0000358 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
359 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
360 }
361 } else if (PartBits == ValueVT.getSizeInBits()) {
362 // Different types of the same size.
363 assert(NumParts == 1 && PartVT != ValueVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000365 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
366 // If the parts cover less bits than value has, truncate the value.
367 assert(PartVT.isInteger() && ValueVT.isInteger() &&
368 "Unknown mismatch!");
369 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
370 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
371 }
372
373 // The value may have changed - recompute ValueVT.
374 ValueVT = Val.getValueType();
375 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
376 "Failed to tile the value with PartVT!");
377
378 if (NumParts == 1) {
379 assert(PartVT == ValueVT && "Type conversion failed!");
380 Parts[0] = Val;
381 return;
382 }
383
384 // Expand the value into multiple parts.
385 if (NumParts & (NumParts - 1)) {
386 // The number of parts is not a power of 2. Split off and copy the tail.
387 assert(PartVT.isInteger() && ValueVT.isInteger() &&
388 "Do not know what to expand to!");
389 unsigned RoundParts = 1 << Log2_32(NumParts);
390 unsigned RoundBits = RoundParts * PartBits;
391 unsigned OddParts = NumParts - RoundParts;
392 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
393 DAG.getIntPtrConstant(RoundBits));
394 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT);
395
396 if (TLI.isBigEndian())
397 // The odd parts were reversed by getCopyToParts - unreverse them.
398 std::reverse(Parts + RoundParts, Parts + NumParts);
399
400 NumParts = RoundParts;
401 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
402 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
403 }
404
405 // The number of parts is a power of 2. Repeatedly bisect the value using
406 // EXTRACT_ELEMENT.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000407 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000408 EVT::getIntegerVT(*DAG.getContext(),
409 ValueVT.getSizeInBits()),
410 Val);
411
412 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
413 for (unsigned i = 0; i < NumParts; i += StepSize) {
414 unsigned ThisBits = StepSize * PartBits / 2;
415 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
416 SDValue &Part0 = Parts[i];
417 SDValue &Part1 = Parts[i+StepSize/2];
418
419 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
420 ThisVT, Part0, DAG.getIntPtrConstant(1));
421 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
422 ThisVT, Part0, DAG.getIntPtrConstant(0));
423
424 if (ThisBits == PartBits && ThisVT != PartVT) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000425 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
426 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
Chris Lattnera13b8602010-08-24 23:10:06 +0000427 }
428 }
429 }
430
431 if (TLI.isBigEndian())
432 std::reverse(Parts, Parts + OrigNumParts);
433}
434
435
436/// getCopyToPartsVector - Create a series of nodes that contain the specified
437/// value split into legal parts.
438static void getCopyToPartsVector(SelectionDAG &DAG, DebugLoc DL,
439 SDValue Val, SDValue *Parts, unsigned NumParts,
440 EVT PartVT) {
441 EVT ValueVT = Val.getValueType();
442 assert(ValueVT.isVector() && "Not a vector");
443 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000444
Chris Lattnera13b8602010-08-24 23:10:06 +0000445 if (NumParts == 1) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000446 if (PartVT == ValueVT) {
447 // Nothing to do.
448 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
449 // Bitconvert vector->vector case.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000450 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000451 } else if (PartVT.isVector() &&
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000452 PartVT.getVectorElementType() == ValueVT.getVectorElementType() &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000453 PartVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
454 EVT ElementVT = PartVT.getVectorElementType();
455 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
456 // undef elements.
457 SmallVector<SDValue, 16> Ops;
458 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
459 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
460 ElementVT, Val, DAG.getIntPtrConstant(i)));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000461
Chris Lattnere6f7c262010-08-25 22:49:25 +0000462 for (unsigned i = ValueVT.getVectorNumElements(),
463 e = PartVT.getVectorNumElements(); i != e; ++i)
464 Ops.push_back(DAG.getUNDEF(ElementVT));
465
466 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size());
467
468 // FIXME: Use CONCAT for 2x -> 4x.
Michael J. Spencere70c5262010-10-16 08:25:21 +0000469
Chris Lattnere6f7c262010-08-25 22:49:25 +0000470 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
471 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
Nadav Rotem0b666362011-06-04 20:58:08 +0000472 } else if (PartVT.isVector() &&
473 PartVT.getVectorElementType().bitsGE(
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000474 ValueVT.getVectorElementType()) &&
Nadav Rotem0b666362011-06-04 20:58:08 +0000475 PartVT.getVectorNumElements() == ValueVT.getVectorNumElements()) {
476
477 // Promoted vector extract
478 unsigned NumElts = ValueVT.getVectorNumElements();
479 SmallVector<SDValue, 8> NewOps;
480 for (unsigned i = 0; i < NumElts; ++i) {
481 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
482 ValueVT.getScalarType(), Val ,DAG.getIntPtrConstant(i));
483 SDValue Cast = DAG.getNode(ISD::ANY_EXTEND,
484 DL, PartVT.getScalarType(), Ext);
485 NewOps.push_back(Cast);
486 }
487 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT,
488 &NewOps[0], NewOps.size());
489 } else{
Chris Lattnere6f7c262010-08-25 22:49:25 +0000490 // Vector -> scalar conversion.
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000491 assert(ValueVT.getVectorNumElements() == 1 &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000492 "Only trivial vector-to-scalar conversions should get here!");
493 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
494 PartVT, Val, DAG.getIntPtrConstant(0));
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000495
496 bool Smaller = ValueVT.bitsLE(PartVT);
497 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
498 DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000499 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000500
Chris Lattnera13b8602010-08-24 23:10:06 +0000501 Parts[0] = Val;
502 return;
503 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000504
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000505 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000506 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000507 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000508 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
Devang Patel8f09bea2010-08-26 20:32:32 +0000509 IntermediateVT,
510 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000511 unsigned NumElements = ValueVT.getVectorNumElements();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000512
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000513 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
514 NumParts = NumRegs; // Silence a compiler warning.
515 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000516
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000517 // Split the vector into intermediate operands.
518 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000519 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000520 if (IntermediateVT.isVector())
Chris Lattnera13b8602010-08-24 23:10:06 +0000521 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000522 IntermediateVT, Val,
Chris Lattnera13b8602010-08-24 23:10:06 +0000523 DAG.getIntPtrConstant(i * (NumElements / NumIntermediates)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000524 else
Chris Lattnera13b8602010-08-24 23:10:06 +0000525 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Chris Lattnere6f7c262010-08-25 22:49:25 +0000526 IntermediateVT, Val, DAG.getIntPtrConstant(i));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000527 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000528
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000529 // Split the intermediate operands into legal parts.
530 if (NumParts == NumIntermediates) {
531 // If the register was not expanded, promote or copy the value,
532 // as appropriate.
533 for (unsigned i = 0; i != NumParts; ++i)
Chris Lattnera13b8602010-08-24 23:10:06 +0000534 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000535 } else if (NumParts > 0) {
536 // If the intermediate type was expanded, split each the value into
537 // legal parts.
538 assert(NumParts % NumIntermediates == 0 &&
539 "Must expand into a divisible number of parts!");
540 unsigned Factor = NumParts / NumIntermediates;
541 for (unsigned i = 0; i != NumIntermediates; ++i)
Chris Lattnera13b8602010-08-24 23:10:06 +0000542 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000543 }
544}
545
Chris Lattnera13b8602010-08-24 23:10:06 +0000546
547
548
Dan Gohman462f6b52010-05-29 17:53:24 +0000549namespace {
550 /// RegsForValue - This struct represents the registers (physical or virtual)
551 /// that a particular set of values is assigned, and the type information
552 /// about the value. The most common situation is to represent one value at a
553 /// time, but struct or array values are handled element-wise as multiple
554 /// values. The splitting of aggregates is performed recursively, so that we
555 /// never have aggregate-typed registers. The values at this point do not
556 /// necessarily have legal types, so each value may require one or more
557 /// registers of some legal type.
558 ///
559 struct RegsForValue {
560 /// ValueVTs - The value types of the values, which may not be legal, and
561 /// may need be promoted or synthesized from one or more registers.
562 ///
563 SmallVector<EVT, 4> ValueVTs;
564
565 /// RegVTs - The value types of the registers. This is the same size as
566 /// ValueVTs and it records, for each value, what the type of the assigned
567 /// register or registers are. (Individual values are never synthesized
568 /// from more than one type of register.)
569 ///
570 /// With virtual registers, the contents of RegVTs is redundant with TLI's
571 /// getRegisterType member function, however when with physical registers
572 /// it is necessary to have a separate record of the types.
573 ///
574 SmallVector<EVT, 4> RegVTs;
575
576 /// Regs - This list holds the registers assigned to the values.
577 /// Each legal or promoted value requires one register, and each
578 /// expanded value requires multiple registers.
579 ///
580 SmallVector<unsigned, 4> Regs;
581
582 RegsForValue() {}
583
584 RegsForValue(const SmallVector<unsigned, 4> &regs,
585 EVT regvt, EVT valuevt)
586 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
587
Dan Gohman462f6b52010-05-29 17:53:24 +0000588 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
589 unsigned Reg, const Type *Ty) {
590 ComputeValueVTs(tli, Ty, ValueVTs);
591
592 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
593 EVT ValueVT = ValueVTs[Value];
594 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
595 EVT RegisterVT = tli.getRegisterType(Context, ValueVT);
596 for (unsigned i = 0; i != NumRegs; ++i)
597 Regs.push_back(Reg + i);
598 RegVTs.push_back(RegisterVT);
599 Reg += NumRegs;
600 }
601 }
602
603 /// areValueTypesLegal - Return true if types of all the values are legal.
604 bool areValueTypesLegal(const TargetLowering &TLI) {
605 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
606 EVT RegisterVT = RegVTs[Value];
607 if (!TLI.isTypeLegal(RegisterVT))
608 return false;
609 }
610 return true;
611 }
612
613 /// append - Add the specified values to this one.
614 void append(const RegsForValue &RHS) {
615 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
616 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
617 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
618 }
619
620 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
621 /// this value and returns the result as a ValueVTs value. This uses
622 /// Chain/Flag as the input and updates them for the output Chain/Flag.
623 /// If the Flag pointer is NULL, no flag is used.
624 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
625 DebugLoc dl,
626 SDValue &Chain, SDValue *Flag) const;
627
628 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
629 /// specified value into the registers specified by this object. This uses
630 /// Chain/Flag as the input and updates them for the output Chain/Flag.
631 /// If the Flag pointer is NULL, no flag is used.
632 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
633 SDValue &Chain, SDValue *Flag) const;
634
635 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
636 /// operand list. This adds the code marker, matching input operand index
637 /// (if applicable), and includes the number of values added into it.
638 void AddInlineAsmOperands(unsigned Kind,
639 bool HasMatching, unsigned MatchingIdx,
640 SelectionDAG &DAG,
641 std::vector<SDValue> &Ops) const;
642 };
643}
644
645/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
646/// this value and returns the result as a ValueVT value. This uses
647/// Chain/Flag as the input and updates them for the output Chain/Flag.
648/// If the Flag pointer is NULL, no flag is used.
649SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
650 FunctionLoweringInfo &FuncInfo,
651 DebugLoc dl,
652 SDValue &Chain, SDValue *Flag) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000653 // A Value with type {} or [0 x %t] needs no registers.
654 if (ValueVTs.empty())
655 return SDValue();
656
Dan Gohman462f6b52010-05-29 17:53:24 +0000657 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
658
659 // Assemble the legal parts into the final values.
660 SmallVector<SDValue, 4> Values(ValueVTs.size());
661 SmallVector<SDValue, 8> Parts;
662 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
663 // Copy the legal parts from the registers.
664 EVT ValueVT = ValueVTs[Value];
665 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
666 EVT RegisterVT = RegVTs[Value];
667
668 Parts.resize(NumRegs);
669 for (unsigned i = 0; i != NumRegs; ++i) {
670 SDValue P;
671 if (Flag == 0) {
672 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
673 } else {
674 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
675 *Flag = P.getValue(2);
676 }
677
678 Chain = P.getValue(1);
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000679 Parts[i] = P;
Dan Gohman462f6b52010-05-29 17:53:24 +0000680
681 // If the source register was virtual and if we know something about it,
682 // add an assert node.
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000683 if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) ||
Cameron Zwariche1497b92011-02-24 10:00:08 +0000684 !RegisterVT.isInteger() || RegisterVT.isVector())
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000685 continue;
Cameron Zwariche1497b92011-02-24 10:00:08 +0000686
687 const FunctionLoweringInfo::LiveOutInfo *LOI =
688 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
689 if (!LOI)
690 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000691
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000692 unsigned RegSize = RegisterVT.getSizeInBits();
Cameron Zwariche1497b92011-02-24 10:00:08 +0000693 unsigned NumSignBits = LOI->NumSignBits;
694 unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes();
Dan Gohman462f6b52010-05-29 17:53:24 +0000695
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000696 // FIXME: We capture more information than the dag can represent. For
697 // now, just use the tightest assertzext/assertsext possible.
698 bool isSExt = true;
699 EVT FromVT(MVT::Other);
700 if (NumSignBits == RegSize)
701 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
702 else if (NumZeroBits >= RegSize-1)
703 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
704 else if (NumSignBits > RegSize-8)
705 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
706 else if (NumZeroBits >= RegSize-8)
707 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
708 else if (NumSignBits > RegSize-16)
709 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
710 else if (NumZeroBits >= RegSize-16)
711 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
712 else if (NumSignBits > RegSize-32)
713 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
714 else if (NumZeroBits >= RegSize-32)
715 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
716 else
717 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000718
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000719 // Add an assertion node.
720 assert(FromVT != MVT::Other);
721 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
722 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohman462f6b52010-05-29 17:53:24 +0000723 }
724
725 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
726 NumRegs, RegisterVT, ValueVT);
727 Part += NumRegs;
728 Parts.clear();
729 }
730
731 return DAG.getNode(ISD::MERGE_VALUES, dl,
732 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
733 &Values[0], ValueVTs.size());
734}
735
736/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
737/// specified value into the registers specified by this object. This uses
738/// Chain/Flag as the input and updates them for the output Chain/Flag.
739/// If the Flag pointer is NULL, no flag is used.
740void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
741 SDValue &Chain, SDValue *Flag) const {
742 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
743
744 // Get the list of the values's legal parts.
745 unsigned NumRegs = Regs.size();
746 SmallVector<SDValue, 8> Parts(NumRegs);
747 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
748 EVT ValueVT = ValueVTs[Value];
749 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
750 EVT RegisterVT = RegVTs[Value];
751
Chris Lattner3ac18842010-08-24 23:20:40 +0000752 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohman462f6b52010-05-29 17:53:24 +0000753 &Parts[Part], NumParts, RegisterVT);
754 Part += NumParts;
755 }
756
757 // Copy the parts into the registers.
758 SmallVector<SDValue, 8> Chains(NumRegs);
759 for (unsigned i = 0; i != NumRegs; ++i) {
760 SDValue Part;
761 if (Flag == 0) {
762 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
763 } else {
764 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
765 *Flag = Part.getValue(1);
766 }
767
768 Chains[i] = Part.getValue(0);
769 }
770
771 if (NumRegs == 1 || Flag)
772 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
773 // flagged to it. That is the CopyToReg nodes and the user are considered
774 // a single scheduling unit. If we create a TokenFactor and return it as
775 // chain, then the TokenFactor is both a predecessor (operand) of the
776 // user as well as a successor (the TF operands are flagged to the user).
777 // c1, f1 = CopyToReg
778 // c2, f2 = CopyToReg
779 // c3 = TokenFactor c1, c2
780 // ...
781 // = op c3, ..., f2
782 Chain = Chains[NumRegs-1];
783 else
784 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
785}
786
787/// AddInlineAsmOperands - Add this value to the specified inlineasm node
788/// operand list. This adds the code marker and includes the number of
789/// values added into it.
790void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
791 unsigned MatchingIdx,
792 SelectionDAG &DAG,
793 std::vector<SDValue> &Ops) const {
794 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
795
796 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
797 if (HasMatching)
798 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
799 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
800 Ops.push_back(Res);
801
802 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
803 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
804 EVT RegisterVT = RegVTs[Value];
805 for (unsigned i = 0; i != NumRegs; ++i) {
806 assert(Reg < Regs.size() && "Mismatch in # registers expected");
807 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
808 }
809 }
810}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000811
Dan Gohman2048b852009-11-23 18:04:58 +0000812void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000813 AA = &aa;
814 GFI = gfi;
815 TD = DAG.getTarget().getTargetData();
816}
817
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000818/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000819/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000820/// for a new block. This doesn't clear out information about
821/// additional blocks that are needed to complete switch lowering
822/// or PHI node updating; that information is cleared out as it is
823/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000824void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000825 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000826 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000827 PendingLoads.clear();
828 PendingExports.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000829 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000830 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000831}
832
Devang Patel23385752011-05-23 17:44:13 +0000833/// clearDanglingDebugInfo - Clear the dangling debug information
834/// map. This function is seperated from the clear so that debug
835/// information that is dangling in a basic block can be properly
836/// resolved in a different basic block. This allows the
837/// SelectionDAG to resolve dangling debug information attached
838/// to PHI nodes.
839void SelectionDAGBuilder::clearDanglingDebugInfo() {
840 DanglingDebugInfoMap.clear();
841}
842
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000843/// getRoot - Return the current virtual root of the Selection DAG,
844/// flushing any PendingLoad items. This must be done before emitting
845/// a store or any other node that may need to be ordered after any
846/// prior load instructions.
847///
Dan Gohman2048b852009-11-23 18:04:58 +0000848SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000849 if (PendingLoads.empty())
850 return DAG.getRoot();
851
852 if (PendingLoads.size() == 1) {
853 SDValue Root = PendingLoads[0];
854 DAG.setRoot(Root);
855 PendingLoads.clear();
856 return Root;
857 }
858
859 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000861 &PendingLoads[0], PendingLoads.size());
862 PendingLoads.clear();
863 DAG.setRoot(Root);
864 return Root;
865}
866
867/// getControlRoot - Similar to getRoot, but instead of flushing all the
868/// PendingLoad items, flush all the PendingExports items. It is necessary
869/// to do this before emitting a terminator instruction.
870///
Dan Gohman2048b852009-11-23 18:04:58 +0000871SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000872 SDValue Root = DAG.getRoot();
873
874 if (PendingExports.empty())
875 return Root;
876
877 // Turn all of the CopyToReg chains into one factored node.
878 if (Root.getOpcode() != ISD::EntryToken) {
879 unsigned i = 0, e = PendingExports.size();
880 for (; i != e; ++i) {
881 assert(PendingExports[i].getNode()->getNumOperands() > 1);
882 if (PendingExports[i].getNode()->getOperand(0) == Root)
883 break; // Don't add the root if we already indirectly depend on it.
884 }
885
886 if (i == e)
887 PendingExports.push_back(Root);
888 }
889
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000891 &PendingExports[0],
892 PendingExports.size());
893 PendingExports.clear();
894 DAG.setRoot(Root);
895 return Root;
896}
897
Bill Wendling4533cac2010-01-28 21:51:40 +0000898void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
899 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
900 DAG.AssignOrdering(Node, SDNodeOrder);
901
902 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
903 AssignOrderingToNode(Node->getOperand(I).getNode());
904}
905
Dan Gohman46510a72010-04-15 01:51:59 +0000906void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000907 // Set up outgoing PHI node register values before emitting the terminator.
908 if (isa<TerminatorInst>(&I))
909 HandlePHINodesInSuccessorBlocks(I.getParent());
910
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000911 CurDebugLoc = I.getDebugLoc();
912
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000913 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000914
Dan Gohman92884f72010-04-20 15:03:56 +0000915 if (!isa<TerminatorInst>(&I) && !HasTailCall)
916 CopyToExportRegsIfNeeded(&I);
917
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000918 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000919}
920
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000921void SelectionDAGBuilder::visitPHI(const PHINode &) {
922 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
923}
924
Dan Gohman46510a72010-04-15 01:51:59 +0000925void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000926 // Note: this doesn't use InstVisitor, because it has to work with
927 // ConstantExpr's in addition to instructions.
928 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000929 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000930 // Build the switch statement using the Instruction.def file.
931#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000932 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000933#include "llvm/Instruction.def"
934 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000935
936 // Assign the ordering to the freshly created DAG nodes.
937 if (NodeMap.count(&I)) {
938 ++SDNodeOrder;
939 AssignOrderingToNode(getValue(&I).getNode());
940 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000941}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000942
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000943// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
944// generate the debug data structures now that we've seen its definition.
945void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
946 SDValue Val) {
947 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
Devang Patel4cf81c42010-08-26 23:35:15 +0000948 if (DDI.getDI()) {
949 const DbgValueInst *DI = DDI.getDI();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000950 DebugLoc dl = DDI.getdl();
951 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
Devang Patel4cf81c42010-08-26 23:35:15 +0000952 MDNode *Variable = DI->getVariable();
953 uint64_t Offset = DI->getOffset();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000954 SDDbgValue *SDV;
955 if (Val.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +0000956 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000957 SDV = DAG.getDbgValue(Variable, Val.getNode(),
958 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
959 DAG.AddDbgValue(SDV, Val.getNode(), false);
960 }
Owen Anderson95771af2011-02-25 21:41:48 +0000961 } else
Devang Patelafeaae72010-12-06 22:39:26 +0000962 DEBUG(dbgs() << "Dropping debug info for " << DI);
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000963 DanglingDebugInfoMap[V] = DanglingDebugInfo();
964 }
965}
966
Dan Gohman28a17352010-07-01 01:59:43 +0000967// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +0000968SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +0000969 // If we already have an SDValue for this value, use it. It's important
970 // to do this first, so that we don't create a CopyFromReg if we already
971 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000972 SDValue &N = NodeMap[V];
973 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000974
Dan Gohman28a17352010-07-01 01:59:43 +0000975 // If there's a virtual register allocated and initialized for this
976 // value, use it.
977 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
978 if (It != FuncInfo.ValueMap.end()) {
979 unsigned InReg = It->second;
980 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
981 SDValue Chain = DAG.getEntryNode();
Devang Patel8f314282011-01-25 18:09:58 +0000982 N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain,NULL);
983 resolveDanglingDebugInfo(V, N);
984 return N;
Dan Gohman28a17352010-07-01 01:59:43 +0000985 }
986
987 // Otherwise create a new SDValue and remember it.
988 SDValue Val = getValueImpl(V);
989 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000990 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +0000991 return Val;
992}
993
994/// getNonRegisterValue - Return an SDValue for the given Value, but
995/// don't look in FuncInfo.ValueMap for a virtual register.
996SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
997 // If we already have an SDValue for this value, use it.
998 SDValue &N = NodeMap[V];
999 if (N.getNode()) return N;
1000
1001 // Otherwise create a new SDValue and remember it.
1002 SDValue Val = getValueImpl(V);
1003 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001004 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001005 return Val;
1006}
1007
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001008/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +00001009/// Create an SDValue for the given value.
1010SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Dan Gohman383b5f62010-04-17 15:32:28 +00001011 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001012 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001013
Dan Gohman383b5f62010-04-17 15:32:28 +00001014 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001015 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001016
Dan Gohman383b5f62010-04-17 15:32:28 +00001017 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Devang Patel0d881da2010-07-06 22:08:15 +00001018 return DAG.getGlobalAddress(GV, getCurDebugLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001019
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001020 if (isa<ConstantPointerNull>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001021 return DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001022
Dan Gohman383b5f62010-04-17 15:32:28 +00001023 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001024 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001025
Nate Begeman9008ca62009-04-27 18:41:29 +00001026 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +00001027 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001028
Dan Gohman383b5f62010-04-17 15:32:28 +00001029 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001030 visit(CE->getOpcode(), *CE);
1031 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +00001032 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001033 return N1;
1034 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001035
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001036 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1037 SmallVector<SDValue, 4> Constants;
1038 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
1039 OI != OE; ++OI) {
1040 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +00001041 // If the operand is an empty aggregate, there are no values.
1042 if (!Val) continue;
1043 // Add each leaf value from the operand to the Constants list
1044 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001045 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1046 Constants.push_back(SDValue(Val, i));
1047 }
Bill Wendling87710f02009-12-21 23:47:40 +00001048
Bill Wendling4533cac2010-01-28 21:51:40 +00001049 return DAG.getMergeValues(&Constants[0], Constants.size(),
1050 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001051 }
1052
Duncan Sands1df98592010-02-16 11:11:14 +00001053 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001054 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1055 "Unknown struct or array constant!");
1056
Owen Andersone50ed302009-08-10 22:56:29 +00001057 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001058 ComputeValueVTs(TLI, C->getType(), ValueVTs);
1059 unsigned NumElts = ValueVTs.size();
1060 if (NumElts == 0)
1061 return SDValue(); // empty struct
1062 SmallVector<SDValue, 4> Constants(NumElts);
1063 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001064 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001065 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001066 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001067 else if (EltVT.isFloatingPoint())
1068 Constants[i] = DAG.getConstantFP(0, EltVT);
1069 else
1070 Constants[i] = DAG.getConstant(0, EltVT);
1071 }
Bill Wendling87710f02009-12-21 23:47:40 +00001072
Bill Wendling4533cac2010-01-28 21:51:40 +00001073 return DAG.getMergeValues(&Constants[0], NumElts,
1074 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001075 }
1076
Dan Gohman383b5f62010-04-17 15:32:28 +00001077 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001078 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001079
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001080 const VectorType *VecTy = cast<VectorType>(V->getType());
1081 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 // Now that we know the number and type of the elements, get that number of
1084 // elements into the Ops array based on what kind of constant it is.
1085 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +00001086 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001087 for (unsigned i = 0; i != NumElements; ++i)
1088 Ops.push_back(getValue(CP->getOperand(i)));
1089 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001090 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +00001091 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001092
1093 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001094 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001095 Op = DAG.getConstantFP(0, EltVT);
1096 else
1097 Op = DAG.getConstant(0, EltVT);
1098 Ops.assign(NumElements, Op);
1099 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001100
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001101 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +00001102 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
1103 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001104 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001105
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001106 // If this is a static alloca, generate it as the frameindex instead of
1107 // computation.
1108 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1109 DenseMap<const AllocaInst*, int>::iterator SI =
1110 FuncInfo.StaticAllocaMap.find(AI);
1111 if (SI != FuncInfo.StaticAllocaMap.end())
1112 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
1113 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001114
Dan Gohman28a17352010-07-01 01:59:43 +00001115 // If this is an instruction which fast-isel has deferred, select it now.
1116 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001117 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
1118 RegsForValue RFV(*DAG.getContext(), TLI, InReg, Inst->getType());
1119 SDValue Chain = DAG.getEntryNode();
1120 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL);
Dan Gohman28a17352010-07-01 01:59:43 +00001121 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001122
Dan Gohman28a17352010-07-01 01:59:43 +00001123 llvm_unreachable("Can't get register for value!");
1124 return SDValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001125}
1126
Dan Gohman46510a72010-04-15 01:51:59 +00001127void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001128 SDValue Chain = getControlRoot();
1129 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001130 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001131
Dan Gohman7451d3e2010-05-29 17:03:36 +00001132 if (!FuncInfo.CanLowerReturn) {
1133 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001134 const Function *F = I.getParent()->getParent();
1135
1136 // Emit a store of the return value through the virtual register.
1137 // Leave Outs empty so that LowerReturn won't try to load return
1138 // registers the usual way.
1139 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001140 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001141 PtrValueVTs);
1142
1143 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1144 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001145
Owen Andersone50ed302009-08-10 22:56:29 +00001146 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001147 SmallVector<uint64_t, 4> Offsets;
1148 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001149 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001150
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001151 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001152 for (unsigned i = 0; i != NumValues; ++i) {
Chris Lattnera13b8602010-08-24 23:10:06 +00001153 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(),
1154 RetPtr.getValueType(), RetPtr,
1155 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001156 Chains[i] =
1157 DAG.getStore(Chain, getCurDebugLoc(),
1158 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
Chris Lattner84bd98a2010-09-21 18:58:22 +00001159 // FIXME: better loc info would be nice.
1160 Add, MachinePointerInfo(), false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001161 }
1162
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001163 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
1164 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001165 } else if (I.getNumOperands() != 0) {
1166 SmallVector<EVT, 4> ValueVTs;
1167 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
1168 unsigned NumValues = ValueVTs.size();
1169 if (NumValues) {
1170 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001171 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1172 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001173
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001174 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001175
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001176 const Function *F = I.getParent()->getParent();
1177 if (F->paramHasAttr(0, Attribute::SExt))
1178 ExtendKind = ISD::SIGN_EXTEND;
1179 else if (F->paramHasAttr(0, Attribute::ZExt))
1180 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001181
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001182 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
1183 VT = TLI.getTypeForExtArgOrReturn(*DAG.getContext(), VT, ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001184
1185 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
1186 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
1187 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +00001188 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001189 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
1190 &Parts[0], NumParts, PartVT, ExtendKind);
1191
1192 // 'inreg' on function refers to return value
1193 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1194 if (F->paramHasAttr(0, Attribute::InReg))
1195 Flags.setInReg();
1196
1197 // Propagate extension type if any
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001198 if (ExtendKind == ISD::SIGN_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001199 Flags.setSExt();
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001200 else if (ExtendKind == ISD::ZERO_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001201 Flags.setZExt();
1202
Dan Gohmanc9403652010-07-07 15:54:55 +00001203 for (unsigned i = 0; i < NumParts; ++i) {
1204 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
1205 /*isfixed=*/true));
1206 OutVals.push_back(Parts[i]);
1207 }
Evan Cheng3927f432009-03-25 20:20:11 +00001208 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001209 }
1210 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001211
1212 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001213 CallingConv::ID CallConv =
1214 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001215 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
Dan Gohmanc9403652010-07-07 15:54:55 +00001216 Outs, OutVals, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001217
1218 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001219 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001220 "LowerReturn didn't return a valid chain!");
1221
1222 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001223 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001224}
1225
Dan Gohmanad62f532009-04-23 23:13:24 +00001226/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1227/// created for it, emit nodes to copy the value into the virtual
1228/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001229void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00001230 // Skip empty types
1231 if (V->getType()->isEmptyTy())
1232 return;
1233
Dan Gohman33b7a292010-04-16 17:15:02 +00001234 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1235 if (VMI != FuncInfo.ValueMap.end()) {
1236 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1237 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001238 }
1239}
1240
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001241/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1242/// the current basic block, add it to ValueMap now so that we'll get a
1243/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001244void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001245 // No need to export constants.
1246 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001247
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001248 // Already exported?
1249 if (FuncInfo.isExportedInst(V)) return;
1250
1251 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1252 CopyValueToVirtualRegister(V, Reg);
1253}
1254
Dan Gohman46510a72010-04-15 01:51:59 +00001255bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001256 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001257 // The operands of the setcc have to be in this block. We don't know
1258 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001259 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001260 // Can export from current BB.
1261 if (VI->getParent() == FromBB)
1262 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001263
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001264 // Is already exported, noop.
1265 return FuncInfo.isExportedInst(V);
1266 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001267
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001268 // If this is an argument, we can export it if the BB is the entry block or
1269 // if it is already exported.
1270 if (isa<Argument>(V)) {
1271 if (FromBB == &FromBB->getParent()->getEntryBlock())
1272 return true;
1273
1274 // Otherwise, can only export this if it is already exported.
1275 return FuncInfo.isExportedInst(V);
1276 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 // Otherwise, constants can always be exported.
1279 return true;
1280}
1281
1282static bool InBlock(const Value *V, const BasicBlock *BB) {
1283 if (const Instruction *I = dyn_cast<Instruction>(V))
1284 return I->getParent() == BB;
1285 return true;
1286}
1287
Dan Gohmanc2277342008-10-17 21:16:08 +00001288/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1289/// This function emits a branch and is used at the leaves of an OR or an
1290/// AND operator tree.
1291///
1292void
Dan Gohman46510a72010-04-15 01:51:59 +00001293SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001294 MachineBasicBlock *TBB,
1295 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001296 MachineBasicBlock *CurBB,
1297 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001298 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001299
Dan Gohmanc2277342008-10-17 21:16:08 +00001300 // If the leaf of the tree is a comparison, merge the condition into
1301 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001302 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001303 // The operands of the cmp have to be in this block. We don't know
1304 // how to export them from some other block. If this is the first block
1305 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001306 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001307 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1308 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001309 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001310 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001311 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001312 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001313 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001314 } else {
1315 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001316 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001317 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001318
1319 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1321 SwitchCases.push_back(CB);
1322 return;
1323 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001324 }
1325
1326 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001327 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001328 NULL, TBB, FBB, CurBB);
1329 SwitchCases.push_back(CB);
1330}
1331
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001332/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001333void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001334 MachineBasicBlock *TBB,
1335 MachineBasicBlock *FBB,
1336 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001337 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001338 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001339 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001340 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001341 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001342 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1343 BOp->getParent() != CurBB->getBasicBlock() ||
1344 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1345 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001346 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347 return;
1348 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001349
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001350 // Create TmpBB after CurBB.
1351 MachineFunction::iterator BBI = CurBB;
1352 MachineFunction &MF = DAG.getMachineFunction();
1353 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1354 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001355
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001356 if (Opc == Instruction::Or) {
1357 // Codegen X | Y as:
1358 // jmp_if_X TBB
1359 // jmp TmpBB
1360 // TmpBB:
1361 // jmp_if_Y TBB
1362 // jmp FBB
1363 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001364
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001366 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001367
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001368 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001369 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001370 } else {
1371 assert(Opc == Instruction::And && "Unknown merge op!");
1372 // Codegen X & Y as:
1373 // jmp_if_X TmpBB
1374 // jmp FBB
1375 // TmpBB:
1376 // jmp_if_Y TBB
1377 // jmp FBB
1378 //
1379 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001380
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001381 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001382 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001383
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001384 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001385 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386 }
1387}
1388
1389/// If the set of cases should be emitted as a series of branches, return true.
1390/// If we should emit this as a bunch of and/or'd together conditions, return
1391/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001392bool
Dan Gohman2048b852009-11-23 18:04:58 +00001393SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001394 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001395
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 // If this is two comparisons of the same values or'd or and'd together, they
1397 // will get folded into a single comparison, so don't emit two blocks.
1398 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1399 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1400 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1401 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1402 return false;
1403 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001404
Chris Lattner133ce872010-01-02 00:00:03 +00001405 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1406 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1407 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1408 Cases[0].CC == Cases[1].CC &&
1409 isa<Constant>(Cases[0].CmpRHS) &&
1410 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1411 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1412 return false;
1413 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1414 return false;
1415 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00001416
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001417 return true;
1418}
1419
Dan Gohman46510a72010-04-15 01:51:59 +00001420void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001421 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001422
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001423 // Update machine-CFG edges.
1424 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1425
1426 // Figure out which block is immediately after the current one.
1427 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001428 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001429 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430 NextBlock = BBI;
1431
1432 if (I.isUnconditional()) {
1433 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001434 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001435
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001437 if (Succ0MBB != NextBlock)
1438 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001439 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001440 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001441
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442 return;
1443 }
1444
1445 // If this condition is one of the special cases we handle, do special stuff
1446 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001447 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001448 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1449
1450 // If this is a series of conditions that are or'd or and'd together, emit
1451 // this as a sequence of branches instead of setcc's with and/or operations.
Chris Lattnerde189be2010-11-30 18:12:52 +00001452 // As long as jumps are not expensive, this should improve performance.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001453 // For example, instead of something like:
1454 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001455 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001456 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001457 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458 // or C, F
1459 // jnz foo
1460 // Emit:
1461 // cmp A, B
1462 // je foo
1463 // cmp D, E
1464 // jle foo
1465 //
Dan Gohman46510a72010-04-15 01:51:59 +00001466 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Owen Anderson95771af2011-02-25 21:41:48 +00001467 if (!TLI.isJumpExpensive() &&
Chris Lattnerde189be2010-11-30 18:12:52 +00001468 BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001469 (BOp->getOpcode() == Instruction::And ||
1470 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001471 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1472 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 // If the compares in later blocks need to use values not currently
1474 // exported from this block, export them now. This block should always
1475 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001476 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001477
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001478 // Allow some cases to be rejected.
1479 if (ShouldEmitAsBranches(SwitchCases)) {
1480 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1481 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1482 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1483 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001484
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001486 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001487 SwitchCases.erase(SwitchCases.begin());
1488 return;
1489 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001490
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001491 // Okay, we decided not to do this, remove any inserted MBB's and clear
1492 // SwitchCases.
1493 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001494 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001495
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001496 SwitchCases.clear();
1497 }
1498 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001499
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001500 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001501 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001502 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001503
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001504 // Use visitSwitchCase to actually insert the fast branch sequence for this
1505 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001506 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001507}
1508
1509/// visitSwitchCase - Emits the necessary code to represent a single node in
1510/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001511void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1512 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001513 SDValue Cond;
1514 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001515 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001516
1517 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001518 if (CB.CmpMHS == NULL) {
1519 // Fold "(X == true)" to X and "(X == false)" to !X to
1520 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001521 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001522 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001523 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001524 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001525 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001526 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001527 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001528 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001530 } else {
1531 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1532
Anton Korobeynikov23218582008-12-23 22:25:27 +00001533 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1534 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001535
1536 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001537 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001538
1539 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001540 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001541 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001543 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001544 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001545 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 DAG.getConstant(High-Low, VT), ISD::SETULE);
1547 }
1548 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001549
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001550 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001551 SwitchBB->addSuccessor(CB.TrueBB);
1552 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001553
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001554 // Set NextBlock to be the MBB immediately after the current one, if any.
1555 // This is used to avoid emitting unnecessary branches to the next block.
1556 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001557 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001558 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001559 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001560
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001561 // If the lhs block is the next block, invert the condition so that we can
1562 // fall through to the lhs instead of the rhs block.
1563 if (CB.TrueBB == NextBlock) {
1564 std::swap(CB.TrueBB, CB.FalseBB);
1565 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001566 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001568
Dale Johannesenf5d97892009-02-04 01:48:28 +00001569 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001571 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001572
Evan Cheng266a99d2010-09-23 06:51:55 +00001573 // Insert the false branch. Do this even if it's a fall through branch,
1574 // this makes it easier to do DAG optimizations which require inverting
1575 // the branch condition.
1576 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1577 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001578
1579 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580}
1581
1582/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001583void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001584 // Emit the code for the jump table
1585 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001586 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001587 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1588 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001589 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001590 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1591 MVT::Other, Index.getValue(1),
1592 Table, Index);
1593 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001594}
1595
1596/// visitJumpTableHeader - This function emits necessary code to produce index
1597/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001598void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001599 JumpTableHeader &JTH,
1600 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001601 // Subtract the lowest switch case value from the value being switched on and
1602 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001603 // difference between smallest and largest cases.
1604 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001606 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001607 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001608
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001609 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001610 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001611 // can be used as an index into the jump table in a subsequent basic block.
1612 // This value may be smaller or larger than the target's pointer type, and
1613 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001614 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001615
Dan Gohman89496d02010-07-02 00:10:16 +00001616 unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001617 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1618 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 JT.Reg = JumpTableReg;
1620
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001621 // Emit the range check for the jump table, and branch to the default block
1622 // for the switch statement if the value being switched on exceeds the largest
1623 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001624 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001625 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001626 DAG.getConstant(JTH.Last-JTH.First,VT),
1627 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001628
1629 // Set NextBlock to be the MBB immediately after the current one, if any.
1630 // This is used to avoid emitting unnecessary branches to the next block.
1631 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001632 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001633
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001634 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001635 NextBlock = BBI;
1636
Dale Johannesen66978ee2009-01-31 02:22:37 +00001637 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001638 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001639 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640
Bill Wendling4533cac2010-01-28 21:51:40 +00001641 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001642 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1643 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001644
Bill Wendling87710f02009-12-21 23:47:40 +00001645 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001646}
1647
1648/// visitBitTestHeader - This function emits necessary code to produce value
1649/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001650void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1651 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001652 // Subtract the minimum value
1653 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001654 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001655 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001656 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001657
1658 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001659 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001660 TLI.getSetCCResultType(Sub.getValueType()),
1661 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001662 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001663
Evan Chengd08e5b42011-01-06 01:02:44 +00001664 // Determine the type of the test operands.
1665 bool UsePtrType = false;
1666 if (!TLI.isTypeLegal(VT))
1667 UsePtrType = true;
1668 else {
1669 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
1670 if ((uint64_t)((int64_t)B.Cases[i].Mask >> VT.getSizeInBits()) + 1 >= 2) {
1671 // Switch table case range are encoded into series of masks.
1672 // Just use pointer type, it's guaranteed to fit.
1673 UsePtrType = true;
1674 break;
1675 }
1676 }
1677 if (UsePtrType) {
1678 VT = TLI.getPointerTy();
1679 Sub = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), VT);
1680 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681
Evan Chengd08e5b42011-01-06 01:02:44 +00001682 B.RegVT = VT;
1683 B.Reg = FuncInfo.CreateReg(VT);
Dale Johannesena04b7572009-02-03 23:04:43 +00001684 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001685 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001686
1687 // Set NextBlock to be the MBB immediately after the current one, if any.
1688 // This is used to avoid emitting unnecessary branches to the next block.
1689 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001690 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001691 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001692 NextBlock = BBI;
1693
1694 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1695
Dan Gohman99be8ae2010-04-19 22:41:47 +00001696 SwitchBB->addSuccessor(B.Default);
1697 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001698
Dale Johannesen66978ee2009-01-31 02:22:37 +00001699 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001700 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001701 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001702
Evan Cheng8c1f4322010-09-23 18:32:19 +00001703 if (MBB != NextBlock)
1704 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1705 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001706
Bill Wendling87710f02009-12-21 23:47:40 +00001707 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001708}
1709
1710/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001711void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1712 MachineBasicBlock* NextMBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001713 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001714 BitTestCase &B,
1715 MachineBasicBlock *SwitchBB) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001716 EVT VT = BB.RegVT;
1717 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1718 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001719 SDValue Cmp;
1720 if (CountPopulation_64(B.Mask) == 1) {
1721 // Testing for a single bit; just compare the shift count with what it
1722 // would need to be to shift a 1 bit in that position.
1723 Cmp = DAG.getSetCC(getCurDebugLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001724 TLI.getSetCCResultType(VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001725 ShiftOp,
Evan Chengd08e5b42011-01-06 01:02:44 +00001726 DAG.getConstant(CountTrailingZeros_64(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001727 ISD::SETEQ);
1728 } else {
1729 // Make desired shift
Evan Chengd08e5b42011-01-06 01:02:44 +00001730 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), VT,
1731 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001732
Dan Gohman8e0163a2010-06-24 02:06:24 +00001733 // Emit bit tests and jumps
1734 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001735 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Dan Gohman8e0163a2010-06-24 02:06:24 +00001736 Cmp = DAG.getSetCC(getCurDebugLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001737 TLI.getSetCCResultType(VT),
1738 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001739 ISD::SETNE);
1740 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741
Dan Gohman99be8ae2010-04-19 22:41:47 +00001742 SwitchBB->addSuccessor(B.TargetBB);
1743 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001744
Dale Johannesen66978ee2009-01-31 02:22:37 +00001745 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001746 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001747 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748
1749 // Set NextBlock to be the MBB immediately after the current one, if any.
1750 // This is used to avoid emitting unnecessary branches to the next block.
1751 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001752 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001753 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001754 NextBlock = BBI;
1755
Evan Cheng8c1f4322010-09-23 18:32:19 +00001756 if (NextMBB != NextBlock)
1757 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1758 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001759
Bill Wendling87710f02009-12-21 23:47:40 +00001760 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001761}
1762
Dan Gohman46510a72010-04-15 01:51:59 +00001763void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001764 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001766 // Retrieve successors.
1767 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1768 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1769
Gabor Greifb67e6b32009-01-15 11:10:44 +00001770 const Value *Callee(I.getCalledValue());
1771 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001772 visitInlineAsm(&I);
1773 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001774 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001775
1776 // If the value of the invoke is used outside of its defining block, make it
1777 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001778 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001779
1780 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001781 InvokeMBB->addSuccessor(Return);
1782 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001783
1784 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001785 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1786 MVT::Other, getControlRoot(),
1787 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001788}
1789
Dan Gohman46510a72010-04-15 01:51:59 +00001790void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791}
1792
1793/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1794/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001795bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1796 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001797 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001798 MachineBasicBlock *Default,
1799 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001800 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001802 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001803 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001804 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001805 return false;
1806
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001807 // Get the MachineFunction which holds the current MBB. This is used when
1808 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001809 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001810
1811 // Figure out which block is immediately after the current one.
1812 MachineBasicBlock *NextBlock = 0;
1813 MachineFunction::iterator BBI = CR.CaseBB;
1814
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001815 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816 NextBlock = BBI;
1817
Benjamin Kramerce750f02010-11-22 09:45:38 +00001818 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001819 // is the same as the other, but has one bit unset that the other has set,
1820 // use bit manipulation to do two compares at once. For example:
1821 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00001822 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
1823 // TODO: Handle cases where CR.CaseBB != SwitchBB.
1824 if (Size == 2 && CR.CaseBB == SwitchBB) {
1825 Case &Small = *CR.Range.first;
1826 Case &Big = *(CR.Range.second-1);
1827
1828 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
1829 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
1830 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
1831
1832 // Check that there is only one bit different.
1833 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
1834 (SmallValue | BigValue) == BigValue) {
1835 // Isolate the common bit.
1836 APInt CommonBit = BigValue & ~SmallValue;
1837 assert((SmallValue | CommonBit) == BigValue &&
1838 CommonBit.countPopulation() == 1 && "Not a common bit?");
1839
1840 SDValue CondLHS = getValue(SV);
1841 EVT VT = CondLHS.getValueType();
1842 DebugLoc DL = getCurDebugLoc();
1843
1844 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
1845 DAG.getConstant(CommonBit, VT));
1846 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
1847 Or, DAG.getConstant(BigValue, VT),
1848 ISD::SETEQ);
1849
1850 // Update successor info.
1851 SwitchBB->addSuccessor(Small.BB);
1852 SwitchBB->addSuccessor(Default);
1853
1854 // Insert the true branch.
1855 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
1856 getControlRoot(), Cond,
1857 DAG.getBasicBlock(Small.BB));
1858
1859 // Insert the false branch.
1860 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
1861 DAG.getBasicBlock(Default));
1862
1863 DAG.setRoot(BrCond);
1864 return true;
1865 }
1866 }
1867 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001868
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001869 // Rearrange the case blocks so that the last one falls through if possible.
1870 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1871 // The last case block won't fall through into 'NextBlock' if we emit the
1872 // branches in this order. See if rearranging a case value would help.
1873 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1874 if (I->BB == NextBlock) {
1875 std::swap(*I, BackCase);
1876 break;
1877 }
1878 }
1879 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001880
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001881 // Create a CaseBlock record representing a conditional branch to
1882 // the Case's target mbb if the value being switched on SV is equal
1883 // to C.
1884 MachineBasicBlock *CurBlock = CR.CaseBB;
1885 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1886 MachineBasicBlock *FallThrough;
1887 if (I != E-1) {
1888 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1889 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001890
1891 // Put SV in a virtual register to make it available from the new blocks.
1892 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001893 } else {
1894 // If the last case doesn't match, go to the default block.
1895 FallThrough = Default;
1896 }
1897
Dan Gohman46510a72010-04-15 01:51:59 +00001898 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001899 ISD::CondCode CC;
1900 if (I->High == I->Low) {
1901 // This is just small small case range :) containing exactly 1 case
1902 CC = ISD::SETEQ;
1903 LHS = SV; RHS = I->High; MHS = NULL;
1904 } else {
1905 CC = ISD::SETLE;
1906 LHS = I->Low; MHS = SV; RHS = I->High;
1907 }
1908 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001910 // If emitting the first comparison, just call visitSwitchCase to emit the
1911 // code into the current block. Otherwise, push the CaseBlock onto the
1912 // vector to be later processed by SDISel, and insert the node's MBB
1913 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001914 if (CurBlock == SwitchBB)
1915 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001916 else
1917 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001918
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919 CurBlock = FallThrough;
1920 }
1921
1922 return true;
1923}
1924
1925static inline bool areJTsAllowed(const TargetLowering &TLI) {
1926 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1928 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001930
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001931static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001932 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Jay Foad40f8f622010-12-07 08:25:19 +00001933 APInt LastExt = Last.sext(BitWidth), FirstExt = First.sext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001934 return (LastExt - FirstExt + 1ULL);
1935}
1936
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001937/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001938bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1939 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001940 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001941 MachineBasicBlock* Default,
1942 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001943 Case& FrontCase = *CR.Range.first;
1944 Case& BackCase = *(CR.Range.second-1);
1945
Chris Lattnere880efe2009-11-07 07:50:34 +00001946 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1947 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001948
Chris Lattnere880efe2009-11-07 07:50:34 +00001949 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001950 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1951 I!=E; ++I)
1952 TSize += I->size();
1953
Dan Gohmane0567812010-04-08 23:03:40 +00001954 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001956
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001957 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001958 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001959 if (Density < 0.4)
1960 return false;
1961
David Greene4b69d992010-01-05 01:24:57 +00001962 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001963 << "First entry: " << First << ". Last entry: " << Last << '\n'
1964 << "Range: " << Range
Jim Grosbach3fc83172011-02-25 03:59:03 +00001965 << ". Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001966
1967 // Get the MachineFunction which holds the current MBB. This is used when
1968 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001969 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001970
1971 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001972 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001973 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001974
1975 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1976
1977 // Create a new basic block to hold the code for loading the address
1978 // of the jump table, and jumping to it. Update successor information;
1979 // we will either branch to the default case for the switch, or the jump
1980 // table.
1981 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1982 CurMF->insert(BBI, JumpTableBB);
1983 CR.CaseBB->addSuccessor(Default);
1984 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001985
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001986 // Build a vector of destination BBs, corresponding to each target
1987 // of the jump table. If the value of the jump table slot corresponds to
1988 // a case statement, push the case's BB onto the vector, otherwise, push
1989 // the default BB.
1990 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001991 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001993 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1994 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001995
1996 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001997 DestBBs.push_back(I->BB);
1998 if (TEI==High)
1999 ++I;
2000 } else {
2001 DestBBs.push_back(Default);
2002 }
2003 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002005 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002006 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
2007 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002008 E = DestBBs.end(); I != E; ++I) {
2009 if (!SuccsHandled[(*I)->getNumber()]) {
2010 SuccsHandled[(*I)->getNumber()] = true;
2011 JumpTableBB->addSuccessor(*I);
2012 }
2013 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002014
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002015 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00002016 unsigned JTEncoding = TLI.getJumpTableEncoding();
2017 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002018 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002019
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002020 // Set the jump table information so that we can codegen it as a second
2021 // MachineBasicBlock
2022 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00002023 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
2024 if (CR.CaseBB == SwitchBB)
2025 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002026
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002027 JTCases.push_back(JumpTableBlock(JTH, JT));
2028
2029 return true;
2030}
2031
2032/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
2033/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00002034bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
2035 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002036 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002037 MachineBasicBlock *Default,
2038 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039 // Get the MachineFunction which holds the current MBB. This is used when
2040 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002041 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002042
2043 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002044 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002045 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002046
2047 Case& FrontCase = *CR.Range.first;
2048 Case& BackCase = *(CR.Range.second-1);
2049 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2050
2051 // Size is the number of Cases represented by this range.
2052 unsigned Size = CR.Range.second - CR.Range.first;
2053
Chris Lattnere880efe2009-11-07 07:50:34 +00002054 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2055 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002056 double FMetric = 0;
2057 CaseItr Pivot = CR.Range.first + Size/2;
2058
2059 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2060 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002061 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002062 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2063 I!=E; ++I)
2064 TSize += I->size();
2065
Chris Lattnere880efe2009-11-07 07:50:34 +00002066 APInt LSize = FrontCase.size();
2067 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002068 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002069 << "First: " << First << ", Last: " << Last <<'\n'
2070 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002071 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2072 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002073 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2074 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002075 APInt Range = ComputeRange(LEnd, RBegin);
2076 assert((Range - 2ULL).isNonNegative() &&
2077 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002078 // Use volatile double here to avoid excess precision issues on some hosts,
2079 // e.g. that use 80-bit X87 registers.
2080 volatile double LDensity =
2081 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002082 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002083 volatile double RDensity =
2084 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002085 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002086 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002087 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002088 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002089 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2090 << "LDensity: " << LDensity
2091 << ", RDensity: " << RDensity << '\n'
2092 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093 if (FMetric < Metric) {
2094 Pivot = J;
2095 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002096 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002097 }
2098
2099 LSize += J->size();
2100 RSize -= J->size();
2101 }
2102 if (areJTsAllowed(TLI)) {
2103 // If our case is dense we *really* should handle it earlier!
2104 assert((FMetric > 0) && "Should handle dense range earlier!");
2105 } else {
2106 Pivot = CR.Range.first + Size/2;
2107 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002109 CaseRange LHSR(CR.Range.first, Pivot);
2110 CaseRange RHSR(Pivot, CR.Range.second);
2111 Constant *C = Pivot->Low;
2112 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002113
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002114 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002115 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002116 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002117 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002118 // Pivot's Value, then we can branch directly to the LHS's Target,
2119 // rather than creating a leaf node for it.
2120 if ((LHSR.second - LHSR.first) == 1 &&
2121 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002122 cast<ConstantInt>(C)->getValue() ==
2123 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002124 TrueBB = LHSR.first->BB;
2125 } else {
2126 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2127 CurMF->insert(BBI, TrueBB);
2128 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002129
2130 // Put SV in a virtual register to make it available from the new blocks.
2131 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002132 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002134 // Similar to the optimization above, if the Value being switched on is
2135 // known to be less than the Constant CR.LT, and the current Case Value
2136 // is CR.LT - 1, then we can branch directly to the target block for
2137 // the current Case Value, rather than emitting a RHS leaf node for it.
2138 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002139 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2140 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141 FalseBB = RHSR.first->BB;
2142 } else {
2143 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2144 CurMF->insert(BBI, FalseBB);
2145 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002146
2147 // Put SV in a virtual register to make it available from the new blocks.
2148 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002149 }
2150
2151 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002152 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002153 // Otherwise, branch to LHS.
2154 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
2155
Dan Gohman99be8ae2010-04-19 22:41:47 +00002156 if (CR.CaseBB == SwitchBB)
2157 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002158 else
2159 SwitchCases.push_back(CB);
2160
2161 return true;
2162}
2163
2164/// handleBitTestsSwitchCase - if current case range has few destination and
2165/// range span less, than machine word bitwidth, encode case range into series
2166/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002167bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2168 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002169 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002170 MachineBasicBlock* Default,
2171 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00002172 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002173 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002174
2175 Case& FrontCase = *CR.Range.first;
2176 Case& BackCase = *(CR.Range.second-1);
2177
2178 // Get the MachineFunction which holds the current MBB. This is used when
2179 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002180 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002181
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002182 // If target does not have legal shift left, do not emit bit tests at all.
2183 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
2184 return false;
2185
Anton Korobeynikov23218582008-12-23 22:25:27 +00002186 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2188 I!=E; ++I) {
2189 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002190 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002191 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002192
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002193 // Count unique destinations
2194 SmallSet<MachineBasicBlock*, 4> Dests;
2195 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2196 Dests.insert(I->BB);
2197 if (Dests.size() > 3)
2198 // Don't bother the code below, if there are too much unique destinations
2199 return false;
2200 }
David Greene4b69d992010-01-05 01:24:57 +00002201 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002202 << Dests.size() << '\n'
2203 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002206 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2207 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002208 APInt cmpRange = maxValue - minValue;
2209
David Greene4b69d992010-01-05 01:24:57 +00002210 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002211 << "Low bound: " << minValue << '\n'
2212 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002213
Dan Gohmane0567812010-04-08 23:03:40 +00002214 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002215 (!(Dests.size() == 1 && numCmps >= 3) &&
2216 !(Dests.size() == 2 && numCmps >= 5) &&
2217 !(Dests.size() >= 3 && numCmps >= 6)))
2218 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002219
David Greene4b69d992010-01-05 01:24:57 +00002220 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002221 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223 // Optimize the case where all the case values fit in a
2224 // word without having to subtract minValue. In this case,
2225 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00002226 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002227 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002229 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002230 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002231
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002232 CaseBitsVector CasesBits;
2233 unsigned i, count = 0;
2234
2235 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2236 MachineBasicBlock* Dest = I->BB;
2237 for (i = 0; i < count; ++i)
2238 if (Dest == CasesBits[i].BB)
2239 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002240
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241 if (i == count) {
2242 assert((count < 3) && "Too much destinations to test!");
2243 CasesBits.push_back(CaseBits(0, Dest, 0));
2244 count++;
2245 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002246
2247 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2248 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2249
2250 uint64_t lo = (lowValue - lowBound).getZExtValue();
2251 uint64_t hi = (highValue - lowBound).getZExtValue();
2252
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002253 for (uint64_t j = lo; j <= hi; j++) {
2254 CasesBits[i].Mask |= 1ULL << j;
2255 CasesBits[i].Bits++;
2256 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002258 }
2259 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002261 BitTestInfo BTC;
2262
2263 // Figure out which block is immediately after the current one.
2264 MachineFunction::iterator BBI = CR.CaseBB;
2265 ++BBI;
2266
2267 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2268
David Greene4b69d992010-01-05 01:24:57 +00002269 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002270 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002271 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002272 << ", Bits: " << CasesBits[i].Bits
2273 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274
2275 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2276 CurMF->insert(BBI, CaseBB);
2277 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2278 CaseBB,
2279 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002280
2281 // Put SV in a virtual register to make it available from the new blocks.
2282 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002283 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002284
2285 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002286 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002287 CR.CaseBB, Default, BTC);
2288
Dan Gohman99be8ae2010-04-19 22:41:47 +00002289 if (CR.CaseBB == SwitchBB)
2290 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002291
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002292 BitTestCases.push_back(BTB);
2293
2294 return true;
2295}
2296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002297/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002298size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2299 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002300 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002301
2302 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002303 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002304 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2305 Cases.push_back(Case(SI.getSuccessorValue(i),
2306 SI.getSuccessorValue(i),
2307 SMBB));
2308 }
2309 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2310
2311 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002312 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002313 // Must recompute end() each iteration because it may be
2314 // invalidated by erase if we hold on to it
Nick Lewyckyed4efd32011-01-28 04:00:15 +00002315 for (CaseItr I = Cases.begin(), J = llvm::next(Cases.begin());
2316 J != Cases.end(); ) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002317 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2318 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002319 MachineBasicBlock* nextBB = J->BB;
2320 MachineBasicBlock* currentBB = I->BB;
2321
2322 // If the two neighboring cases go to the same destination, merge them
2323 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002324 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002325 I->High = J->High;
2326 J = Cases.erase(J);
2327 } else {
2328 I = J++;
2329 }
2330 }
2331
2332 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2333 if (I->Low != I->High)
2334 // A range counts double, since it requires two compares.
2335 ++numCmps;
2336 }
2337
2338 return numCmps;
2339}
2340
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002341void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2342 MachineBasicBlock *Last) {
2343 // Update JTCases.
2344 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2345 if (JTCases[i].first.HeaderBB == First)
2346 JTCases[i].first.HeaderBB = Last;
2347
2348 // Update BitTestCases.
2349 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2350 if (BitTestCases[i].Parent == First)
2351 BitTestCases[i].Parent = Last;
2352}
2353
Dan Gohman46510a72010-04-15 01:51:59 +00002354void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002355 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002356
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002357 // Figure out which block is immediately after the current one.
2358 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002359 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2360
2361 // If there is only the default destination, branch to it if it is not the
2362 // next basic block. Otherwise, just fall through.
2363 if (SI.getNumOperands() == 2) {
2364 // Update machine-CFG edges.
2365
2366 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002367 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002368 if (Default != NextBlock)
2369 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
2370 MVT::Other, getControlRoot(),
2371 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002372
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002373 return;
2374 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002375
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002376 // If there are any non-default case statements, create a vector of Cases
2377 // representing each one, and sort the vector so that we can efficiently
2378 // create a binary search tree from them.
2379 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002380 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002381 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002382 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002383 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002384
2385 // Get the Value to be switched on and default basic blocks, which will be
2386 // inserted into CaseBlock records, representing basic blocks in the binary
2387 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002388 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002389
2390 // Push the initial CaseRec onto the worklist
2391 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002392 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2393 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002394
2395 while (!WorkList.empty()) {
2396 // Grab a record representing a case range to process off the worklist
2397 CaseRec CR = WorkList.back();
2398 WorkList.pop_back();
2399
Dan Gohman99be8ae2010-04-19 22:41:47 +00002400 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002401 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002402
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002403 // If the range has few cases (two or less) emit a series of specific
2404 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002405 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002406 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002407
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002408 // If the switch has more than 5 blocks, and at least 40% dense, and the
2409 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002410 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002411 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002412 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002413
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002414 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2415 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002416 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002417 }
2418}
2419
Dan Gohman46510a72010-04-15 01:51:59 +00002420void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002421 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002422
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002423 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002424 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002425 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002426 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002427 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002428 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002429 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2430 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002431 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002432
Bill Wendling4533cac2010-01-28 21:51:40 +00002433 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2434 MVT::Other, getControlRoot(),
2435 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002436}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002437
Dan Gohman46510a72010-04-15 01:51:59 +00002438void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002439 // -0.0 - X --> fneg
2440 const Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002441 if (isa<Constant>(I.getOperand(0)) &&
2442 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2443 SDValue Op2 = getValue(I.getOperand(1));
2444 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2445 Op2.getValueType(), Op2));
2446 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002447 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002448
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002449 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002450}
2451
Dan Gohman46510a72010-04-15 01:51:59 +00002452void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002453 SDValue Op1 = getValue(I.getOperand(0));
2454 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002455 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2456 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002457}
2458
Dan Gohman46510a72010-04-15 01:51:59 +00002459void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002460 SDValue Op1 = getValue(I.getOperand(0));
2461 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002462
2463 MVT ShiftTy = TLI.getShiftAmountTy(Op2.getValueType());
2464
Chris Lattnerd3027732011-02-13 09:02:52 +00002465 // Coerce the shift amount to the right type if we can.
2466 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002467 unsigned ShiftSize = ShiftTy.getSizeInBits();
2468 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Chris Lattnerd3027732011-02-13 09:02:52 +00002469 DebugLoc DL = getCurDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002470
Dan Gohman57fc82d2009-04-09 03:51:29 +00002471 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002472 if (ShiftSize > Op2Size)
2473 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002474
Dan Gohman57fc82d2009-04-09 03:51:29 +00002475 // If the operand is larger than the shift count type but the shift
2476 // count type has enough bits to represent any shift value, truncate
2477 // it now. This is a common case and it exposes the truncate to
2478 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002479 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2480 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2481 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002482 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002483 else
Chris Lattnere0751182011-02-13 19:09:16 +00002484 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002485 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002486
Bill Wendling4533cac2010-01-28 21:51:40 +00002487 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2488 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002489}
2490
Dan Gohman46510a72010-04-15 01:51:59 +00002491void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002493 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002494 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002495 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002496 predicate = ICmpInst::Predicate(IC->getPredicate());
2497 SDValue Op1 = getValue(I.getOperand(0));
2498 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002499 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002500
Owen Andersone50ed302009-08-10 22:56:29 +00002501 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002502 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002503}
2504
Dan Gohman46510a72010-04-15 01:51:59 +00002505void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002506 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002507 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002508 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002509 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002510 predicate = FCmpInst::Predicate(FC->getPredicate());
2511 SDValue Op1 = getValue(I.getOperand(0));
2512 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002513 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002514 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002515 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002516}
2517
Dan Gohman46510a72010-04-15 01:51:59 +00002518void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002519 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002520 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2521 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002522 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002523
Bill Wendling49fcff82009-12-21 22:30:11 +00002524 SmallVector<SDValue, 4> Values(NumValues);
2525 SDValue Cond = getValue(I.getOperand(0));
2526 SDValue TrueVal = getValue(I.getOperand(1));
2527 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002528
Bill Wendling4533cac2010-01-28 21:51:40 +00002529 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002530 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002531 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2532 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002533 SDValue(TrueVal.getNode(),
2534 TrueVal.getResNo() + i),
2535 SDValue(FalseVal.getNode(),
2536 FalseVal.getResNo() + i));
2537
Bill Wendling4533cac2010-01-28 21:51:40 +00002538 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2539 DAG.getVTList(&ValueVTs[0], NumValues),
2540 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002541}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002542
Dan Gohman46510a72010-04-15 01:51:59 +00002543void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002544 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2545 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002546 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002547 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002548}
2549
Dan Gohman46510a72010-04-15 01:51:59 +00002550void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002551 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2552 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2553 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002554 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002555 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002556}
2557
Dan Gohman46510a72010-04-15 01:51:59 +00002558void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002559 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2560 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2561 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002562 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002563 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002564}
2565
Dan Gohman46510a72010-04-15 01:51:59 +00002566void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002567 // FPTrunc is never a no-op cast, no need to check
2568 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002569 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002570 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2571 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002572}
2573
Dan Gohman46510a72010-04-15 01:51:59 +00002574void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002575 // FPTrunc is never a no-op cast, no need to check
2576 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002577 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002578 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002579}
2580
Dan Gohman46510a72010-04-15 01:51:59 +00002581void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002582 // FPToUI is never a no-op cast, no need to check
2583 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002584 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002585 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002586}
2587
Dan Gohman46510a72010-04-15 01:51:59 +00002588void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002589 // FPToSI is never a no-op cast, no need to check
2590 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002591 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002592 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002593}
2594
Dan Gohman46510a72010-04-15 01:51:59 +00002595void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002596 // UIToFP is never a no-op cast, no need to check
2597 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002598 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002599 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002600}
2601
Dan Gohman46510a72010-04-15 01:51:59 +00002602void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002603 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002604 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002605 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002606 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002607}
2608
Dan Gohman46510a72010-04-15 01:51:59 +00002609void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002610 // What to do depends on the size of the integer and the size of the pointer.
2611 // We can either truncate, zero extend, or no-op, accordingly.
2612 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002613 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002614 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002615}
2616
Dan Gohman46510a72010-04-15 01:51:59 +00002617void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002618 // What to do depends on the size of the integer and the size of the pointer.
2619 // We can either truncate, zero extend, or no-op, accordingly.
2620 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002621 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002622 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002623}
2624
Dan Gohman46510a72010-04-15 01:51:59 +00002625void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002626 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002627 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002628
Bill Wendling49fcff82009-12-21 22:30:11 +00002629 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002630 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002631 if (DestVT != N.getValueType())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002632 setValue(&I, DAG.getNode(ISD::BITCAST, getCurDebugLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002633 DestVT, N)); // convert types.
2634 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002635 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002636}
2637
Dan Gohman46510a72010-04-15 01:51:59 +00002638void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002639 SDValue InVec = getValue(I.getOperand(0));
2640 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002641 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002642 TLI.getPointerTy(),
2643 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002644 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2645 TLI.getValueType(I.getType()),
2646 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002647}
2648
Dan Gohman46510a72010-04-15 01:51:59 +00002649void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002650 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002651 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002652 TLI.getPointerTy(),
2653 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002654 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2655 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002656}
2657
Mon P Wangaeb06d22008-11-10 04:46:22 +00002658// Utility for visitShuffleVector - Returns true if the mask is mask starting
2659// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002660static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2661 unsigned MaskNumElts = Mask.size();
2662 for (unsigned i = 0; i != MaskNumElts; ++i)
2663 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002664 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002665 return true;
2666}
2667
Dan Gohman46510a72010-04-15 01:51:59 +00002668void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002669 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002670 SDValue Src1 = getValue(I.getOperand(0));
2671 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002672
Nate Begeman9008ca62009-04-27 18:41:29 +00002673 // Convert the ConstantVector mask operand into an array of ints, with -1
2674 // representing undef values.
2675 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002676 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002677 unsigned MaskNumElts = MaskElts.size();
2678 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002679 if (isa<UndefValue>(MaskElts[i]))
2680 Mask.push_back(-1);
2681 else
2682 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2683 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002684
Owen Andersone50ed302009-08-10 22:56:29 +00002685 EVT VT = TLI.getValueType(I.getType());
2686 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002687 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002688
Mon P Wangc7849c22008-11-16 05:06:27 +00002689 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002690 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2691 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002692 return;
2693 }
2694
2695 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002696 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2697 // Mask is longer than the source vectors and is a multiple of the source
2698 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002699 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002700 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2701 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002702 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2703 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002704 return;
2705 }
2706
Mon P Wangc7849c22008-11-16 05:06:27 +00002707 // Pad both vectors with undefs to make them the same length as the mask.
2708 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002709 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2710 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002711 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002712
Nate Begeman9008ca62009-04-27 18:41:29 +00002713 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2714 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002715 MOps1[0] = Src1;
2716 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002717
2718 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2719 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002720 &MOps1[0], NumConcat);
2721 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002722 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002723 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002724
Mon P Wangaeb06d22008-11-10 04:46:22 +00002725 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002726 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002727 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002728 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002729 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002730 MappedOps.push_back(Idx);
2731 else
2732 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002733 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002734
Bill Wendling4533cac2010-01-28 21:51:40 +00002735 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2736 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002737 return;
2738 }
2739
Mon P Wangc7849c22008-11-16 05:06:27 +00002740 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002741 // Analyze the access pattern of the vector to see if we can extract
2742 // two subvectors and do the shuffle. The analysis is done by calculating
2743 // the range of elements the mask access on both vectors.
2744 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2745 int MaxRange[2] = {-1, -1};
2746
Nate Begeman5a5ca152009-04-29 05:20:52 +00002747 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002748 int Idx = Mask[i];
2749 int Input = 0;
2750 if (Idx < 0)
2751 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002752
Nate Begeman5a5ca152009-04-29 05:20:52 +00002753 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002754 Input = 1;
2755 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002756 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002757 if (Idx > MaxRange[Input])
2758 MaxRange[Input] = Idx;
2759 if (Idx < MinRange[Input])
2760 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002761 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002762
Mon P Wangc7849c22008-11-16 05:06:27 +00002763 // Check if the access is smaller than the vector size and can we find
2764 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002765 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2766 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002767 int StartIdx[2]; // StartIdx to extract from
2768 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002769 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002770 RangeUse[Input] = 0; // Unused
2771 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002772 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002773 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002774 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002775 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002776 RangeUse[Input] = 1; // Extract from beginning of the vector
2777 StartIdx[Input] = 0;
2778 } else {
2779 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002780 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Bob Wilson5e8b8332011-01-07 04:59:04 +00002781 StartIdx[Input] + MaskNumElts <= SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002782 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002783 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002784 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002785 }
2786
Bill Wendling636e2582009-08-21 18:16:06 +00002787 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002788 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002789 return;
2790 }
2791 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2792 // Extract appropriate subvector and generate a vector shuffle
2793 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002794 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002795 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002796 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002797 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002798 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002799 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002800 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002801
Mon P Wangc7849c22008-11-16 05:06:27 +00002802 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002803 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002804 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002805 int Idx = Mask[i];
2806 if (Idx < 0)
2807 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002808 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002809 MappedOps.push_back(Idx - StartIdx[0]);
2810 else
2811 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002812 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002813
Bill Wendling4533cac2010-01-28 21:51:40 +00002814 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2815 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002816 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002817 }
2818 }
2819
Mon P Wangc7849c22008-11-16 05:06:27 +00002820 // We can't use either concat vectors or extract subvectors so fall back to
2821 // replacing the shuffle with extract and build vector.
2822 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002823 EVT EltVT = VT.getVectorElementType();
2824 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002825 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002826 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002827 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002828 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002829 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002830 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002831 SDValue Res;
2832
Nate Begeman5a5ca152009-04-29 05:20:52 +00002833 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002834 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2835 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002836 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002837 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2838 EltVT, Src2,
2839 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2840
2841 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002842 }
2843 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002844
Bill Wendling4533cac2010-01-28 21:51:40 +00002845 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2846 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847}
2848
Dan Gohman46510a72010-04-15 01:51:59 +00002849void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002850 const Value *Op0 = I.getOperand(0);
2851 const Value *Op1 = I.getOperand(1);
2852 const Type *AggTy = I.getType();
2853 const Type *ValTy = Op1->getType();
2854 bool IntoUndef = isa<UndefValue>(Op0);
2855 bool FromUndef = isa<UndefValue>(Op1);
2856
Dan Gohman0dadb152010-10-06 16:18:29 +00002857 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.idx_begin(), I.idx_end());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002858
Owen Andersone50ed302009-08-10 22:56:29 +00002859 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002860 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002861 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002862 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2863
2864 unsigned NumAggValues = AggValueVTs.size();
2865 unsigned NumValValues = ValValueVTs.size();
2866 SmallVector<SDValue, 4> Values(NumAggValues);
2867
2868 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869 unsigned i = 0;
2870 // Copy the beginning value(s) from the original aggregate.
2871 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002872 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002873 SDValue(Agg.getNode(), Agg.getResNo() + i);
2874 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00002875 if (NumValValues) {
2876 SDValue Val = getValue(Op1);
2877 for (; i != LinearIndex + NumValValues; ++i)
2878 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
2879 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2880 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881 // Copy remaining value(s) from the original aggregate.
2882 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002883 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002884 SDValue(Agg.getNode(), Agg.getResNo() + i);
2885
Bill Wendling4533cac2010-01-28 21:51:40 +00002886 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2887 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2888 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002889}
2890
Dan Gohman46510a72010-04-15 01:51:59 +00002891void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002892 const Value *Op0 = I.getOperand(0);
2893 const Type *AggTy = Op0->getType();
2894 const Type *ValTy = I.getType();
2895 bool OutOfUndef = isa<UndefValue>(Op0);
2896
Dan Gohman0dadb152010-10-06 16:18:29 +00002897 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.idx_begin(), I.idx_end());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002898
Owen Andersone50ed302009-08-10 22:56:29 +00002899 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2901
2902 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00002903
2904 // Ignore a extractvalue that produces an empty object
2905 if (!NumValValues) {
2906 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
2907 return;
2908 }
2909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002910 SmallVector<SDValue, 4> Values(NumValValues);
2911
2912 SDValue Agg = getValue(Op0);
2913 // Copy out the selected value(s).
2914 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2915 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002916 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002917 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002918 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002919
Bill Wendling4533cac2010-01-28 21:51:40 +00002920 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2921 DAG.getVTList(&ValValueVTs[0], NumValValues),
2922 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002923}
2924
Dan Gohman46510a72010-04-15 01:51:59 +00002925void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002926 SDValue N = getValue(I.getOperand(0));
2927 const Type *Ty = I.getOperand(0)->getType();
2928
Dan Gohman46510a72010-04-15 01:51:59 +00002929 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002930 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002931 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002932 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2933 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2934 if (Field) {
2935 // N = N + Offset
2936 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002937 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002938 DAG.getIntPtrConstant(Offset));
2939 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002940
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002941 Ty = StTy->getElementType(Field);
2942 } else {
2943 Ty = cast<SequentialType>(Ty)->getElementType();
2944
2945 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002946 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00002947 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002948 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002949 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002950 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002951 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002952 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002953 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002954 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2955 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002957 else
Evan Chengb1032a82009-02-09 20:54:38 +00002958 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002959
Dale Johannesen66978ee2009-01-31 02:22:37 +00002960 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002961 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002962 continue;
2963 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002965 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002966 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2967 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002968 SDValue IdxN = getValue(Idx);
2969
2970 // If the index is smaller or larger than intptr_t, truncate or extend
2971 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002972 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002973
2974 // If this is a multiply by a power of two, turn it into a shl
2975 // immediately. This is a very common case.
2976 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002977 if (ElementSize.isPowerOf2()) {
2978 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002979 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002980 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002981 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002982 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002983 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002984 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002985 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002986 }
2987 }
2988
Scott Michelfdc40a02009-02-17 22:15:04 +00002989 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002990 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002991 }
2992 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002993
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002994 setValue(&I, N);
2995}
2996
Dan Gohman46510a72010-04-15 01:51:59 +00002997void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002998 // If this is a fixed sized alloca in the entry block of the function,
2999 // allocate it statically on the stack.
3000 if (FuncInfo.StaticAllocaMap.count(&I))
3001 return; // getValue will auto-populate this.
3002
3003 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00003004 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003005 unsigned Align =
3006 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
3007 I.getAlignment());
3008
3009 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003010
Owen Andersone50ed302009-08-10 22:56:29 +00003011 EVT IntPtr = TLI.getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003012 if (AllocSize.getValueType() != IntPtr)
3013 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
3014
3015 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr,
3016 AllocSize,
3017 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003018
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003019 // Handle alignment. If the requested alignment is less than or equal to
3020 // the stack alignment, ignore it. If the size is greater than or equal to
3021 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003022 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003023 if (Align <= StackAlign)
3024 Align = 0;
3025
3026 // Round the size of the allocation up to the stack alignment size
3027 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00003028 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003029 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003030 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003031
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003032 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00003033 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003034 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003035 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3036
3037 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003038 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00003039 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003040 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003041 setValue(&I, DSA);
3042 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003043
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003044 // Inform the Frame Information that we have just allocated a variable-sized
3045 // object.
Eric Christopher2b8271e2010-07-17 00:28:22 +00003046 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003047}
3048
Dan Gohman46510a72010-04-15 01:51:59 +00003049void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003050 const Value *SV = I.getOperand(0);
3051 SDValue Ptr = getValue(SV);
3052
3053 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003055 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003056 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003057 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003058 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003059
Owen Andersone50ed302009-08-10 22:56:29 +00003060 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003061 SmallVector<uint64_t, 4> Offsets;
3062 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
3063 unsigned NumValues = ValueVTs.size();
3064 if (NumValues == 0)
3065 return;
3066
3067 SDValue Root;
3068 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003069 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003070 // Serialize volatile loads with other side effects.
3071 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003072 else if (AA->pointsToConstantMemory(
3073 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003074 // Do not serialize (non-volatile) loads of constant memory with anything.
3075 Root = DAG.getEntryNode();
3076 ConstantMemory = true;
3077 } else {
3078 // Do not serialize non-volatile loads against each other.
3079 Root = DAG.getRoot();
3080 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003082 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003083 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3084 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003085 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003086 unsigned ChainI = 0;
3087 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3088 // Serializing loads here may result in excessive register pressure, and
3089 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3090 // could recover a bit by hoisting nodes upward in the chain by recognizing
3091 // they are side-effect free or do not alias. The optimizer should really
3092 // avoid this case by converting large object/array copies to llvm.memcpy
3093 // (MaxParallelChains should always remain as failsafe).
3094 if (ChainI == MaxParallelChains) {
3095 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
3096 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3097 MVT::Other, &Chains[0], ChainI);
3098 Root = Chain;
3099 ChainI = 0;
3100 }
Bill Wendling856ff412009-12-22 00:12:37 +00003101 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
3102 PtrVT, Ptr,
3103 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003104 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003105 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003106 isNonTemporal, Alignment, TBAAInfo);
Bill Wendling856ff412009-12-22 00:12:37 +00003107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003108 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003109 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003110 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003112 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003113 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003114 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003115 if (isVolatile)
3116 DAG.setRoot(Chain);
3117 else
3118 PendingLoads.push_back(Chain);
3119 }
3120
Bill Wendling4533cac2010-01-28 21:51:40 +00003121 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
3122 DAG.getVTList(&ValueVTs[0], NumValues),
3123 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003124}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003125
Dan Gohman46510a72010-04-15 01:51:59 +00003126void SelectionDAGBuilder::visitStore(const StoreInst &I) {
3127 const Value *SrcV = I.getOperand(0);
3128 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003129
Owen Andersone50ed302009-08-10 22:56:29 +00003130 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003131 SmallVector<uint64_t, 4> Offsets;
3132 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3133 unsigned NumValues = ValueVTs.size();
3134 if (NumValues == 0)
3135 return;
3136
3137 // Get the lowered operands. Note that we do this after
3138 // checking if NumResults is zero, because with zero results
3139 // the operands won't have values in the map.
3140 SDValue Src = getValue(SrcV);
3141 SDValue Ptr = getValue(PtrV);
3142
3143 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003144 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3145 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003146 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003147 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003148 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003149 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003150 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003151
Andrew Trickde91f3c2010-11-12 17:50:46 +00003152 unsigned ChainI = 0;
3153 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3154 // See visitLoad comments.
3155 if (ChainI == MaxParallelChains) {
3156 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3157 MVT::Other, &Chains[0], ChainI);
3158 Root = Chain;
3159 ChainI = 0;
3160 }
Bill Wendling856ff412009-12-22 00:12:37 +00003161 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3162 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickde91f3c2010-11-12 17:50:46 +00003163 SDValue St = DAG.getStore(Root, getCurDebugLoc(),
3164 SDValue(Src.getNode(), Src.getResNo() + i),
3165 Add, MachinePointerInfo(PtrV, Offsets[i]),
3166 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3167 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003168 }
3169
Devang Patel7e13efa2010-10-26 22:14:52 +00003170 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003171 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003172 ++SDNodeOrder;
3173 AssignOrderingToNode(StoreNode.getNode());
3174 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003175}
3176
3177/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3178/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003179void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003180 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003181 bool HasChain = !I.doesNotAccessMemory();
3182 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3183
3184 // Build the operand list.
3185 SmallVector<SDValue, 8> Ops;
3186 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3187 if (OnlyLoad) {
3188 // We don't need to serialize loads against other loads.
3189 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003190 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003191 Ops.push_back(getRoot());
3192 }
3193 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003194
3195 // Info is set by getTgtMemInstrinsic
3196 TargetLowering::IntrinsicInfo Info;
3197 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3198
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003199 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003200 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3201 Info.opc == ISD::INTRINSIC_W_CHAIN)
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003202 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003203
3204 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003205 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3206 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003207 assert(TLI.isTypeLegal(Op.getValueType()) &&
3208 "Intrinsic uses a non-legal type?");
3209 Ops.push_back(Op);
3210 }
3211
Owen Andersone50ed302009-08-10 22:56:29 +00003212 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003213 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3214#ifndef NDEBUG
3215 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3216 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3217 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003218 }
Bob Wilson8d919552009-07-31 22:41:21 +00003219#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003220
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003221 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003223
Bob Wilson8d919552009-07-31 22:41:21 +00003224 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003225
3226 // Create the node.
3227 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003228 if (IsTgtIntrinsic) {
3229 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003230 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003231 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003232 Info.memVT,
3233 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003234 Info.align, Info.vol,
3235 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003236 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003237 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003238 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003239 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003240 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003241 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003242 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003243 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003244 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003245 }
3246
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003247 if (HasChain) {
3248 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3249 if (OnlyLoad)
3250 PendingLoads.push_back(Chain);
3251 else
3252 DAG.setRoot(Chain);
3253 }
Bill Wendling856ff412009-12-22 00:12:37 +00003254
Benjamin Kramerf0127052010-01-05 13:12:22 +00003255 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003256 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003257 EVT VT = TLI.getValueType(PTy);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003258 Result = DAG.getNode(ISD::BITCAST, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003259 }
Bill Wendling856ff412009-12-22 00:12:37 +00003260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003261 setValue(&I, Result);
3262 }
3263}
3264
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003265/// GetSignificand - Get the significand and build it into a floating-point
3266/// number with exponent of 1:
3267///
3268/// Op = (Op & 0x007fffff) | 0x3f800000;
3269///
3270/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003271static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00003272GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003273 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3274 DAG.getConstant(0x007fffff, MVT::i32));
3275 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3276 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003277 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003278}
3279
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003280/// GetExponent - Get the exponent:
3281///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003282/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003283///
3284/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003285static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003286GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00003287 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003288 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3289 DAG.getConstant(0x7f800000, MVT::i32));
3290 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003291 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3293 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003294 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003295}
3296
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003297/// getF32Constant - Get 32-bit floating point constant.
3298static SDValue
3299getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003301}
3302
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003303/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003304/// visitIntrinsicCall: I is a call instruction
3305/// Op is the associated NodeType for I
3306const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003307SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
3308 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003309 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003310 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003311 DAG.getAtomic(Op, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00003312 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003313 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00003314 getValue(I.getArgOperand(0)),
3315 getValue(I.getArgOperand(1)),
3316 I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003317 setValue(&I, L);
3318 DAG.setRoot(L.getValue(1));
3319 return 0;
3320}
3321
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003322// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003323const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003324SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Gabor Greif0635f352010-06-25 09:38:13 +00003325 SDValue Op1 = getValue(I.getArgOperand(0));
3326 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling74c37652008-12-09 22:08:41 +00003327
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00003329 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003330 return 0;
3331}
Bill Wendling74c37652008-12-09 22:08:41 +00003332
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003333/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3334/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003335void
Dan Gohman46510a72010-04-15 01:51:59 +00003336SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003337 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003338 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003339
Gabor Greif0635f352010-06-25 09:38:13 +00003340 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003341 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003342 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003343
3344 // Put the exponent in the right bit position for later addition to the
3345 // final result:
3346 //
3347 // #define LOG2OFe 1.4426950f
3348 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003350 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003351 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003352
3353 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003354 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3355 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003356
3357 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003359 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003360
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003361 if (LimitFloatPrecision <= 6) {
3362 // For floating-point precision of 6:
3363 //
3364 // TwoToFractionalPartOfX =
3365 // 0.997535578f +
3366 // (0.735607626f + 0.252464424f * x) * x;
3367 //
3368 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003370 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003371 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003372 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3374 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003375 getF32Constant(DAG, 0x3f7f5e7e));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003376 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BITCAST, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003377
3378 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003380 TwoToFracPartOfX, IntegerPartOfX);
3381
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003382 result = DAG.getNode(ISD::BITCAST, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003383 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3384 // For floating-point precision of 12:
3385 //
3386 // TwoToFractionalPartOfX =
3387 // 0.999892986f +
3388 // (0.696457318f +
3389 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3390 //
3391 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003393 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003394 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003395 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3397 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003398 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3400 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003401 getF32Constant(DAG, 0x3f7ff8fd));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003402 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BITCAST, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003403
3404 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003406 TwoToFracPartOfX, IntegerPartOfX);
3407
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003408 result = DAG.getNode(ISD::BITCAST, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003409 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3410 // For floating-point precision of 18:
3411 //
3412 // TwoToFractionalPartOfX =
3413 // 0.999999982f +
3414 // (0.693148872f +
3415 // (0.240227044f +
3416 // (0.554906021e-1f +
3417 // (0.961591928e-2f +
3418 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3419 //
3420 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003422 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003424 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3426 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003427 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003428 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3429 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003430 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003431 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3432 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003433 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3435 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003436 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3438 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003439 getF32Constant(DAG, 0x3f800000));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003440 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003442
3443 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003445 TwoToFracPartOfX, IntegerPartOfX);
3446
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003447 result = DAG.getNode(ISD::BITCAST, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003448 }
3449 } else {
3450 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003451 result = DAG.getNode(ISD::FEXP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003452 getValue(I.getArgOperand(0)).getValueType(),
3453 getValue(I.getArgOperand(0)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003454 }
3455
Dale Johannesen59e577f2008-09-05 18:38:42 +00003456 setValue(&I, result);
3457}
3458
Bill Wendling39150252008-09-09 20:39:27 +00003459/// visitLog - Lower a log intrinsic. Handles the special sequences for
3460/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003461void
Dan Gohman46510a72010-04-15 01:51:59 +00003462SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003463 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003464 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003465
Gabor Greif0635f352010-06-25 09:38:13 +00003466 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003467 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003468 SDValue Op = getValue(I.getArgOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003469 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003470
3471 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003472 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003473 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003474 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003475
3476 // Get the significand and build it into a floating-point number with
3477 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003478 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003479
3480 if (LimitFloatPrecision <= 6) {
3481 // For floating-point precision of 6:
3482 //
3483 // LogofMantissa =
3484 // -1.1609546f +
3485 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003486 //
Bill Wendling39150252008-09-09 20:39:27 +00003487 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003488 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003489 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003490 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003491 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003492 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3493 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003494 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003495
Scott Michelfdc40a02009-02-17 22:15:04 +00003496 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003498 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3499 // For floating-point precision of 12:
3500 //
3501 // LogOfMantissa =
3502 // -1.7417939f +
3503 // (2.8212026f +
3504 // (-1.4699568f +
3505 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3506 //
3507 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003509 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003510 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003511 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3513 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003514 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3516 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003517 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003518 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3519 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003520 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003521
Scott Michelfdc40a02009-02-17 22:15:04 +00003522 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003524 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3525 // For floating-point precision of 18:
3526 //
3527 // LogOfMantissa =
3528 // -2.1072184f +
3529 // (4.2372794f +
3530 // (-3.7029485f +
3531 // (2.2781945f +
3532 // (-0.87823314f +
3533 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3534 //
3535 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003537 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003539 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3541 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003542 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3544 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003545 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3547 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003548 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3550 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003551 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3553 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003554 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003555
Scott Michelfdc40a02009-02-17 22:15:04 +00003556 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003558 }
3559 } else {
3560 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003561 result = DAG.getNode(ISD::FLOG, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003562 getValue(I.getArgOperand(0)).getValueType(),
3563 getValue(I.getArgOperand(0)));
Bill Wendling39150252008-09-09 20:39:27 +00003564 }
3565
Dale Johannesen59e577f2008-09-05 18:38:42 +00003566 setValue(&I, result);
3567}
3568
Bill Wendling3eb59402008-09-09 00:28:24 +00003569/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3570/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003571void
Dan Gohman46510a72010-04-15 01:51:59 +00003572SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003573 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003574 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003575
Gabor Greif0635f352010-06-25 09:38:13 +00003576 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003577 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003578 SDValue Op = getValue(I.getArgOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003579 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003580
Bill Wendling39150252008-09-09 20:39:27 +00003581 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003582 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003583
Bill Wendling3eb59402008-09-09 00:28:24 +00003584 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003585 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003586 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003587
Bill Wendling3eb59402008-09-09 00:28:24 +00003588 // Different possible minimax approximations of significand in
3589 // floating-point for various degrees of accuracy over [1,2].
3590 if (LimitFloatPrecision <= 6) {
3591 // For floating-point precision of 6:
3592 //
3593 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3594 //
3595 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003596 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003597 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003598 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003599 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3601 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003602 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003603
Scott Michelfdc40a02009-02-17 22:15:04 +00003604 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003605 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003606 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3607 // For floating-point precision of 12:
3608 //
3609 // Log2ofMantissa =
3610 // -2.51285454f +
3611 // (4.07009056f +
3612 // (-2.12067489f +
3613 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003614 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003615 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003616 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003617 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003618 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003619 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003620 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3621 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003622 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003623 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3624 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003625 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003626 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3627 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003628 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003629
Scott Michelfdc40a02009-02-17 22:15:04 +00003630 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003631 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003632 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3633 // For floating-point precision of 18:
3634 //
3635 // Log2ofMantissa =
3636 // -3.0400495f +
3637 // (6.1129976f +
3638 // (-5.3420409f +
3639 // (3.2865683f +
3640 // (-1.2669343f +
3641 // (0.27515199f -
3642 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3643 //
3644 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003645 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003646 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003648 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003649 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3650 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003651 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003652 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3653 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003654 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3656 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003657 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003658 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3659 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003660 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3662 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003663 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003664
Scott Michelfdc40a02009-02-17 22:15:04 +00003665 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003666 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003667 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003668 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003669 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003670 result = DAG.getNode(ISD::FLOG2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003671 getValue(I.getArgOperand(0)).getValueType(),
3672 getValue(I.getArgOperand(0)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003673 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003674
Dale Johannesen59e577f2008-09-05 18:38:42 +00003675 setValue(&I, result);
3676}
3677
Bill Wendling3eb59402008-09-09 00:28:24 +00003678/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3679/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003680void
Dan Gohman46510a72010-04-15 01:51:59 +00003681SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003682 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003683 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003684
Gabor Greif0635f352010-06-25 09:38:13 +00003685 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003686 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003687 SDValue Op = getValue(I.getArgOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003688 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003689
Bill Wendling39150252008-09-09 20:39:27 +00003690 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003691 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003692 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003693 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003694
3695 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003696 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003697 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003698
3699 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003700 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003701 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003702 // Log10ofMantissa =
3703 // -0.50419619f +
3704 // (0.60948995f - 0.10380950f * x) * x;
3705 //
3706 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003707 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003708 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003709 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003710 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003711 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3712 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003713 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003714
Scott Michelfdc40a02009-02-17 22:15:04 +00003715 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003717 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3718 // For floating-point precision of 12:
3719 //
3720 // Log10ofMantissa =
3721 // -0.64831180f +
3722 // (0.91751397f +
3723 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3724 //
3725 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003726 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003727 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003728 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003729 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003730 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3731 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003732 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003733 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3734 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003735 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003736
Scott Michelfdc40a02009-02-17 22:15:04 +00003737 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003738 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003739 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003740 // For floating-point precision of 18:
3741 //
3742 // Log10ofMantissa =
3743 // -0.84299375f +
3744 // (1.5327582f +
3745 // (-1.0688956f +
3746 // (0.49102474f +
3747 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3748 //
3749 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003750 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003751 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003752 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003753 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003754 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3755 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003756 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003757 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3758 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003759 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003760 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3761 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003762 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003763 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3764 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003765 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003766
Scott Michelfdc40a02009-02-17 22:15:04 +00003767 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003769 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003770 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003771 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003772 result = DAG.getNode(ISD::FLOG10, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003773 getValue(I.getArgOperand(0)).getValueType(),
3774 getValue(I.getArgOperand(0)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003775 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003776
Dale Johannesen59e577f2008-09-05 18:38:42 +00003777 setValue(&I, result);
3778}
3779
Bill Wendlinge10c8142008-09-09 22:39:21 +00003780/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3781/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003782void
Dan Gohman46510a72010-04-15 01:51:59 +00003783SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003784 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003785 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003786
Gabor Greif0635f352010-06-25 09:38:13 +00003787 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003788 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003789 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003790
Owen Anderson825b72b2009-08-11 20:47:22 +00003791 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003792
3793 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3795 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003796
3797 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003799 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003800
3801 if (LimitFloatPrecision <= 6) {
3802 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003803 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003804 // TwoToFractionalPartOfX =
3805 // 0.997535578f +
3806 // (0.735607626f + 0.252464424f * x) * x;
3807 //
3808 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003809 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003810 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003812 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3814 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003815 getF32Constant(DAG, 0x3f7f5e7e));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003816 SDValue t6 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003817 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003818 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003819
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003820 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003821 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003822 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3823 // For floating-point precision of 12:
3824 //
3825 // TwoToFractionalPartOfX =
3826 // 0.999892986f +
3827 // (0.696457318f +
3828 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3829 //
3830 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003831 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003832 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003833 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003834 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003835 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3836 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003837 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003838 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3839 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003840 getF32Constant(DAG, 0x3f7ff8fd));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003841 SDValue t8 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003842 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003843 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003844
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003845 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003846 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003847 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3848 // For floating-point precision of 18:
3849 //
3850 // TwoToFractionalPartOfX =
3851 // 0.999999982f +
3852 // (0.693148872f +
3853 // (0.240227044f +
3854 // (0.554906021e-1f +
3855 // (0.961591928e-2f +
3856 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3857 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003858 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003859 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003860 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003861 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003862 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3863 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003864 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003865 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3866 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003867 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3869 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003870 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3872 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003873 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003874 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3875 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003876 getF32Constant(DAG, 0x3f800000));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003877 SDValue t14 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003878 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003879 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003880
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003881 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003883 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003884 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003885 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003886 result = DAG.getNode(ISD::FEXP2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003887 getValue(I.getArgOperand(0)).getValueType(),
3888 getValue(I.getArgOperand(0)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003889 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003890
Dale Johannesen601d3c02008-09-05 01:48:15 +00003891 setValue(&I, result);
3892}
3893
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003894/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3895/// limited-precision mode with x == 10.0f.
3896void
Dan Gohman46510a72010-04-15 01:51:59 +00003897SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003898 SDValue result;
Gabor Greif0635f352010-06-25 09:38:13 +00003899 const Value *Val = I.getArgOperand(0);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003900 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003901 bool IsExp10 = false;
3902
Owen Anderson825b72b2009-08-11 20:47:22 +00003903 if (getValue(Val).getValueType() == MVT::f32 &&
Gabor Greif0635f352010-06-25 09:38:13 +00003904 getValue(I.getArgOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003905 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3906 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3907 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3908 APFloat Ten(10.0f);
3909 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3910 }
3911 }
3912 }
3913
3914 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003915 SDValue Op = getValue(I.getArgOperand(1));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003916
3917 // Put the exponent in the right bit position for later addition to the
3918 // final result:
3919 //
3920 // #define LOG2OF10 3.3219281f
3921 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003923 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003924 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003925
3926 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003927 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3928 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003929
3930 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003931 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003932 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003933
3934 if (LimitFloatPrecision <= 6) {
3935 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003936 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003937 // twoToFractionalPartOfX =
3938 // 0.997535578f +
3939 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003940 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003941 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003943 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003944 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003945 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003946 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3947 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003948 getF32Constant(DAG, 0x3f7f5e7e));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003949 SDValue t6 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003950 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003952
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003953 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003955 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3956 // For floating-point precision of 12:
3957 //
3958 // TwoToFractionalPartOfX =
3959 // 0.999892986f +
3960 // (0.696457318f +
3961 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3962 //
3963 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003965 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003966 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003967 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003968 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3969 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003970 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003971 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3972 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003973 getF32Constant(DAG, 0x3f7ff8fd));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003974 SDValue t8 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003975 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003976 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003977
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003978 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003979 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003980 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3981 // For floating-point precision of 18:
3982 //
3983 // TwoToFractionalPartOfX =
3984 // 0.999999982f +
3985 // (0.693148872f +
3986 // (0.240227044f +
3987 // (0.554906021e-1f +
3988 // (0.961591928e-2f +
3989 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3990 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003991 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003992 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003993 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003994 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003995 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3996 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003997 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003998 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3999 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004000 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004001 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4002 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004003 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004004 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4005 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004006 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004007 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4008 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004009 getF32Constant(DAG, 0x3f800000));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004010 SDValue t14 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004011 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004012 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004013
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004014 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004015 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004016 }
4017 } else {
4018 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004019 result = DAG.getNode(ISD::FPOW, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004020 getValue(I.getArgOperand(0)).getValueType(),
4021 getValue(I.getArgOperand(0)),
4022 getValue(I.getArgOperand(1)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004023 }
4024
4025 setValue(&I, result);
4026}
4027
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004028
4029/// ExpandPowI - Expand a llvm.powi intrinsic.
4030static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
4031 SelectionDAG &DAG) {
4032 // If RHS is a constant, we can expand this out to a multiplication tree,
4033 // otherwise we end up lowering to a call to __powidf2 (for example). When
4034 // optimizing for size, we only want to do this if the expansion would produce
4035 // a small number of multiplies, otherwise we do the full expansion.
4036 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4037 // Get the exponent as a positive value.
4038 unsigned Val = RHSC->getSExtValue();
4039 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004040
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004041 // powi(x, 0) -> 1.0
4042 if (Val == 0)
4043 return DAG.getConstantFP(1.0, LHS.getValueType());
4044
Dan Gohmanae541aa2010-04-15 04:33:49 +00004045 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004046 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
4047 // If optimizing for size, don't insert too many multiplies. This
4048 // inserts up to 5 multiplies.
4049 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4050 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004051 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004052 // powi(x,15) generates one more multiply than it should), but this has
4053 // the benefit of being both really simple and much better than a libcall.
4054 SDValue Res; // Logically starts equal to 1.0
4055 SDValue CurSquare = LHS;
4056 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004057 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004058 if (Res.getNode())
4059 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4060 else
4061 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004062 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004063
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004064 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4065 CurSquare, CurSquare);
4066 Val >>= 1;
4067 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004068
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004069 // If the original was negative, invert the result, producing 1/(x*x*x).
4070 if (RHSC->getSExtValue() < 0)
4071 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4072 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4073 return Res;
4074 }
4075 }
4076
4077 // Otherwise, expand to a libcall.
4078 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4079}
4080
Devang Patel227dfdb2011-05-16 21:24:05 +00004081// getTruncatedArgReg - Find underlying register used for an truncated
4082// argument.
4083static unsigned getTruncatedArgReg(const SDValue &N) {
4084 if (N.getOpcode() != ISD::TRUNCATE)
4085 return 0;
4086
4087 const SDValue &Ext = N.getOperand(0);
4088 if (Ext.getOpcode() == ISD::AssertZext || Ext.getOpcode() == ISD::AssertSext){
4089 const SDValue &CFR = Ext.getOperand(0);
4090 if (CFR.getOpcode() == ISD::CopyFromReg)
4091 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
4092 else
4093 if (CFR.getOpcode() == ISD::TRUNCATE)
4094 return getTruncatedArgReg(CFR);
4095 }
4096 return 0;
4097}
4098
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004099/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4100/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4101/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004102bool
Devang Patel78a06e52010-08-25 20:39:26 +00004103SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004104 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004105 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004106 const Argument *Arg = dyn_cast<Argument>(V);
4107 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004108 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004109
Devang Patel719f6a92010-04-29 20:40:36 +00004110 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004111 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
4112 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4113
Devang Patela83ce982010-04-29 18:50:36 +00004114 // Ignore inlined function arguments here.
4115 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004116 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004117 return false;
4118
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004119 unsigned Reg = 0;
Devang Patel0b48ead2010-08-31 22:22:42 +00004120 if (Arg->hasByValAttr()) {
4121 // Byval arguments' frame index is recorded during argument lowering.
4122 // Use this info directly.
Devang Patel0b48ead2010-08-31 22:22:42 +00004123 Reg = TRI->getFrameRegister(MF);
4124 Offset = FuncInfo.getByValArgumentFrameIndex(Arg);
Devang Patel27f46cd2010-10-01 19:00:44 +00004125 // If byval argument ofset is not recorded then ignore this.
4126 if (!Offset)
4127 Reg = 0;
Devang Patel0b48ead2010-08-31 22:22:42 +00004128 }
4129
Devang Patel227dfdb2011-05-16 21:24:05 +00004130 if (N.getNode()) {
4131 if (N.getOpcode() == ISD::CopyFromReg)
4132 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4133 else
4134 Reg = getTruncatedArgReg(N);
4135 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004136 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4137 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4138 if (PR)
4139 Reg = PR;
4140 }
4141 }
4142
Evan Chenga36acad2010-04-29 06:33:38 +00004143 if (!Reg) {
Devang Patela90b3052010-11-02 17:01:30 +00004144 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004145 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004146 if (VMI != FuncInfo.ValueMap.end())
4147 Reg = VMI->second;
Evan Chenga36acad2010-04-29 06:33:38 +00004148 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004149
Devang Patel8bc9ef72010-11-02 17:19:03 +00004150 if (!Reg && N.getNode()) {
Devang Patela90b3052010-11-02 17:01:30 +00004151 // Check if frame index is available.
4152 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004153 if (FrameIndexSDNode *FINode =
Devang Patela90b3052010-11-02 17:01:30 +00004154 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) {
4155 Reg = TRI->getFrameRegister(MF);
4156 Offset = FINode->getIndex();
4157 }
Devang Patel8bc9ef72010-11-02 17:19:03 +00004158 }
4159
4160 if (!Reg)
4161 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004162
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004163 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
4164 TII->get(TargetOpcode::DBG_VALUE))
Evan Chenga36acad2010-04-29 06:33:38 +00004165 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004166 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004167 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004168}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004169
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004170// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004171#if defined(_MSC_VER) && defined(setjmp) && \
4172 !defined(setjmp_undefined_for_msvc)
4173# pragma push_macro("setjmp")
4174# undef setjmp
4175# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004176#endif
4177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004178/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4179/// we want to emit this as a call to a named external function, return the name
4180/// otherwise lower it and return null.
4181const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004182SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004183 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004184 SDValue Res;
4185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004186 switch (Intrinsic) {
4187 default:
4188 // By default, turn this into a target intrinsic node.
4189 visitTargetIntrinsic(I, Intrinsic);
4190 return 0;
4191 case Intrinsic::vastart: visitVAStart(I); return 0;
4192 case Intrinsic::vaend: visitVAEnd(I); return 0;
4193 case Intrinsic::vacopy: visitVACopy(I); return 0;
4194 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00004195 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004196 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004197 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004198 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00004199 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004200 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004201 return 0;
4202 case Intrinsic::setjmp:
4203 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004204 case Intrinsic::longjmp:
4205 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004206 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004207 // Assert for address < 256 since we support only user defined address
4208 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004209 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004210 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004211 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004212 < 256 &&
4213 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004214 SDValue Op1 = getValue(I.getArgOperand(0));
4215 SDValue Op2 = getValue(I.getArgOperand(1));
4216 SDValue Op3 = getValue(I.getArgOperand(2));
4217 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4218 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00004219 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004220 MachinePointerInfo(I.getArgOperand(0)),
4221 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004222 return 0;
4223 }
Chris Lattner824b9582008-11-21 16:42:48 +00004224 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004225 // Assert for address < 256 since we support only user defined address
4226 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004227 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004228 < 256 &&
4229 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004230 SDValue Op1 = getValue(I.getArgOperand(0));
4231 SDValue Op2 = getValue(I.getArgOperand(1));
4232 SDValue Op3 = getValue(I.getArgOperand(2));
4233 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4234 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00004235 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004236 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004237 return 0;
4238 }
Chris Lattner824b9582008-11-21 16:42:48 +00004239 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004240 // Assert for address < 256 since we support only user defined address
4241 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004242 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004243 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004244 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004245 < 256 &&
4246 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004247 SDValue Op1 = getValue(I.getArgOperand(0));
4248 SDValue Op2 = getValue(I.getArgOperand(1));
4249 SDValue Op3 = getValue(I.getArgOperand(2));
4250 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4251 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00004252 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004253 MachinePointerInfo(I.getArgOperand(0)),
4254 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004255 return 0;
4256 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004257 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004258 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004259 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004260 const Value *Address = DI.getAddress();
Devang Patel8e741ed2010-09-02 21:02:27 +00004261 if (!Address || !DIVariable(DI.getVariable()).Verify())
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004262 return 0;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004263
4264 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4265 // but do not always have a corresponding SDNode built. The SDNodeOrder
4266 // absolute, but not relative, values are different depending on whether
4267 // debug info exists.
4268 ++SDNodeOrder;
Devang Patel3f74a112010-09-02 21:29:42 +00004269
4270 // Check if address has undef value.
4271 if (isa<UndefValue>(Address) ||
4272 (Address->use_empty() && !isa<Argument>(Address))) {
Devang Patelafeaae72010-12-06 22:39:26 +00004273 DEBUG(dbgs() << "Dropping debug info for " << DI);
Devang Patel3f74a112010-09-02 21:29:42 +00004274 return 0;
4275 }
4276
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004277 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004278 if (!N.getNode() && isa<Argument>(Address))
4279 // Check unused arguments map.
4280 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004281 SDDbgValue *SDV;
4282 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004283 // Parameters are handled specially.
Michael J. Spencere70c5262010-10-16 08:25:21 +00004284 bool isParameter =
Devang Patel8e741ed2010-09-02 21:02:27 +00004285 DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable;
4286 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4287 Address = BCI->getOperand(0);
4288 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4289
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004290 if (isParameter && !AI) {
4291 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4292 if (FINode)
4293 // Byval parameter. We have a frame index at this point.
4294 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4295 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004296 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004297 // Address is an argument, so try to emit its dbg value using
4298 // virtual register info from the FuncInfo.ValueMap.
4299 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004300 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004301 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004302 } else if (AI)
4303 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4304 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004305 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004306 // Can't do anything with other non-AI cases yet.
Devang Patelafeaae72010-12-06 22:39:26 +00004307 DEBUG(dbgs() << "Dropping debug info for " << DI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004308 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004309 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004310 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4311 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004312 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004313 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004314 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004315 // If variable is pinned by a alloca in dominating bb then
4316 // use StaticAllocaMap.
4317 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004318 if (AI->getParent() != DI.getParent()) {
4319 DenseMap<const AllocaInst*, int>::iterator SI =
4320 FuncInfo.StaticAllocaMap.find(AI);
4321 if (SI != FuncInfo.StaticAllocaMap.end()) {
4322 SDV = DAG.getDbgValue(Variable, SI->second,
4323 0, dl, SDNodeOrder);
4324 DAG.AddDbgValue(SDV, 0, false);
4325 return 0;
4326 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004327 }
4328 }
Devang Patelafeaae72010-12-06 22:39:26 +00004329 DEBUG(dbgs() << "Dropping debug info for " << DI);
Devang Patel6cd467b2010-08-26 22:53:27 +00004330 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004331 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004332 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004333 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004334 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004335 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004336 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004337 return 0;
4338
4339 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004340 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004341 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004342 if (!V)
4343 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004344
4345 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4346 // but do not always have a corresponding SDNode built. The SDNodeOrder
4347 // absolute, but not relative, values are different depending on whether
4348 // debug info exists.
4349 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004350 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00004351 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004352 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4353 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004354 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004355 // Do not use getValue() in here; we don't want to generate code at
4356 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004357 SDValue N = NodeMap[V];
4358 if (!N.getNode() && isa<Argument>(V))
4359 // Check unused arguments map.
4360 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004361 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004362 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004363 SDV = DAG.getDbgValue(Variable, N.getNode(),
4364 N.getResNo(), Offset, dl, SDNodeOrder);
4365 DAG.AddDbgValue(SDV, N.getNode(), false);
4366 }
Devang Patela778f5c2011-02-18 22:43:42 +00004367 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004368 // Do not call getValue(V) yet, as we don't want to generate code.
4369 // Remember it for later.
4370 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4371 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004372 } else {
Devang Patel00190342010-03-15 19:15:44 +00004373 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004374 // data available is an unreferenced parameter.
4375 DEBUG(dbgs() << "Dropping debug info for " << DI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004376 }
Devang Patel00190342010-03-15 19:15:44 +00004377 }
4378
4379 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004380 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004381 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004382 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004383 // Don't handle byval struct arguments or VLAs, for example.
4384 if (!AI)
4385 return 0;
4386 DenseMap<const AllocaInst*, int>::iterator SI =
4387 FuncInfo.StaticAllocaMap.find(AI);
4388 if (SI == FuncInfo.StaticAllocaMap.end())
4389 return 0; // VLAs.
4390 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004391
Chris Lattner512063d2010-04-05 06:19:28 +00004392 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4393 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4394 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004395 return 0;
4396 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004398 // Insert the EXCEPTIONADDR instruction.
Dan Gohman84023e02010-07-10 09:00:22 +00004399 assert(FuncInfo.MBB->isLandingPad() &&
Dan Gohman99be8ae2010-04-19 22:41:47 +00004400 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004401 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004402 SDValue Ops[1];
4403 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004404 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004405 setValue(&I, Op);
4406 DAG.setRoot(Op.getValue(1));
4407 return 0;
4408 }
4409
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004410 case Intrinsic::eh_selector: {
Dan Gohman84023e02010-07-10 09:00:22 +00004411 MachineBasicBlock *CallMBB = FuncInfo.MBB;
Chris Lattner512063d2010-04-05 06:19:28 +00004412 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00004413 if (CallMBB->isLandingPad())
4414 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004415 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004416#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004417 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004418#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004419 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4420 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman84023e02010-07-10 09:00:22 +00004421 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004422 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004423
Chris Lattner3a5815f2009-09-17 23:54:54 +00004424 // Insert the EHSELECTION instruction.
4425 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4426 SDValue Ops[2];
Gabor Greif0635f352010-06-25 09:38:13 +00004427 Ops[0] = getValue(I.getArgOperand(0));
Chris Lattner3a5815f2009-09-17 23:54:54 +00004428 Ops[1] = getRoot();
4429 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004430 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004431 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004432 return 0;
4433 }
4434
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004435 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004436 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004437 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004438 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4439 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004440 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004441 return 0;
4442 }
4443
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004444 case Intrinsic::eh_return_i32:
4445 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004446 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
4447 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
4448 MVT::Other,
4449 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004450 getValue(I.getArgOperand(0)),
4451 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004452 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004453 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004454 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004455 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004456 case Intrinsic::eh_dwarf_cfa: {
Gabor Greif0635f352010-06-25 09:38:13 +00004457 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004458 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004459 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004460 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004461 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004462 TLI.getPointerTy()),
4463 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004464 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004465 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004466 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00004467 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4468 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004469 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004470 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004471 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004472 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004473 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004474 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004475 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004476
Chris Lattner512063d2010-04-05 06:19:28 +00004477 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004478 return 0;
4479 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004480 case Intrinsic::eh_sjlj_setjmp: {
4481 setValue(&I, DAG.getNode(ISD::EH_SJLJ_SETJMP, dl, MVT::i32, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004482 getValue(I.getArgOperand(0))));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004483 return 0;
4484 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004485 case Intrinsic::eh_sjlj_longjmp: {
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004486 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, dl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004487 getRoot(), getValue(I.getArgOperand(0))));
4488 return 0;
4489 }
4490 case Intrinsic::eh_sjlj_dispatch_setup: {
4491 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00004492 getRoot(), getValue(I.getArgOperand(0))));
Jim Grosbach5eb19512010-05-22 01:06:18 +00004493 return 0;
4494 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004495
Dale Johannesen0488fb62010-09-30 23:57:10 +00004496 case Intrinsic::x86_mmx_pslli_w:
4497 case Intrinsic::x86_mmx_pslli_d:
4498 case Intrinsic::x86_mmx_pslli_q:
4499 case Intrinsic::x86_mmx_psrli_w:
4500 case Intrinsic::x86_mmx_psrli_d:
4501 case Intrinsic::x86_mmx_psrli_q:
4502 case Intrinsic::x86_mmx_psrai_w:
4503 case Intrinsic::x86_mmx_psrai_d: {
4504 SDValue ShAmt = getValue(I.getArgOperand(1));
4505 if (isa<ConstantSDNode>(ShAmt)) {
4506 visitTargetIntrinsic(I, Intrinsic);
4507 return 0;
4508 }
4509 unsigned NewIntrinsic = 0;
4510 EVT ShAmtVT = MVT::v2i32;
4511 switch (Intrinsic) {
4512 case Intrinsic::x86_mmx_pslli_w:
4513 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4514 break;
4515 case Intrinsic::x86_mmx_pslli_d:
4516 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4517 break;
4518 case Intrinsic::x86_mmx_pslli_q:
4519 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4520 break;
4521 case Intrinsic::x86_mmx_psrli_w:
4522 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4523 break;
4524 case Intrinsic::x86_mmx_psrli_d:
4525 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4526 break;
4527 case Intrinsic::x86_mmx_psrli_q:
4528 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4529 break;
4530 case Intrinsic::x86_mmx_psrai_w:
4531 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4532 break;
4533 case Intrinsic::x86_mmx_psrai_d:
4534 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4535 break;
4536 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4537 }
4538
4539 // The vector shift intrinsics with scalars uses 32b shift amounts but
4540 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4541 // to be zero.
4542 // We must do this early because v2i32 is not a legal type.
4543 DebugLoc dl = getCurDebugLoc();
4544 SDValue ShOps[2];
4545 ShOps[0] = ShAmt;
4546 ShOps[1] = DAG.getConstant(0, MVT::i32);
4547 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
4548 EVT DestVT = TLI.getValueType(I.getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004549 ShAmt = DAG.getNode(ISD::BITCAST, dl, DestVT, ShAmt);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004550 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
4551 DAG.getConstant(NewIntrinsic, MVT::i32),
4552 getValue(I.getArgOperand(0)), ShAmt);
4553 setValue(&I, Res);
4554 return 0;
4555 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004556 case Intrinsic::convertff:
4557 case Intrinsic::convertfsi:
4558 case Intrinsic::convertfui:
4559 case Intrinsic::convertsif:
4560 case Intrinsic::convertuif:
4561 case Intrinsic::convertss:
4562 case Intrinsic::convertsu:
4563 case Intrinsic::convertus:
4564 case Intrinsic::convertuu: {
4565 ISD::CvtCode Code = ISD::CVT_INVALID;
4566 switch (Intrinsic) {
4567 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4568 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4569 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4570 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4571 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4572 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4573 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4574 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4575 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4576 }
Owen Andersone50ed302009-08-10 22:56:29 +00004577 EVT DestVT = TLI.getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004578 const Value *Op1 = I.getArgOperand(0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004579 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4580 DAG.getValueType(DestVT),
4581 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004582 getValue(I.getArgOperand(1)),
4583 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004584 Code);
4585 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004586 return 0;
4587 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004589 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004590 getValue(I.getArgOperand(0)).getValueType(),
4591 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004592 return 0;
4593 case Intrinsic::powi:
Gabor Greif0635f352010-06-25 09:38:13 +00004594 setValue(&I, ExpandPowI(dl, getValue(I.getArgOperand(0)),
4595 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596 return 0;
4597 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004598 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004599 getValue(I.getArgOperand(0)).getValueType(),
4600 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004601 return 0;
4602 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004603 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004604 getValue(I.getArgOperand(0)).getValueType(),
4605 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004606 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004607 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004608 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004609 return 0;
4610 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004611 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004612 return 0;
4613 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004614 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004615 return 0;
4616 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004617 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004618 return 0;
4619 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004620 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004621 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004622 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004623 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004625 case Intrinsic::convert_to_fp16:
4626 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004627 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004628 return 0;
4629 case Intrinsic::convert_from_fp16:
4630 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004631 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004632 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004633 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004634 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004635 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004636 return 0;
4637 }
4638 case Intrinsic::readcyclecounter: {
4639 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004640 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4641 DAG.getVTList(MVT::i64, MVT::Other),
4642 &Op, 1);
4643 setValue(&I, Res);
4644 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004645 return 0;
4646 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004647 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004648 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004649 getValue(I.getArgOperand(0)).getValueType(),
4650 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004651 return 0;
4652 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004653 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004654 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004655 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 return 0;
4657 }
4658 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004659 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004660 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004661 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004662 return 0;
4663 }
4664 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00004665 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004666 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004667 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668 return 0;
4669 }
4670 case Intrinsic::stacksave: {
4671 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004672 Res = DAG.getNode(ISD::STACKSAVE, dl,
4673 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4674 setValue(&I, Res);
4675 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 return 0;
4677 }
4678 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00004679 Res = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004680 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004681 return 0;
4682 }
Bill Wendling57344502008-11-18 11:01:33 +00004683 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004684 // Emit code into the DAG to store the stack guard onto the stack.
4685 MachineFunction &MF = DAG.getMachineFunction();
4686 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004687 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004688
Gabor Greif0635f352010-06-25 09:38:13 +00004689 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
4690 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004691
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004692 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004693 MFI->setStackProtectorIndex(FI);
4694
4695 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4696
4697 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004698 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00004699 MachinePointerInfo::getFixedStack(FI),
4700 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004701 setValue(&I, Res);
4702 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004703 return 0;
4704 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004705 case Intrinsic::objectsize: {
4706 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00004707 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004708
4709 assert(CI && "Non-constant type in __builtin_object_size?");
4710
Gabor Greif0635f352010-06-25 09:38:13 +00004711 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004712 EVT Ty = Arg.getValueType();
4713
Dan Gohmane368b462010-06-18 14:22:04 +00004714 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004715 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004716 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004717 Res = DAG.getConstant(0, Ty);
4718
4719 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004720 return 0;
4721 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004722 case Intrinsic::var_annotation:
4723 // Discard annotate attributes
4724 return 0;
4725
4726 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00004727 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004728
4729 SDValue Ops[6];
4730 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004731 Ops[1] = getValue(I.getArgOperand(0));
4732 Ops[2] = getValue(I.getArgOperand(1));
4733 Ops[3] = getValue(I.getArgOperand(2));
4734 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004735 Ops[5] = DAG.getSrcValue(F);
4736
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004737 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4738 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4739 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004740
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004741 setValue(&I, Res);
4742 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004743 return 0;
4744 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004745 case Intrinsic::gcroot:
4746 if (GFI) {
Gabor Greif0635f352010-06-25 09:38:13 +00004747 const Value *Alloca = I.getArgOperand(0);
4748 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004749
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004750 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4751 GFI->addStackRoot(FI->getIndex(), TypeMap);
4752 }
4753 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004754 case Intrinsic::gcread:
4755 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004756 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004758 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004759 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004760 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00004761 case Intrinsic::trap: {
4762 StringRef TrapFuncName = getTrapFunctionName();
4763 if (TrapFuncName.empty()) {
4764 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
4765 return 0;
4766 }
4767 TargetLowering::ArgListTy Args;
4768 std::pair<SDValue, SDValue> Result =
4769 TLI.LowerCallTo(getRoot(), I.getType(),
4770 false, false, false, false, 0, CallingConv::C,
4771 /*isTailCall=*/false, /*isReturnValueUsed=*/true,
4772 DAG.getExternalSymbol(TrapFuncName.data(), TLI.getPointerTy()),
4773 Args, DAG, getCurDebugLoc());
4774 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004775 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00004776 }
Bill Wendlingef375462008-11-21 02:38:44 +00004777 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004778 return implVisitAluOverflow(I, ISD::UADDO);
4779 case Intrinsic::sadd_with_overflow:
4780 return implVisitAluOverflow(I, ISD::SADDO);
4781 case Intrinsic::usub_with_overflow:
4782 return implVisitAluOverflow(I, ISD::USUBO);
4783 case Intrinsic::ssub_with_overflow:
4784 return implVisitAluOverflow(I, ISD::SSUBO);
4785 case Intrinsic::umul_with_overflow:
4786 return implVisitAluOverflow(I, ISD::UMULO);
4787 case Intrinsic::smul_with_overflow:
4788 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004789
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790 case Intrinsic::prefetch: {
4791 SDValue Ops[4];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004792 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004793 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004794 Ops[1] = getValue(I.getArgOperand(0));
4795 Ops[2] = getValue(I.getArgOperand(1));
4796 Ops[3] = getValue(I.getArgOperand(2));
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004797 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, dl,
4798 DAG.getVTList(MVT::Other),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004799 &Ops[0], 4,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004800 EVT::getIntegerVT(*Context, 8),
4801 MachinePointerInfo(I.getArgOperand(0)),
4802 0, /* align */
4803 false, /* volatile */
4804 rw==0, /* read */
4805 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004806 return 0;
4807 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004808 case Intrinsic::memory_barrier: {
4809 SDValue Ops[6];
4810 Ops[0] = getRoot();
4811 for (int x = 1; x < 6; ++x)
Gabor Greif0635f352010-06-25 09:38:13 +00004812 Ops[x] = getValue(I.getArgOperand(x - 1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004813
Bill Wendling4533cac2010-01-28 21:51:40 +00004814 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004815 return 0;
4816 }
4817 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004818 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004819 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004820 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00004821 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004822 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00004823 getValue(I.getArgOperand(0)),
4824 getValue(I.getArgOperand(1)),
4825 getValue(I.getArgOperand(2)),
Chris Lattner60bddc82010-09-21 04:53:42 +00004826 MachinePointerInfo(I.getArgOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004827 setValue(&I, L);
4828 DAG.setRoot(L.getValue(1));
4829 return 0;
4830 }
4831 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004832 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004833 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004834 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004835 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004836 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004837 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004838 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004839 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004840 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004841 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004842 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004843 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004844 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004845 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004846 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004847 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004848 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004849 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004850 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004851 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004852 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004853
4854 case Intrinsic::invariant_start:
4855 case Intrinsic::lifetime_start:
4856 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004857 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004858 return 0;
4859 case Intrinsic::invariant_end:
4860 case Intrinsic::lifetime_end:
4861 // Discard region information.
4862 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004863 }
4864}
4865
Dan Gohman46510a72010-04-15 01:51:59 +00004866void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004867 bool isTailCall,
4868 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004869 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4870 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004871 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004872 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004873 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004874
4875 TargetLowering::ArgListTy Args;
4876 TargetLowering::ArgListEntry Entry;
4877 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004878
4879 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00004880 SmallVector<ISD::OutputArg, 4> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004881 SmallVector<uint64_t, 4> Offsets;
Dan Gohman84023e02010-07-10 09:00:22 +00004882 GetReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4883 Outs, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004884
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004885 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Eric Christopher471e4222011-06-08 23:55:35 +00004886 DAG.getMachineFunction(),
4887 FTy->isVarArg(), Outs,
4888 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004889
4890 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00004891 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004892
4893 if (!CanLowerReturn) {
4894 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4895 FTy->getReturnType());
4896 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4897 FTy->getReturnType());
4898 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00004899 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004900 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4901
Chris Lattnerecf42c42010-09-21 16:36:31 +00004902 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI.getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004903 Entry.Node = DemoteStackSlot;
4904 Entry.Ty = StackSlotPtrType;
4905 Entry.isSExt = false;
4906 Entry.isZExt = false;
4907 Entry.isInReg = false;
4908 Entry.isSRet = true;
4909 Entry.isNest = false;
4910 Entry.isByVal = false;
4911 Entry.Alignment = Align;
4912 Args.push_back(Entry);
4913 RetTy = Type::getVoidTy(FTy->getContext());
4914 }
4915
Dan Gohman46510a72010-04-15 01:51:59 +00004916 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004917 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00004918 const Value *V = *i;
4919
4920 // Skip empty types
4921 if (V->getType()->isEmptyTy())
4922 continue;
4923
4924 SDValue ArgNode = getValue(V);
4925 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004926
4927 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004928 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4929 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4930 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4931 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4932 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4933 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004934 Entry.Alignment = CS.getParamAlignment(attrInd);
4935 Args.push_back(Entry);
4936 }
4937
Chris Lattner512063d2010-04-05 06:19:28 +00004938 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004939 // Insert a label before the invoke call to mark the try range. This can be
4940 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004941 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004942
Jim Grosbachca752c92010-01-28 01:45:32 +00004943 // For SjLj, keep track of which landing pads go with which invokes
4944 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004945 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004946 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004947 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004948 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004949 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004950 }
4951
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004952 // Both PendingLoads and PendingExports must be flushed here;
4953 // this call might not return.
4954 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004955 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004956 }
4957
Dan Gohman98ca4f22009-08-05 01:29:28 +00004958 // Check if target-independent constraints permit a tail call here.
4959 // Target-dependent constraints are checked within TLI.LowerCallTo.
4960 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004961 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004962 isTailCall = false;
4963
Dan Gohmanbadcda42010-08-28 00:51:03 +00004964 // If there's a possibility that fast-isel has already selected some amount
4965 // of the current basic block, don't emit a tail call.
4966 if (isTailCall && EnableFastISel)
4967 isTailCall = false;
4968
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004969 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004970 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004971 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004972 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004973 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004974 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004975 isTailCall,
4976 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004977 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004978 assert((isTailCall || Result.second.getNode()) &&
4979 "Non-null chain expected with non-tail call!");
4980 assert((Result.second.getNode() || !Result.first.getNode()) &&
4981 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004982 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004983 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004984 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004985 // The instruction result is the result of loading from the
4986 // hidden sret parameter.
4987 SmallVector<EVT, 1> PVTs;
4988 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4989
4990 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4991 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4992 EVT PtrVT = PVTs[0];
Dan Gohman84023e02010-07-10 09:00:22 +00004993 unsigned NumValues = Outs.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004994 SmallVector<SDValue, 4> Values(NumValues);
4995 SmallVector<SDValue, 4> Chains(NumValues);
4996
4997 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004998 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4999 DemoteStackSlot,
5000 DAG.getConstant(Offsets[i], PtrVT));
Dan Gohman84023e02010-07-10 09:00:22 +00005001 SDValue L = DAG.getLoad(Outs[i].VT, getCurDebugLoc(), Result.second,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005002 Add,
5003 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
5004 false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005005 Values[i] = L;
5006 Chains[i] = L.getValue(1);
5007 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005008
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005009 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
5010 MVT::Other, &Chains[0], NumValues);
5011 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005012
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00005013 // Collect the legal value parts into potentially illegal values
5014 // that correspond to the original function's return values.
5015 SmallVector<EVT, 4> RetTys;
5016 RetTy = FTy->getReturnType();
5017 ComputeValueVTs(TLI, RetTy, RetTys);
5018 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5019 SmallVector<SDValue, 4> ReturnValues;
5020 unsigned CurReg = 0;
5021 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5022 EVT VT = RetTys[I];
5023 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
5024 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005025
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00005026 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00005027 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00005028 RegisterVT, VT, AssertOp);
5029 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00005030 CurReg += NumRegs;
5031 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005032
Bill Wendling4533cac2010-01-28 21:51:40 +00005033 setValue(CS.getInstruction(),
5034 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
5035 DAG.getVTList(&RetTys[0], RetTys.size()),
5036 &ReturnValues[0], ReturnValues.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005037 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005038
Evan Chengc249e482011-04-01 19:57:01 +00005039 // Assign order to nodes here. If the call does not produce a result, it won't
5040 // be mapped to a SDNode and visit() will not assign it an order number.
Evan Cheng8380c032011-04-01 19:42:22 +00005041 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00005042 // As a special case, a null chain means that a tail call has been emitted and
5043 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005044 HasTailCall = true;
Evan Cheng8380c032011-04-01 19:42:22 +00005045 ++SDNodeOrder;
5046 AssignOrderingToNode(DAG.getRoot().getNode());
5047 } else {
5048 DAG.setRoot(Result.second);
5049 ++SDNodeOrder;
5050 AssignOrderingToNode(Result.second.getNode());
5051 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005052
Chris Lattner512063d2010-04-05 06:19:28 +00005053 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005054 // Insert a label at the end of the invoke call to mark the try range. This
5055 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005056 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00005057 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005058
5059 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005060 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005061 }
5062}
5063
Chris Lattner8047d9a2009-12-24 00:37:38 +00005064/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5065/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005066static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5067 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005068 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005069 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005070 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005071 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005072 if (C->isNullValue())
5073 continue;
5074 // Unknown instruction.
5075 return false;
5076 }
5077 return true;
5078}
5079
Dan Gohman46510a72010-04-15 01:51:59 +00005080static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
5081 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005082 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005083
Chris Lattner8047d9a2009-12-24 00:37:38 +00005084 // Check to see if this load can be trivially constant folded, e.g. if the
5085 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005086 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005087 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005088 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005089 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005090
Dan Gohman46510a72010-04-15 01:51:59 +00005091 if (const Constant *LoadCst =
5092 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5093 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005094 return Builder.getValue(LoadCst);
5095 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005096
Chris Lattner8047d9a2009-12-24 00:37:38 +00005097 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5098 // still constant memory, the input chain can be the entry node.
5099 SDValue Root;
5100 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005101
Chris Lattner8047d9a2009-12-24 00:37:38 +00005102 // Do not serialize (non-volatile) loads of constant memory with anything.
5103 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5104 Root = Builder.DAG.getEntryNode();
5105 ConstantMemory = true;
5106 } else {
5107 // Do not serialize non-volatile loads against each other.
5108 Root = Builder.DAG.getRoot();
5109 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005110
Chris Lattner8047d9a2009-12-24 00:37:38 +00005111 SDValue Ptr = Builder.getValue(PtrVal);
5112 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005113 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005114 false /*volatile*/,
5115 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005116
Chris Lattner8047d9a2009-12-24 00:37:38 +00005117 if (!ConstantMemory)
5118 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5119 return LoadVal;
5120}
5121
5122
5123/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5124/// If so, return true and lower it, otherwise return false and it will be
5125/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005126bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005127 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005128 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005129 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005130
Gabor Greif0635f352010-06-25 09:38:13 +00005131 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005132 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005133 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005134 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005135 return false;
5136
Gabor Greif0635f352010-06-25 09:38:13 +00005137 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005138
Chris Lattner8047d9a2009-12-24 00:37:38 +00005139 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5140 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005141 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5142 bool ActuallyDoIt = true;
5143 MVT LoadVT;
5144 const Type *LoadTy;
5145 switch (Size->getZExtValue()) {
5146 default:
5147 LoadVT = MVT::Other;
5148 LoadTy = 0;
5149 ActuallyDoIt = false;
5150 break;
5151 case 2:
5152 LoadVT = MVT::i16;
5153 LoadTy = Type::getInt16Ty(Size->getContext());
5154 break;
5155 case 4:
5156 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005157 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005158 break;
5159 case 8:
5160 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005161 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005162 break;
5163 /*
5164 case 16:
5165 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005166 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005167 LoadTy = VectorType::get(LoadTy, 4);
5168 break;
5169 */
5170 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005171
Chris Lattner04b091a2009-12-24 01:07:17 +00005172 // This turns into unaligned loads. We only do this if the target natively
5173 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5174 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005175
Chris Lattner04b091a2009-12-24 01:07:17 +00005176 // Require that we can find a legal MVT, and only do this if the target
5177 // supports unaligned loads of that type. Expanding into byte loads would
5178 // bloat the code.
5179 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5180 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5181 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5182 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5183 ActuallyDoIt = false;
5184 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005185
Chris Lattner04b091a2009-12-24 01:07:17 +00005186 if (ActuallyDoIt) {
5187 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5188 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005189
Chris Lattner04b091a2009-12-24 01:07:17 +00005190 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
5191 ISD::SETNE);
5192 EVT CallVT = TLI.getValueType(I.getType(), true);
5193 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
5194 return true;
5195 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005196 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005197
5198
Chris Lattner8047d9a2009-12-24 00:37:38 +00005199 return false;
5200}
5201
5202
Dan Gohman46510a72010-04-15 01:51:59 +00005203void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005204 // Handle inline assembly differently.
5205 if (isa<InlineAsm>(I.getCalledValue())) {
5206 visitInlineAsm(&I);
5207 return;
5208 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005209
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005210 // See if any floating point values are being passed to this function. This is
5211 // used to emit an undefined reference to fltused on Windows.
5212 const FunctionType *FT =
5213 cast<FunctionType>(I.getCalledValue()->getType()->getContainedType(0));
5214 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
5215 if (FT->isVarArg() &&
5216 !MMI.callsExternalVAFunctionWithFloatingPointArguments()) {
5217 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
5218 const Type* T = I.getArgOperand(i)->getType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005219 for (po_iterator<const Type*> i = po_begin(T), e = po_end(T);
Chris Lattnera29aae72010-11-12 17:24:29 +00005220 i != e; ++i) {
5221 if (!i->isFloatingPointTy()) continue;
5222 MMI.setCallsExternalVAFunctionWithFloatingPointArguments(true);
5223 break;
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005224 }
5225 }
5226 }
5227
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005228 const char *RenameFn = 0;
5229 if (Function *F = I.getCalledFunction()) {
5230 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005231 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005232 if (unsigned IID = II->getIntrinsicID(F)) {
5233 RenameFn = visitIntrinsicCall(I, IID);
5234 if (!RenameFn)
5235 return;
5236 }
5237 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005238 if (unsigned IID = F->getIntrinsicID()) {
5239 RenameFn = visitIntrinsicCall(I, IID);
5240 if (!RenameFn)
5241 return;
5242 }
5243 }
5244
5245 // Check for well-known libc/libm calls. If the function is internal, it
5246 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005247 if (!F->hasLocalLinkage() && F->hasName()) {
5248 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00005249 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005250 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005251 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5252 I.getType() == I.getArgOperand(0)->getType() &&
5253 I.getType() == I.getArgOperand(1)->getType()) {
5254 SDValue LHS = getValue(I.getArgOperand(0));
5255 SDValue RHS = getValue(I.getArgOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005256 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5257 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 return;
5259 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005260 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005261 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005262 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5263 I.getType() == I.getArgOperand(0)->getType()) {
5264 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00005265 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
5266 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005267 return;
5268 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005269 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005270 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005271 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5272 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005273 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005274 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00005275 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5276 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005277 return;
5278 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005279 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005280 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005281 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5282 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005283 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005284 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00005285 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5286 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005287 return;
5288 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005289 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005290 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005291 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5292 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005293 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005294 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00005295 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5296 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005297 return;
5298 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005299 } else if (Name == "memcmp") {
5300 if (visitMemCmpCall(I))
5301 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005302 }
5303 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005304 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005305
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005306 SDValue Callee;
5307 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005308 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005309 else
Bill Wendling056292f2008-09-16 21:48:12 +00005310 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005311
Bill Wendling0d580132009-12-23 01:28:19 +00005312 // Check if we can potentially perform a tail call. More detailed checking is
5313 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005314 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005315}
5316
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005317namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005319/// AsmOperandInfo - This contains information for each constraint that we are
5320/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005321class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005322public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005323 /// CallOperand - If this is the result output operand or a clobber
5324 /// this is null, otherwise it is the incoming operand to the CallInst.
5325 /// This gets modified as the asm is processed.
5326 SDValue CallOperand;
5327
5328 /// AssignedRegs - If this is a register or register class operand, this
5329 /// contains the set of register corresponding to the operand.
5330 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005331
John Thompsoneac6e1d2010-09-13 18:15:37 +00005332 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005333 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5334 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005335
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5337 /// busy in OutputRegs/InputRegs.
5338 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005339 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005340 std::set<unsigned> &InputRegs,
5341 const TargetRegisterInfo &TRI) const {
5342 if (isOutReg) {
5343 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5344 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5345 }
5346 if (isInReg) {
5347 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5348 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5349 }
5350 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005351
Owen Andersone50ed302009-08-10 22:56:29 +00005352 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005353 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005354 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005355 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005356 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005357 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005358 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005359
Chris Lattner81249c92008-10-17 17:05:25 +00005360 if (isa<BasicBlock>(CallOperandVal))
5361 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005362
Chris Lattner81249c92008-10-17 17:05:25 +00005363 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005364
Eric Christophercef81b72011-05-09 20:04:43 +00005365 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00005366 // If this is an indirect operand, the operand is a pointer to the
5367 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005368 if (isIndirect) {
5369 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5370 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005371 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005372 OpTy = PtrTy->getElementType();
5373 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005374
Eric Christophercef81b72011-05-09 20:04:43 +00005375 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
5376 if (const StructType *STy = dyn_cast<StructType>(OpTy))
5377 if (STy->getNumElements() == 1)
5378 OpTy = STy->getElementType(0);
5379
Chris Lattner81249c92008-10-17 17:05:25 +00005380 // If OpTy is not a single value, it may be a struct/union that we
5381 // can tile with integers.
5382 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5383 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5384 switch (BitSize) {
5385 default: break;
5386 case 1:
5387 case 8:
5388 case 16:
5389 case 32:
5390 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005391 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005392 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005393 break;
5394 }
5395 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005396
Chris Lattner81249c92008-10-17 17:05:25 +00005397 return TLI.getValueType(OpTy, true);
5398 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005399
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400private:
5401 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5402 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005403 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005404 const TargetRegisterInfo &TRI) {
5405 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5406 Regs.insert(Reg);
5407 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5408 for (; *Aliases; ++Aliases)
5409 Regs.insert(*Aliases);
5410 }
5411};
Dan Gohman462f6b52010-05-29 17:53:24 +00005412
John Thompson44ab89e2010-10-29 17:29:13 +00005413typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
5414
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005415} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005416
Dan Gohman462f6b52010-05-29 17:53:24 +00005417/// isAllocatableRegister - If the specified register is safe to allocate,
5418/// i.e. it isn't a stack pointer or some other special register, return the
5419/// register class for the register. Otherwise, return null.
5420static const TargetRegisterClass *
5421isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5422 const TargetLowering &TLI,
5423 const TargetRegisterInfo *TRI) {
5424 EVT FoundVT = MVT::Other;
5425 const TargetRegisterClass *FoundRC = 0;
5426 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5427 E = TRI->regclass_end(); RCI != E; ++RCI) {
5428 EVT ThisVT = MVT::Other;
5429
5430 const TargetRegisterClass *RC = *RCI;
5431 // If none of the value types for this register class are valid, we
5432 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5433 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5434 I != E; ++I) {
5435 if (TLI.isTypeLegal(*I)) {
5436 // If we have already found this register in a different register class,
5437 // choose the one with the largest VT specified. For example, on
5438 // PowerPC, we favor f64 register classes over f32.
5439 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
5440 ThisVT = *I;
5441 break;
5442 }
5443 }
5444 }
5445
5446 if (ThisVT == MVT::Other) continue;
5447
5448 // NOTE: This isn't ideal. In particular, this might allocate the
5449 // frame pointer in functions that need it (due to them not being taken
5450 // out of allocation, because a variable sized allocation hasn't been seen
5451 // yet). This is a slight code pessimization, but should still work.
5452 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5453 E = RC->allocation_order_end(MF); I != E; ++I)
5454 if (*I == Reg) {
5455 // We found a matching register class. Keep looking at others in case
5456 // we find one with larger registers that this physreg is also in.
5457 FoundRC = RC;
5458 FoundVT = ThisVT;
5459 break;
5460 }
5461 }
5462 return FoundRC;
5463}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005464
5465/// GetRegistersForValue - Assign registers (virtual or physical) for the
5466/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005467/// register allocator to handle the assignment process. However, if the asm
5468/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005469/// allocation. This produces generally horrible, but correct, code.
5470///
5471/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005472/// Input and OutputRegs are the set of already allocated physical registers.
5473///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005474static void GetRegistersForValue(SelectionDAG &DAG,
5475 const TargetLowering &TLI,
5476 DebugLoc DL,
5477 SDISelAsmOperandInfo &OpInfo,
5478 std::set<unsigned> &OutputRegs,
5479 std::set<unsigned> &InputRegs) {
5480 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005481
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005482 // Compute whether this value requires an input register, an output register,
5483 // or both.
5484 bool isOutReg = false;
5485 bool isInReg = false;
5486 switch (OpInfo.Type) {
5487 case InlineAsm::isOutput:
5488 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005489
5490 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005491 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005492 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005493 break;
5494 case InlineAsm::isInput:
5495 isInReg = true;
5496 isOutReg = false;
5497 break;
5498 case InlineAsm::isClobber:
5499 isOutReg = true;
5500 isInReg = true;
5501 break;
5502 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005503
5504
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505 MachineFunction &MF = DAG.getMachineFunction();
5506 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005507
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005508 // If this is a constraint for a single physreg, or a constraint for a
5509 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005510 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005511 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5512 OpInfo.ConstraintVT);
5513
5514 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005515 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005516 // If this is a FP input in an integer register (or visa versa) insert a bit
5517 // cast of the input value. More generally, handle any case where the input
5518 // value disagrees with the register class we plan to stick this in.
5519 if (OpInfo.Type == InlineAsm::isInput &&
5520 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005521 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005522 // types are identical size, use a bitcast to convert (e.g. two differing
5523 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005524 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005525 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005526 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005527 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005528 OpInfo.ConstraintVT = RegVT;
5529 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5530 // If the input is a FP value and we want it in FP registers, do a
5531 // bitcast to the corresponding integer type. This turns an f64 value
5532 // into i64, which can be passed with two i32 values on a 32-bit
5533 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005534 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005535 OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005536 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005537 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005538 OpInfo.ConstraintVT = RegVT;
5539 }
5540 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005541
Owen Anderson23b9b192009-08-12 00:36:31 +00005542 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005543 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005544
Owen Andersone50ed302009-08-10 22:56:29 +00005545 EVT RegVT;
5546 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005547
5548 // If this is a constraint for a specific physical register, like {r17},
5549 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005550 if (unsigned AssignedReg = PhysReg.first) {
5551 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005552 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005553 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005554
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005555 // Get the actual register value type. This is important, because the user
5556 // may have asked for (e.g.) the AX register in i32 type. We need to
5557 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005558 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005559
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005560 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005561 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005562
5563 // If this is an expanded reference, add the rest of the regs to Regs.
5564 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005565 TargetRegisterClass::iterator I = RC->begin();
5566 for (; *I != AssignedReg; ++I)
5567 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005569 // Already added the first reg.
5570 --NumRegs; ++I;
5571 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005572 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005573 Regs.push_back(*I);
5574 }
5575 }
Bill Wendling651ad132009-12-22 01:25:10 +00005576
Dan Gohman7451d3e2010-05-29 17:03:36 +00005577 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005578 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5579 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5580 return;
5581 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005582
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005583 // Otherwise, if this was a reference to an LLVM register class, create vregs
5584 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005585 if (const TargetRegisterClass *RC = PhysReg.second) {
5586 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005587 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005588 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005589
Evan Chengfb112882009-03-23 08:01:15 +00005590 // Create the appropriate number of virtual registers.
5591 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5592 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005593 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005594
Dan Gohman7451d3e2010-05-29 17:03:36 +00005595 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005596 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005597 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005598
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005599 // This is a reference to a register class that doesn't directly correspond
5600 // to an LLVM register class. Allocate NumRegs consecutive, available,
5601 // registers from the class.
5602 std::vector<unsigned> RegClassRegs
5603 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5604 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005605
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005606 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5607 unsigned NumAllocated = 0;
5608 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5609 unsigned Reg = RegClassRegs[i];
5610 // See if this register is available.
5611 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5612 (isInReg && InputRegs.count(Reg))) { // Already used.
5613 // Make sure we find consecutive registers.
5614 NumAllocated = 0;
5615 continue;
5616 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005617
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005618 // Check to see if this register is allocatable (i.e. don't give out the
5619 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005620 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5621 if (!RC) { // Couldn't allocate this register.
5622 // Reset NumAllocated to make sure we return consecutive registers.
5623 NumAllocated = 0;
5624 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005625 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005626
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005627 // Okay, this register is good, we can use it.
5628 ++NumAllocated;
5629
5630 // If we allocated enough consecutive registers, succeed.
5631 if (NumAllocated == NumRegs) {
5632 unsigned RegStart = (i-NumAllocated)+1;
5633 unsigned RegEnd = i+1;
5634 // Mark all of the allocated registers used.
5635 for (unsigned i = RegStart; i != RegEnd; ++i)
5636 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005637
Dan Gohman7451d3e2010-05-29 17:03:36 +00005638 OpInfo.AssignedRegs = RegsForValue(Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005639 OpInfo.ConstraintVT);
5640 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5641 return;
5642 }
5643 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005644
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005645 // Otherwise, we couldn't allocate enough registers for this.
5646}
5647
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005648/// visitInlineAsm - Handle a call to an InlineAsm object.
5649///
Dan Gohman46510a72010-04-15 01:51:59 +00005650void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5651 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652
5653 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00005654 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005655
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005656 std::set<unsigned> OutputRegs, InputRegs;
5657
Evan Chengce1cdac2011-05-06 20:52:23 +00005658 TargetLowering::AsmOperandInfoVector
5659 TargetConstraints = TLI.ParseConstraints(CS);
5660
John Thompsoneac6e1d2010-09-13 18:15:37 +00005661 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005663 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5664 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005665 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
5666 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005667 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00005668
Owen Anderson825b72b2009-08-11 20:47:22 +00005669 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005670
5671 // Compute the value type for each operand.
5672 switch (OpInfo.Type) {
5673 case InlineAsm::isOutput:
5674 // Indirect outputs just consume an argument.
5675 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005676 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 break;
5678 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005679
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005680 // The return value of the call is this value. As such, there is no
5681 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005682 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005683 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005684 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5685 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5686 } else {
5687 assert(ResNo == 0 && "Asm only has one result!");
5688 OpVT = TLI.getValueType(CS.getType());
5689 }
5690 ++ResNo;
5691 break;
5692 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005693 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005694 break;
5695 case InlineAsm::isClobber:
5696 // Nothing to do.
5697 break;
5698 }
5699
5700 // If this is an input or an indirect output, process the call argument.
5701 // BasicBlocks are labels, currently appearing only in asm's.
5702 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00005703 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005704 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005705 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005706 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005707 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005708
Owen Anderson1d0be152009-08-13 21:58:54 +00005709 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005710 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005711
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005712 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005713
John Thompsoneac6e1d2010-09-13 18:15:37 +00005714 // Indirect operand accesses access memory.
5715 if (OpInfo.isIndirect)
5716 hasMemory = true;
5717 else {
5718 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005719 TargetLowering::ConstraintType
5720 CType = TLI.getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00005721 if (CType == TargetLowering::C_Memory) {
5722 hasMemory = true;
5723 break;
5724 }
5725 }
5726 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005727 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005728
John Thompsoneac6e1d2010-09-13 18:15:37 +00005729 SDValue Chain, Flag;
5730
5731 // We won't need to flush pending loads if this asm doesn't touch
5732 // memory and is nonvolatile.
5733 if (hasMemory || IA->hasSideEffects())
5734 Chain = getRoot();
5735 else
5736 Chain = DAG.getRoot();
5737
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005738 // Second pass over the constraints: compute which constraint option to use
5739 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005740 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005741 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005742
John Thompson54584742010-09-24 22:24:05 +00005743 // If this is an output operand with a matching input operand, look up the
5744 // matching input. If their types mismatch, e.g. one is an integer, the
5745 // other is floating point, or their sizes are different, flag it as an
5746 // error.
5747 if (OpInfo.hasMatchingInput()) {
5748 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00005749
John Thompson54584742010-09-24 22:24:05 +00005750 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
5751 if ((OpInfo.ConstraintVT.isInteger() !=
5752 Input.ConstraintVT.isInteger()) ||
5753 (OpInfo.ConstraintVT.getSizeInBits() !=
5754 Input.ConstraintVT.getSizeInBits())) {
5755 report_fatal_error("Unsupported asm: input constraint"
5756 " with a matching output constraint of"
5757 " incompatible type!");
5758 }
5759 Input.ConstraintVT = OpInfo.ConstraintVT;
5760 }
5761 }
5762
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005763 // Compute the constraint code and ConstraintType to use.
Dale Johannesen1784d162010-06-25 21:55:36 +00005764 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005766 // If this is a memory input, and if the operand is not indirect, do what we
5767 // need to to provide an address for the memory input.
5768 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5769 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005770 assert((OpInfo.isMultipleAlternative ||
5771 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005772 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005773
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005774 // Memory operands really want the address of the value. If we don't have
5775 // an indirect input, put it in the constpool if we can, otherwise spill
5776 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00005777 // TODO: This isn't quite right. We need to handle these according to
5778 // the addressing mode that the constraint wants. Also, this may take
5779 // an additional register for the computation and we don't want that
5780 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00005781
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005782 // If the operand is a float, integer, or vector constant, spill to a
5783 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005784 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005785 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5786 isa<ConstantVector>(OpVal)) {
5787 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5788 TLI.getPointerTy());
5789 } else {
5790 // Otherwise, create a stack slot and emit a store to it before the
5791 // asm.
5792 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005793 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005794 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5795 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005796 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005797 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005798 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00005799 OpInfo.CallOperand, StackSlot,
5800 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00005801 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 OpInfo.CallOperand = StackSlot;
5803 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005804
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005805 // There is no longer a Value* corresponding to this operand.
5806 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808 // It is now an indirect operand.
5809 OpInfo.isIndirect = true;
5810 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005811
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005812 // If this constraint is for a specific register, allocate it before
5813 // anything else.
5814 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005815 GetRegistersForValue(DAG, TLI, getCurDebugLoc(), OpInfo, OutputRegs,
5816 InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005817 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005818
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005819 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005820 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005821 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5822 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005823
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005824 // C_Register operands have already been allocated, Other/Memory don't need
5825 // to be.
5826 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005827 GetRegistersForValue(DAG, TLI, getCurDebugLoc(), OpInfo, OutputRegs,
5828 InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005829 }
5830
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005831 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5832 std::vector<SDValue> AsmNodeOperands;
5833 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5834 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005835 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5836 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005837
Chris Lattnerdecc2672010-04-07 05:20:54 +00005838 // If we have a !srcloc metadata node associated with it, we want to attach
5839 // this to the ultimately generated inline asm machineinstr. To do this, we
5840 // pass in the third operand as this (potentially null) inline asm MDNode.
5841 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5842 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005843
Evan Chengc36b7062011-01-07 23:50:32 +00005844 // Remember the HasSideEffect and AlignStack bits as operand 3.
5845 unsigned ExtraInfo = 0;
5846 if (IA->hasSideEffects())
5847 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
5848 if (IA->isAlignStack())
5849 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
5850 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
5851 TLI.getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005852
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005853 // Loop over all of the inputs, copying the operand values into the
5854 // appropriate registers and processing the output regs.
5855 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005856
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005857 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5858 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005859
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005860 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5861 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5862
5863 switch (OpInfo.Type) {
5864 case InlineAsm::isOutput: {
5865 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5866 OpInfo.ConstraintType != TargetLowering::C_Register) {
5867 // Memory output, or 'other' output (e.g. 'X' constraint).
5868 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5869
5870 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005871 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5872 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005873 TLI.getPointerTy()));
5874 AsmNodeOperands.push_back(OpInfo.CallOperand);
5875 break;
5876 }
5877
5878 // Otherwise, this is a register or register class output.
5879
5880 // Copy the output from the appropriate register. Find a register that
5881 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005882 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005883 report_fatal_error("Couldn't allocate output reg for constraint '" +
5884 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005885
5886 // If this is an indirect operand, store through the pointer after the
5887 // asm.
5888 if (OpInfo.isIndirect) {
5889 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5890 OpInfo.CallOperandVal));
5891 } else {
5892 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005893 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005894 // Concatenate this output onto the outputs list.
5895 RetValRegs.append(OpInfo.AssignedRegs);
5896 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005898 // Add information to the INLINEASM node to know that this register is
5899 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005900 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005901 InlineAsm::Kind_RegDefEarlyClobber :
5902 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005903 false,
5904 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005905 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005906 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005907 break;
5908 }
5909 case InlineAsm::isInput: {
5910 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005911
Chris Lattner6bdcda32008-10-17 16:47:46 +00005912 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005913 // If this is required to match an output register we have already set,
5914 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005915 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005916
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005917 // Scan until we find the definition we already emitted of this operand.
5918 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005919 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005920 for (; OperandNo; --OperandNo) {
5921 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005922 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005923 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005924 assert((InlineAsm::isRegDefKind(OpFlag) ||
5925 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5926 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005927 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005928 }
5929
Evan Cheng697cbbf2009-03-20 18:03:34 +00005930 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005931 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005932 if (InlineAsm::isRegDefKind(OpFlag) ||
5933 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005934 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005935 if (OpInfo.isIndirect) {
5936 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005937 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005938 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5939 " don't know how to handle tied "
5940 "indirect register inputs");
5941 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005943 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005944 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005945 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005946 MatchedRegs.RegVTs.push_back(RegVT);
5947 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005948 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005949 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005950 MatchedRegs.Regs.push_back
5951 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005952
5953 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005954 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005955 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005956 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005957 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005958 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005959 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005960 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005961
Chris Lattnerdecc2672010-04-07 05:20:54 +00005962 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5963 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5964 "Unexpected number of operands");
5965 // Add information to the INLINEASM node to know about this input.
5966 // See InlineAsm.h isUseOperandTiedToDef.
5967 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5968 OpInfo.getMatchedOperand());
5969 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5970 TLI.getPointerTy()));
5971 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5972 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005973 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005974
Dale Johannesenb5611a62010-07-13 20:17:05 +00005975 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00005976 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
5977 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00005978 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005979
Dale Johannesenb5611a62010-07-13 20:17:05 +00005980 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005981 std::vector<SDValue> Ops;
Eric Christopher100c8332011-06-02 23:16:42 +00005982 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
Dale Johannesen1784d162010-06-25 21:55:36 +00005983 Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005984 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005985 report_fatal_error("Invalid operand for inline asm constraint '" +
5986 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005987
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005988 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005989 unsigned ResOpType =
5990 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005991 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005992 TLI.getPointerTy()));
5993 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5994 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005995 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005996
Chris Lattnerdecc2672010-04-07 05:20:54 +00005997 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005998 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5999 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6000 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006001
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006002 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006003 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006004 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006005 TLI.getPointerTy()));
6006 AsmNodeOperands.push_back(InOperandVal);
6007 break;
6008 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006009
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006010 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6011 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6012 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006013 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006014 "Don't know how to handle indirect register inputs yet!");
6015
6016 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00006017 if (OpInfo.AssignedRegs.Regs.empty() ||
Dan Gohman7451d3e2010-05-29 17:03:36 +00006018 !OpInfo.AssignedRegs.areValueTypesLegal(TLI))
Benjamin Kramer1bd73352010-04-08 10:44:28 +00006019 report_fatal_error("Couldn't allocate input reg for constraint '" +
6020 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006021
Dale Johannesen66978ee2009-01-31 02:22:37 +00006022 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00006023 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006024
Chris Lattnerdecc2672010-04-07 05:20:54 +00006025 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006026 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006027 break;
6028 }
6029 case InlineAsm::isClobber: {
6030 // Add the clobbered value to the operand list, so that the register
6031 // allocator is aware that the physreg got clobbered.
6032 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00006033 OpInfo.AssignedRegs.AddInlineAsmOperands(
6034 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006035 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006036 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006037 break;
6038 }
6039 }
6040 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006041
Chris Lattnerdecc2672010-04-07 05:20:54 +00006042 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006043 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006044 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006045
Dale Johannesen66978ee2009-01-31 02:22:37 +00006046 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006047 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006048 &AsmNodeOperands[0], AsmNodeOperands.size());
6049 Flag = Chain.getValue(1);
6050
6051 // If this asm returns a register value, copy the result from that register
6052 // and set it as the value of the call.
6053 if (!RetValRegs.Regs.empty()) {
Dan Gohman7451d3e2010-05-29 17:03:36 +00006054 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00006055 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006056
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006057 // FIXME: Why don't we do this for inline asms with MRVs?
6058 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006059 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006060
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006061 // If any of the results of the inline asm is a vector, it may have the
6062 // wrong width/num elts. This can happen for register classes that can
6063 // contain multiple different value types. The preg or vreg allocated may
6064 // not have the same VT as was expected. Convert it to the right type
6065 // with bit_convert.
6066 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006067 Val = DAG.getNode(ISD::BITCAST, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006068 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006069
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006070 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006071 ResultType.isInteger() && Val.getValueType().isInteger()) {
6072 // If a result value was tied to an input value, the computed result may
6073 // have a wider width than the expected result. Extract the relevant
6074 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006075 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006076 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006077
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006078 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006079 }
Dan Gohman95915732008-10-18 01:03:45 +00006080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006082 // Don't need to use this as a chain in this case.
6083 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6084 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006085 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006086
Dan Gohman46510a72010-04-15 01:51:59 +00006087 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006088
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006089 // Process indirect outputs, first output all of the flagged copies out of
6090 // physregs.
6091 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6092 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006093 const Value *Ptr = IndirectStoresToEmit[i].second;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006094 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00006095 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006096 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6097 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006099 // Emit the non-flagged stores from the physregs.
6100 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006101 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6102 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6103 StoresToEmit[i].first,
6104 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006105 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006106 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006107 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006108 }
6109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006110 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006111 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006112 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006113
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006114 DAG.setRoot(Chain);
6115}
6116
Dan Gohman46510a72010-04-15 01:51:59 +00006117void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006118 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6119 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006120 getValue(I.getArgOperand(0)),
6121 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006122}
6123
Dan Gohman46510a72010-04-15 01:51:59 +00006124void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Rafael Espindola9d544d02010-07-12 18:11:17 +00006125 const TargetData &TD = *TLI.getTargetData();
Dale Johannesena04b7572009-02-03 23:04:43 +00006126 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6127 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006128 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006129 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006130 setValue(&I, V);
6131 DAG.setRoot(V.getValue(1));
6132}
6133
Dan Gohman46510a72010-04-15 01:51:59 +00006134void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006135 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6136 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006137 getValue(I.getArgOperand(0)),
6138 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006139}
6140
Dan Gohman46510a72010-04-15 01:51:59 +00006141void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006142 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6143 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006144 getValue(I.getArgOperand(0)),
6145 getValue(I.getArgOperand(1)),
6146 DAG.getSrcValue(I.getArgOperand(0)),
6147 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006148}
6149
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006150/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006151/// implementation, which just calls LowerCall.
6152/// FIXME: When all targets are
6153/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006154std::pair<SDValue, SDValue>
6155TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6156 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006157 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006158 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006159 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006160 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00006161 ArgListTy &Args, SelectionDAG &DAG,
6162 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006163 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006164 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00006165 SmallVector<SDValue, 32> OutVals;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006166 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006167 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006168 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6169 for (unsigned Value = 0, NumValues = ValueVTs.size();
6170 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006171 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006172 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006173 SDValue Op = SDValue(Args[i].Node.getNode(),
6174 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006175 ISD::ArgFlagsTy Flags;
6176 unsigned OriginalAlignment =
6177 getTargetData()->getABITypeAlignment(ArgTy);
6178
6179 if (Args[i].isZExt)
6180 Flags.setZExt();
6181 if (Args[i].isSExt)
6182 Flags.setSExt();
6183 if (Args[i].isInReg)
6184 Flags.setInReg();
6185 if (Args[i].isSRet)
6186 Flags.setSRet();
6187 if (Args[i].isByVal) {
6188 Flags.setByVal();
6189 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6190 const Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006191 Flags.setByValSize(getTargetData()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006192 // For ByVal, alignment should come from FE. BE will guess if this
6193 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006194 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006195 if (Args[i].Alignment)
6196 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006197 else
6198 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006199 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006200 }
6201 if (Args[i].isNest)
6202 Flags.setNest();
6203 Flags.setOrigAlign(OriginalAlignment);
6204
Owen Anderson23b9b192009-08-12 00:36:31 +00006205 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6206 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006207 SmallVector<SDValue, 4> Parts(NumParts);
6208 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6209
6210 if (Args[i].isSExt)
6211 ExtendKind = ISD::SIGN_EXTEND;
6212 else if (Args[i].isZExt)
6213 ExtendKind = ISD::ZERO_EXTEND;
6214
Bill Wendling46ada192010-03-02 01:55:18 +00006215 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006216 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006217
Dan Gohman98ca4f22009-08-05 01:29:28 +00006218 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006219 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006220 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
6221 i < NumFixedArgs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006222 if (NumParts > 1 && j == 0)
6223 MyFlags.Flags.setSplit();
6224 else if (j != 0)
6225 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006226
Dan Gohman98ca4f22009-08-05 01:29:28 +00006227 Outs.push_back(MyFlags);
Dan Gohmanc9403652010-07-07 15:54:55 +00006228 OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006229 }
6230 }
6231 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006232
Dan Gohman98ca4f22009-08-05 01:29:28 +00006233 // Handle the incoming return values from the call.
6234 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006235 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006236 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006237 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006238 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006239 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6240 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006241 for (unsigned i = 0; i != NumRegs; ++i) {
6242 ISD::InputArg MyFlags;
Duncan Sands1440e8b2010-11-03 11:35:31 +00006243 MyFlags.VT = RegisterVT.getSimpleVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006244 MyFlags.Used = isReturnValueUsed;
6245 if (RetSExt)
6246 MyFlags.Flags.setSExt();
6247 if (RetZExt)
6248 MyFlags.Flags.setZExt();
6249 if (isInreg)
6250 MyFlags.Flags.setInReg();
6251 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006252 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006253 }
6254
Dan Gohman98ca4f22009-08-05 01:29:28 +00006255 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00006256 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +00006257 Outs, OutVals, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006258
6259 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006260 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006261 "LowerCall didn't return a valid chain!");
6262 assert((!isTailCall || InVals.empty()) &&
6263 "LowerCall emitted a return value for a tail call!");
6264 assert((isTailCall || InVals.size() == Ins.size()) &&
6265 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006266
6267 // For a tail call, the return value is merely live-out and there aren't
6268 // any nodes in the DAG representing it. Return a special value to
6269 // indicate that a tail call has been emitted and no more Instructions
6270 // should be processed in the current block.
6271 if (isTailCall) {
6272 DAG.setRoot(Chain);
6273 return std::make_pair(SDValue(), SDValue());
6274 }
6275
Evan Chengaf1871f2010-03-11 19:38:18 +00006276 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6277 assert(InVals[i].getNode() &&
6278 "LowerCall emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00006279 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006280 "LowerCall emitted a value with the wrong type!");
6281 });
6282
Dan Gohman98ca4f22009-08-05 01:29:28 +00006283 // Collect the legal value parts into potentially illegal values
6284 // that correspond to the original function's return values.
6285 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6286 if (RetSExt)
6287 AssertOp = ISD::AssertSext;
6288 else if (RetZExt)
6289 AssertOp = ISD::AssertZext;
6290 SmallVector<SDValue, 4> ReturnValues;
6291 unsigned CurReg = 0;
6292 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006293 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006294 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6295 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006296
Bill Wendling46ada192010-03-02 01:55:18 +00006297 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00006298 NumRegs, RegisterVT, VT,
6299 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006300 CurReg += NumRegs;
6301 }
6302
6303 // For a function returning void, there is no return value. We can't create
6304 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006305 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006306 if (ReturnValues.empty())
6307 return std::make_pair(SDValue(), Chain);
6308
6309 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6310 DAG.getVTList(&RetTys[0], RetTys.size()),
6311 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006312 return std::make_pair(Res, Chain);
6313}
6314
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006315void TargetLowering::LowerOperationWrapper(SDNode *N,
6316 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006317 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006318 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006319 if (Res.getNode())
6320 Results.push_back(Res);
6321}
6322
Dan Gohmand858e902010-04-17 15:26:15 +00006323SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006324 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006325 return SDValue();
6326}
6327
Dan Gohman46510a72010-04-15 01:51:59 +00006328void
6329SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006330 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006331 assert((Op.getOpcode() != ISD::CopyFromReg ||
6332 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6333 "Copy from a reg to the same reg!");
6334 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6335
Owen Anderson23b9b192009-08-12 00:36:31 +00006336 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006337 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00006338 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006339 PendingExports.push_back(Chain);
6340}
6341
6342#include "llvm/CodeGen/SelectionDAGISel.h"
6343
Eli Friedman23d32432011-05-05 16:53:34 +00006344/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6345/// entry block, return true. This includes arguments used by switches, since
6346/// the switch may expand into multiple basic blocks.
6347static bool isOnlyUsedInEntryBlock(const Argument *A) {
6348 // With FastISel active, we may be splitting blocks, so force creation
6349 // of virtual registers for all non-dead arguments.
6350 if (EnableFastISel)
6351 return A->use_empty();
6352
6353 const BasicBlock *Entry = A->getParent()->begin();
6354 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6355 UI != E; ++UI) {
6356 const User *U = *UI;
6357 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6358 return false; // Use not in entry block.
6359 }
6360 return true;
6361}
6362
Dan Gohman46510a72010-04-15 01:51:59 +00006363void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006364 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00006365 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006366 SelectionDAG &DAG = SDB->DAG;
Dan Gohman2048b852009-11-23 18:04:58 +00006367 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006368 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006369 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006370
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006371 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00006372 SmallVector<ISD::OutputArg, 4> Outs;
6373 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
6374 Outs, TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006375
Dan Gohman7451d3e2010-05-29 17:03:36 +00006376 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006377 // Put in an sret pointer parameter before all the other parameters.
6378 SmallVector<EVT, 1> ValueVTs;
6379 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6380
6381 // NOTE: Assuming that a pointer will never break down to more than one VT
6382 // or one register.
6383 ISD::ArgFlagsTy Flags;
6384 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00006385 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006386 ISD::InputArg RetArg(Flags, RegisterVT, true);
6387 Ins.push_back(RetArg);
6388 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006389
Dan Gohman98ca4f22009-08-05 01:29:28 +00006390 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006391 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006392 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006393 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006394 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006395 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6396 bool isArgValueUsed = !I->use_empty();
6397 for (unsigned Value = 0, NumValues = ValueVTs.size();
6398 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006399 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006400 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006401 ISD::ArgFlagsTy Flags;
6402 unsigned OriginalAlignment =
6403 TD->getABITypeAlignment(ArgTy);
6404
6405 if (F.paramHasAttr(Idx, Attribute::ZExt))
6406 Flags.setZExt();
6407 if (F.paramHasAttr(Idx, Attribute::SExt))
6408 Flags.setSExt();
6409 if (F.paramHasAttr(Idx, Attribute::InReg))
6410 Flags.setInReg();
6411 if (F.paramHasAttr(Idx, Attribute::StructRet))
6412 Flags.setSRet();
6413 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6414 Flags.setByVal();
6415 const PointerType *Ty = cast<PointerType>(I->getType());
6416 const Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006417 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006418 // For ByVal, alignment should be passed from FE. BE will guess if
6419 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006420 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006421 if (F.getParamAlignment(Idx))
6422 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00006423 else
6424 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006425 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006426 }
6427 if (F.paramHasAttr(Idx, Attribute::Nest))
6428 Flags.setNest();
6429 Flags.setOrigAlign(OriginalAlignment);
6430
Owen Anderson23b9b192009-08-12 00:36:31 +00006431 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6432 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006433 for (unsigned i = 0; i != NumRegs; ++i) {
6434 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6435 if (NumRegs > 1 && i == 0)
6436 MyFlags.Flags.setSplit();
6437 // if it isn't first piece, alignment must be 1
6438 else if (i > 0)
6439 MyFlags.Flags.setOrigAlign(1);
6440 Ins.push_back(MyFlags);
6441 }
6442 }
6443 }
6444
6445 // Call the target to set up the argument values.
6446 SmallVector<SDValue, 8> InVals;
6447 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6448 F.isVarArg(), Ins,
6449 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006450
6451 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006452 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006453 "LowerFormalArguments didn't return a valid chain!");
6454 assert(InVals.size() == Ins.size() &&
6455 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006456 DEBUG({
6457 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6458 assert(InVals[i].getNode() &&
6459 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00006460 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00006461 "LowerFormalArguments emitted a value with the wrong type!");
6462 }
6463 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006464
Dan Gohman5e866062009-08-06 15:37:27 +00006465 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006466 DAG.setRoot(NewRoot);
6467
6468 // Set up the argument values.
6469 unsigned i = 0;
6470 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006471 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006472 // Create a virtual register for the sret pointer, and put in a copy
6473 // from the sret argument into it.
6474 SmallVector<EVT, 1> ValueVTs;
6475 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6476 EVT VT = ValueVTs[0];
6477 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6478 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006479 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006480 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006481
Dan Gohman2048b852009-11-23 18:04:58 +00006482 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006483 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6484 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006485 FuncInfo->DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006486 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6487 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006488 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006489
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006490 // i indexes lowered arguments. Bump it past the hidden sret argument.
6491 // Idx indexes LLVM arguments. Don't touch it.
6492 ++i;
6493 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006494
Dan Gohman46510a72010-04-15 01:51:59 +00006495 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006496 ++I, ++Idx) {
6497 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006498 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006499 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006500 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006501
6502 // If this argument is unused then remember its value. It is used to generate
6503 // debugging information.
6504 if (I->use_empty() && NumValues)
6505 SDB->setUnusedArgValue(I, InVals[i]);
6506
Eli Friedman23d32432011-05-05 16:53:34 +00006507 for (unsigned Val = 0; Val != NumValues; ++Val) {
6508 EVT VT = ValueVTs[Val];
Owen Anderson23b9b192009-08-12 00:36:31 +00006509 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6510 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006511
6512 if (!I->use_empty()) {
6513 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6514 if (F.paramHasAttr(Idx, Attribute::SExt))
6515 AssertOp = ISD::AssertSext;
6516 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6517 AssertOp = ISD::AssertZext;
6518
Bill Wendling46ada192010-03-02 01:55:18 +00006519 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006520 NumParts, PartVT, VT,
6521 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006522 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006523
Dan Gohman98ca4f22009-08-05 01:29:28 +00006524 i += NumParts;
6525 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006526
Eli Friedman23d32432011-05-05 16:53:34 +00006527 // We don't need to do anything else for unused arguments.
6528 if (ArgValues.empty())
6529 continue;
6530
Devang Patel0b48ead2010-08-31 22:22:42 +00006531 // Note down frame index for byval arguments.
Eli Friedman23d32432011-05-05 16:53:34 +00006532 if (I->hasByValAttr())
Michael J. Spencere70c5262010-10-16 08:25:21 +00006533 if (FrameIndexSDNode *FI =
Devang Patel0b48ead2010-08-31 22:22:42 +00006534 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
6535 FuncInfo->setByValArgumentFrameIndex(I, FI->getIndex());
6536
Eli Friedman23d32432011-05-05 16:53:34 +00006537 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6538 SDB->getCurDebugLoc());
6539 SDB->setValue(I, Res);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006540
Eli Friedman23d32432011-05-05 16:53:34 +00006541 // If this argument is live outside of the entry block, insert a copy from
6542 // wherever we got it to the vreg that other BB's will reference it as.
Eli Friedman7f33d672011-05-10 21:50:58 +00006543 if (!EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00006544 // If we can, though, try to skip creating an unnecessary vreg.
6545 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00006546 // general. It's also subtly incompatible with the hacks FastISel
6547 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00006548 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
6549 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
6550 FuncInfo->ValueMap[I] = Reg;
6551 continue;
6552 }
6553 }
6554 if (!isOnlyUsedInEntryBlock(I)) {
6555 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00006556 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006557 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006558 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006559
Dan Gohman98ca4f22009-08-05 01:29:28 +00006560 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006561
6562 // Finally, if the target has anything special to do, allow it to do so.
6563 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006564 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006565}
6566
6567/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6568/// ensure constants are generated when needed. Remember the virtual registers
6569/// that need to be added to the Machine PHI nodes as input. We cannot just
6570/// directly add them, because expansion might result in multiple MBB's for one
6571/// BB. As such, the start of the BB might correspond to a different MBB than
6572/// the end.
6573///
6574void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006575SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006576 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006577
6578 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6579
6580 // Check successor nodes' PHI nodes that expect a constant to be available
6581 // from this block.
6582 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006583 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006584 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006585 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006586
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006587 // If this terminator has multiple identical successors (common for
6588 // switches), only handle each succ once.
6589 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006590
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006591 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006592
6593 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6594 // nodes and Machine PHI nodes, but the incoming operands have not been
6595 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006596 for (BasicBlock::const_iterator I = SuccBB->begin();
6597 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006598 // Ignore dead phi's.
6599 if (PN->use_empty()) continue;
6600
Rafael Espindola3fa82832011-05-13 15:18:06 +00006601 // Skip empty types
6602 if (PN->getType()->isEmptyTy())
6603 continue;
6604
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006605 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006606 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006607
Dan Gohman46510a72010-04-15 01:51:59 +00006608 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006609 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006610 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00006611 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006612 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006613 }
6614 Reg = RegOut;
6615 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00006616 DenseMap<const Value *, unsigned>::iterator I =
6617 FuncInfo.ValueMap.find(PHIOp);
6618 if (I != FuncInfo.ValueMap.end())
6619 Reg = I->second;
6620 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006621 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006622 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006623 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00006624 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006625 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006626 }
6627 }
6628
6629 // Remember that this register needs to added to the machine PHI node as
6630 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006631 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006632 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6633 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006634 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006635 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006636 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006637 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006638 Reg += NumRegisters;
6639 }
6640 }
6641 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006642 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006643}