blob: 1c8f22a2a2519577aeae71d8087fda7986b11cc9 [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Devang Patel00190342010-03-15 19:15:44 +000015#include "SDNodeDbgValue.h"
Dan Gohman2048b852009-11-23 18:04:58 +000016#include "SelectionDAGBuilder.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000018#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000019#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000020#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000021#include "llvm/Constants.h"
22#include "llvm/CallingConv.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Function.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/InlineAsm.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/IntrinsicInst.h"
Chris Lattner6129c372010-04-08 00:09:16 +000030#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000031#include "llvm/Module.h"
Dan Gohman5eb6d652010-04-21 01:22:34 +000032#include "llvm/CodeGen/Analysis.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000033#include "llvm/CodeGen/FastISel.h"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000034#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000035#include "llvm/CodeGen/GCStrategy.h"
36#include "llvm/CodeGen/GCMetadata.h"
37#include "llvm/CodeGen/MachineFunction.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
40#include "llvm/CodeGen/MachineJumpTableInfo.h"
41#include "llvm/CodeGen/MachineModuleInfo.h"
42#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000043#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000045#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000046#include "llvm/Target/TargetRegisterInfo.h"
47#include "llvm/Target/TargetData.h"
48#include "llvm/Target/TargetFrameInfo.h"
49#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000050#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000052#include "llvm/Target/TargetOptions.h"
53#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000054#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000056#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000058#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059#include <algorithm>
60using namespace llvm;
61
Dale Johannesen601d3c02008-09-05 01:48:15 +000062/// LimitFloatPrecision - Generate low-precision inline sequences for
63/// some float libcalls (6, 8 or 12 bits).
64static unsigned LimitFloatPrecision;
65
66static cl::opt<unsigned, true>
67LimitFPPrecision("limit-float-precision",
68 cl::desc("Generate low-precision inline sequences "
69 "for some float libcalls"),
70 cl::location(LimitFloatPrecision),
71 cl::init(0));
72
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000073/// getCopyFromParts - Create a value that contains the specified legal parts
74/// combined into the value they represent. If the parts combine to a type
75/// larger then ValueVT then AssertOp can be used to specify whether the extra
76/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
77/// (ISD::AssertSext).
Bill Wendling46ada192010-03-02 01:55:18 +000078static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +000079 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +000080 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +000081 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000082 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +000083 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000084 SDValue Val = Parts[0];
85
86 if (NumParts > 1) {
87 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +000088 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000089 unsigned PartBits = PartVT.getSizeInBits();
90 unsigned ValueBits = ValueVT.getSizeInBits();
91
92 // Assemble the power of 2 part.
93 unsigned RoundParts = NumParts & (NumParts - 1) ?
94 1 << Log2_32(NumParts) : NumParts;
95 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +000096 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +000097 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000098 SDValue Lo, Hi;
99
Owen Anderson23b9b192009-08-12 00:36:31 +0000100 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000102 if (RoundParts > 2) {
Bill Wendling46ada192010-03-02 01:55:18 +0000103 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000104 PartVT, HalfVT);
Bill Wendling46ada192010-03-02 01:55:18 +0000105 Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000106 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000107 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000108 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
109 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000112 if (TLI.isBigEndian())
113 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000114
Dale Johannesen66978ee2009-01-31 02:22:37 +0000115 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000116
117 if (RoundParts < NumParts) {
118 // Assemble the trailing non-power-of-2 part.
119 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000120 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Bill Wendling46ada192010-03-02 01:55:18 +0000121 Hi = getCopyFromParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000122 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000123
124 // Combine the round and odd parts.
125 Lo = Val;
126 if (TLI.isBigEndian())
127 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000128 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000129 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
130 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000131 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000132 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000133 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
134 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000135 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000136 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000137 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000138 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000139 unsigned NumIntermediates;
140 unsigned NumRegs =
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000141 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
Owen Anderson23b9b192009-08-12 00:36:31 +0000142 NumIntermediates, RegisterVT);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000143 assert(NumRegs == NumParts
144 && "Part count doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000145 NumParts = NumRegs; // Silence a compiler warning.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000146 assert(RegisterVT == PartVT
147 && "Part type doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000148 assert(RegisterVT == Parts[0].getValueType() &&
149 "Part type doesn't match part!");
150
151 // Assemble the parts into intermediate operands.
152 SmallVector<SDValue, 8> Ops(NumIntermediates);
153 if (NumIntermediates == NumParts) {
154 // If the register was not expanded, truncate or copy the value,
155 // as appropriate.
156 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000157 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000158 PartVT, IntermediateVT);
159 } else if (NumParts > 0) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000160 // If the intermediate type was expanded, build the intermediate
161 // operands from the parts.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000162 assert(NumParts % NumIntermediates == 0 &&
163 "Must expand into a divisible number of parts!");
164 unsigned Factor = NumParts / NumIntermediates;
165 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000166 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000167 PartVT, IntermediateVT);
168 }
169
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000170 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
171 // intermediate operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000172 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000173 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000174 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000175 } else if (PartVT.isFloatingPoint()) {
176 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000178 "Unexpected split");
179 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
181 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000182 if (TLI.isBigEndian())
183 std::swap(Lo, Hi);
184 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
185 } else {
186 // FP split into integer parts (soft fp)
187 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
188 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000189 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling46ada192010-03-02 01:55:18 +0000190 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000191 }
192 }
193
194 // There is now one part, held in Val. Correct it to match ValueVT.
195 PartVT = Val.getValueType();
196
197 if (PartVT == ValueVT)
198 return Val;
199
200 if (PartVT.isVector()) {
201 assert(ValueVT.isVector() && "Unknown vector conversion!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000202 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000203 }
204
205 if (ValueVT.isVector()) {
206 assert(ValueVT.getVectorElementType() == PartVT &&
207 ValueVT.getVectorNumElements() == 1 &&
208 "Only trivial scalar-to-vector conversions should get here!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000209 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000210 }
211
212 if (PartVT.isInteger() &&
213 ValueVT.isInteger()) {
214 if (ValueVT.bitsLT(PartVT)) {
215 // For a truncate, see if we have any information to
216 // indicate whether the truncated bits will always be
217 // zero or sign-extension.
218 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000219 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000220 DAG.getValueType(ValueVT));
Bill Wendling4533cac2010-01-28 21:51:40 +0000221 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000222 } else {
Bill Wendling4533cac2010-01-28 21:51:40 +0000223 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000224 }
225 }
226
227 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000228 if (ValueVT.bitsLT(Val.getValueType())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000229 // FP_ROUND's are always exact here.
Bill Wendling4533cac2010-01-28 21:51:40 +0000230 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
231 DAG.getIntPtrConstant(1));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000232 }
233
Bill Wendling4533cac2010-01-28 21:51:40 +0000234 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000235 }
236
Bill Wendling4533cac2010-01-28 21:51:40 +0000237 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
238 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000239
Torok Edwinc23197a2009-07-14 16:55:14 +0000240 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000241 return SDValue();
242}
243
244/// getCopyToParts - Create a series of nodes that contain the specified value
245/// split into legal parts. If the parts contain more bits than Val, then, for
246/// integers, ExtendKind can be used to specify how to generate the extra bits.
Bill Wendling46ada192010-03-02 01:55:18 +0000247static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000248 SDValue Val, SDValue *Parts, unsigned NumParts,
249 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000250 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000251 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000252 EVT PtrVT = TLI.getPointerTy();
253 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000254 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000255 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000256 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
257
258 if (!NumParts)
259 return;
260
261 if (!ValueVT.isVector()) {
262 if (PartVT == ValueVT) {
263 assert(NumParts == 1 && "No-op copy with multiple parts!");
264 Parts[0] = Val;
265 return;
266 }
267
268 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
269 // If the parts cover more bits than the value has, promote the value.
270 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
271 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000272 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000273 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000274 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000275 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000276 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000277 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000278 }
279 } else if (PartBits == ValueVT.getSizeInBits()) {
280 // Different types of the same size.
281 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000282 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000283 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
284 // If the parts cover less bits than value has, truncate the value.
285 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000286 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000287 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000288 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000289 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000290 }
291 }
292
293 // The value may have changed - recompute ValueVT.
294 ValueVT = Val.getValueType();
295 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
296 "Failed to tile the value with PartVT!");
297
298 if (NumParts == 1) {
299 assert(PartVT == ValueVT && "Type conversion failed!");
300 Parts[0] = Val;
301 return;
302 }
303
304 // Expand the value into multiple parts.
305 if (NumParts & (NumParts - 1)) {
306 // The number of parts is not a power of 2. Split off and copy the tail.
307 assert(PartVT.isInteger() && ValueVT.isInteger() &&
308 "Do not know what to expand to!");
309 unsigned RoundParts = 1 << Log2_32(NumParts);
310 unsigned RoundBits = RoundParts * PartBits;
311 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000312 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000313 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000314 TLI.getPointerTy()));
Bill Wendling46ada192010-03-02 01:55:18 +0000315 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000316 OddParts, PartVT);
317
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000318 if (TLI.isBigEndian())
319 // The odd parts were reversed by getCopyToParts - unreverse them.
320 std::reverse(Parts + RoundParts, Parts + NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000321
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000322 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000323 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000324 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000325 }
326
327 // The number of parts is a power of 2. Repeatedly bisect the value using
328 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000329 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Chris Lattnerf031e8a2010-01-01 03:32:16 +0000330 EVT::getIntegerVT(*DAG.getContext(),
331 ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000332 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000334 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
335 for (unsigned i = 0; i < NumParts; i += StepSize) {
336 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000337 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000338 SDValue &Part0 = Parts[i];
339 SDValue &Part1 = Parts[i+StepSize/2];
340
Scott Michelfdc40a02009-02-17 22:15:04 +0000341 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000342 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000343 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000344 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000345 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000346 DAG.getConstant(0, PtrVT));
347
348 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000349 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000350 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000351 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000352 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000353 }
354 }
355 }
356
357 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000358 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000359
360 return;
361 }
362
363 // Vector ValueVT.
364 if (NumParts == 1) {
365 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000366 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000367 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000368 } else {
369 assert(ValueVT.getVectorElementType() == PartVT &&
370 ValueVT.getVectorNumElements() == 1 &&
371 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000372 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000373 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000374 DAG.getConstant(0, PtrVT));
375 }
376 }
377
378 Parts[0] = Val;
379 return;
380 }
381
382 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000383 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000384 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000385 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
386 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000387 unsigned NumElements = ValueVT.getVectorNumElements();
388
389 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
390 NumParts = NumRegs; // Silence a compiler warning.
391 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
392
393 // Split the vector into intermediate operands.
394 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000395 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000396 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000397 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000398 IntermediateVT, Val,
399 DAG.getConstant(i * (NumElements / NumIntermediates),
400 PtrVT));
401 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000402 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000403 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000404 DAG.getConstant(i, PtrVT));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000405 }
406
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000407 // Split the intermediate operands into legal parts.
408 if (NumParts == NumIntermediates) {
409 // If the register was not expanded, promote or copy the value,
410 // as appropriate.
411 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000412 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000413 } else if (NumParts > 0) {
414 // If the intermediate type was expanded, split each the value into
415 // legal parts.
416 assert(NumParts % NumIntermediates == 0 &&
417 "Must expand into a divisible number of parts!");
418 unsigned Factor = NumParts / NumIntermediates;
419 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000420 getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000421 }
422}
423
Dan Gohman462f6b52010-05-29 17:53:24 +0000424namespace {
425 /// RegsForValue - This struct represents the registers (physical or virtual)
426 /// that a particular set of values is assigned, and the type information
427 /// about the value. The most common situation is to represent one value at a
428 /// time, but struct or array values are handled element-wise as multiple
429 /// values. The splitting of aggregates is performed recursively, so that we
430 /// never have aggregate-typed registers. The values at this point do not
431 /// necessarily have legal types, so each value may require one or more
432 /// registers of some legal type.
433 ///
434 struct RegsForValue {
435 /// ValueVTs - The value types of the values, which may not be legal, and
436 /// may need be promoted or synthesized from one or more registers.
437 ///
438 SmallVector<EVT, 4> ValueVTs;
439
440 /// RegVTs - The value types of the registers. This is the same size as
441 /// ValueVTs and it records, for each value, what the type of the assigned
442 /// register or registers are. (Individual values are never synthesized
443 /// from more than one type of register.)
444 ///
445 /// With virtual registers, the contents of RegVTs is redundant with TLI's
446 /// getRegisterType member function, however when with physical registers
447 /// it is necessary to have a separate record of the types.
448 ///
449 SmallVector<EVT, 4> RegVTs;
450
451 /// Regs - This list holds the registers assigned to the values.
452 /// Each legal or promoted value requires one register, and each
453 /// expanded value requires multiple registers.
454 ///
455 SmallVector<unsigned, 4> Regs;
456
457 RegsForValue() {}
458
459 RegsForValue(const SmallVector<unsigned, 4> &regs,
460 EVT regvt, EVT valuevt)
461 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
462
463 RegsForValue(const SmallVector<unsigned, 4> &regs,
464 const SmallVector<EVT, 4> &regvts,
465 const SmallVector<EVT, 4> &valuevts)
466 : ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
467
468 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
469 unsigned Reg, const Type *Ty) {
470 ComputeValueVTs(tli, Ty, ValueVTs);
471
472 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
473 EVT ValueVT = ValueVTs[Value];
474 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
475 EVT RegisterVT = tli.getRegisterType(Context, ValueVT);
476 for (unsigned i = 0; i != NumRegs; ++i)
477 Regs.push_back(Reg + i);
478 RegVTs.push_back(RegisterVT);
479 Reg += NumRegs;
480 }
481 }
482
483 /// areValueTypesLegal - Return true if types of all the values are legal.
484 bool areValueTypesLegal(const TargetLowering &TLI) {
485 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
486 EVT RegisterVT = RegVTs[Value];
487 if (!TLI.isTypeLegal(RegisterVT))
488 return false;
489 }
490 return true;
491 }
492
493 /// append - Add the specified values to this one.
494 void append(const RegsForValue &RHS) {
495 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
496 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
497 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
498 }
499
500 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
501 /// this value and returns the result as a ValueVTs value. This uses
502 /// Chain/Flag as the input and updates them for the output Chain/Flag.
503 /// If the Flag pointer is NULL, no flag is used.
504 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
505 DebugLoc dl,
506 SDValue &Chain, SDValue *Flag) const;
507
508 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
509 /// specified value into the registers specified by this object. This uses
510 /// Chain/Flag as the input and updates them for the output Chain/Flag.
511 /// If the Flag pointer is NULL, no flag is used.
512 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
513 SDValue &Chain, SDValue *Flag) const;
514
515 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
516 /// operand list. This adds the code marker, matching input operand index
517 /// (if applicable), and includes the number of values added into it.
518 void AddInlineAsmOperands(unsigned Kind,
519 bool HasMatching, unsigned MatchingIdx,
520 SelectionDAG &DAG,
521 std::vector<SDValue> &Ops) const;
522 };
523}
524
525/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
526/// this value and returns the result as a ValueVT value. This uses
527/// Chain/Flag as the input and updates them for the output Chain/Flag.
528/// If the Flag pointer is NULL, no flag is used.
529SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
530 FunctionLoweringInfo &FuncInfo,
531 DebugLoc dl,
532 SDValue &Chain, SDValue *Flag) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000533 // A Value with type {} or [0 x %t] needs no registers.
534 if (ValueVTs.empty())
535 return SDValue();
536
Dan Gohman462f6b52010-05-29 17:53:24 +0000537 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
538
539 // Assemble the legal parts into the final values.
540 SmallVector<SDValue, 4> Values(ValueVTs.size());
541 SmallVector<SDValue, 8> Parts;
542 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
543 // Copy the legal parts from the registers.
544 EVT ValueVT = ValueVTs[Value];
545 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
546 EVT RegisterVT = RegVTs[Value];
547
548 Parts.resize(NumRegs);
549 for (unsigned i = 0; i != NumRegs; ++i) {
550 SDValue P;
551 if (Flag == 0) {
552 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
553 } else {
554 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
555 *Flag = P.getValue(2);
556 }
557
558 Chain = P.getValue(1);
559
560 // If the source register was virtual and if we know something about it,
561 // add an assert node.
562 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
563 RegisterVT.isInteger() && !RegisterVT.isVector()) {
564 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
565 if (FuncInfo.LiveOutRegInfo.size() > SlotNo) {
566 const FunctionLoweringInfo::LiveOutInfo &LOI =
567 FuncInfo.LiveOutRegInfo[SlotNo];
568
569 unsigned RegSize = RegisterVT.getSizeInBits();
570 unsigned NumSignBits = LOI.NumSignBits;
571 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
572
573 // FIXME: We capture more information than the dag can represent. For
574 // now, just use the tightest assertzext/assertsext possible.
575 bool isSExt = true;
576 EVT FromVT(MVT::Other);
577 if (NumSignBits == RegSize)
578 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
579 else if (NumZeroBits >= RegSize-1)
580 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
581 else if (NumSignBits > RegSize-8)
582 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
583 else if (NumZeroBits >= RegSize-8)
584 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
585 else if (NumSignBits > RegSize-16)
586 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
587 else if (NumZeroBits >= RegSize-16)
588 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
589 else if (NumSignBits > RegSize-32)
590 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
591 else if (NumZeroBits >= RegSize-32)
592 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
593
594 if (FromVT != MVT::Other)
595 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
596 RegisterVT, P, DAG.getValueType(FromVT));
597 }
598 }
599
600 Parts[i] = P;
601 }
602
603 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
604 NumRegs, RegisterVT, ValueVT);
605 Part += NumRegs;
606 Parts.clear();
607 }
608
609 return DAG.getNode(ISD::MERGE_VALUES, dl,
610 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
611 &Values[0], ValueVTs.size());
612}
613
614/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
615/// specified value into the registers specified by this object. This uses
616/// Chain/Flag as the input and updates them for the output Chain/Flag.
617/// If the Flag pointer is NULL, no flag is used.
618void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
619 SDValue &Chain, SDValue *Flag) const {
620 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
621
622 // Get the list of the values's legal parts.
623 unsigned NumRegs = Regs.size();
624 SmallVector<SDValue, 8> Parts(NumRegs);
625 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
626 EVT ValueVT = ValueVTs[Value];
627 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
628 EVT RegisterVT = RegVTs[Value];
629
630 getCopyToParts(DAG, dl,
631 Val.getValue(Val.getResNo() + Value),
632 &Parts[Part], NumParts, RegisterVT);
633 Part += NumParts;
634 }
635
636 // Copy the parts into the registers.
637 SmallVector<SDValue, 8> Chains(NumRegs);
638 for (unsigned i = 0; i != NumRegs; ++i) {
639 SDValue Part;
640 if (Flag == 0) {
641 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
642 } else {
643 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
644 *Flag = Part.getValue(1);
645 }
646
647 Chains[i] = Part.getValue(0);
648 }
649
650 if (NumRegs == 1 || Flag)
651 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
652 // flagged to it. That is the CopyToReg nodes and the user are considered
653 // a single scheduling unit. If we create a TokenFactor and return it as
654 // chain, then the TokenFactor is both a predecessor (operand) of the
655 // user as well as a successor (the TF operands are flagged to the user).
656 // c1, f1 = CopyToReg
657 // c2, f2 = CopyToReg
658 // c3 = TokenFactor c1, c2
659 // ...
660 // = op c3, ..., f2
661 Chain = Chains[NumRegs-1];
662 else
663 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
664}
665
666/// AddInlineAsmOperands - Add this value to the specified inlineasm node
667/// operand list. This adds the code marker and includes the number of
668/// values added into it.
669void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
670 unsigned MatchingIdx,
671 SelectionDAG &DAG,
672 std::vector<SDValue> &Ops) const {
673 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
674
675 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
676 if (HasMatching)
677 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
678 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
679 Ops.push_back(Res);
680
681 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
682 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
683 EVT RegisterVT = RegVTs[Value];
684 for (unsigned i = 0; i != NumRegs; ++i) {
685 assert(Reg < Regs.size() && "Mismatch in # registers expected");
686 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
687 }
688 }
689}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000690
Dan Gohman2048b852009-11-23 18:04:58 +0000691void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000692 AA = &aa;
693 GFI = gfi;
694 TD = DAG.getTarget().getTargetData();
695}
696
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000697/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000698/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000699/// for a new block. This doesn't clear out information about
700/// additional blocks that are needed to complete switch lowering
701/// or PHI node updating; that information is cleared out as it is
702/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000703void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000704 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000705 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000706 PendingLoads.clear();
707 PendingExports.clear();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000708 DanglingDebugInfoMap.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000709 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000710 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000711}
712
713/// getRoot - Return the current virtual root of the Selection DAG,
714/// flushing any PendingLoad items. This must be done before emitting
715/// a store or any other node that may need to be ordered after any
716/// prior load instructions.
717///
Dan Gohman2048b852009-11-23 18:04:58 +0000718SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000719 if (PendingLoads.empty())
720 return DAG.getRoot();
721
722 if (PendingLoads.size() == 1) {
723 SDValue Root = PendingLoads[0];
724 DAG.setRoot(Root);
725 PendingLoads.clear();
726 return Root;
727 }
728
729 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000731 &PendingLoads[0], PendingLoads.size());
732 PendingLoads.clear();
733 DAG.setRoot(Root);
734 return Root;
735}
736
737/// getControlRoot - Similar to getRoot, but instead of flushing all the
738/// PendingLoad items, flush all the PendingExports items. It is necessary
739/// to do this before emitting a terminator instruction.
740///
Dan Gohman2048b852009-11-23 18:04:58 +0000741SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000742 SDValue Root = DAG.getRoot();
743
744 if (PendingExports.empty())
745 return Root;
746
747 // Turn all of the CopyToReg chains into one factored node.
748 if (Root.getOpcode() != ISD::EntryToken) {
749 unsigned i = 0, e = PendingExports.size();
750 for (; i != e; ++i) {
751 assert(PendingExports[i].getNode()->getNumOperands() > 1);
752 if (PendingExports[i].getNode()->getOperand(0) == Root)
753 break; // Don't add the root if we already indirectly depend on it.
754 }
755
756 if (i == e)
757 PendingExports.push_back(Root);
758 }
759
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000761 &PendingExports[0],
762 PendingExports.size());
763 PendingExports.clear();
764 DAG.setRoot(Root);
765 return Root;
766}
767
Bill Wendling4533cac2010-01-28 21:51:40 +0000768void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
769 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
770 DAG.AssignOrdering(Node, SDNodeOrder);
771
772 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
773 AssignOrderingToNode(Node->getOperand(I).getNode());
774}
775
Dan Gohman46510a72010-04-15 01:51:59 +0000776void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000777 // Set up outgoing PHI node register values before emitting the terminator.
778 if (isa<TerminatorInst>(&I))
779 HandlePHINodesInSuccessorBlocks(I.getParent());
780
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000781 CurDebugLoc = I.getDebugLoc();
782
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000783 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000784
Dan Gohman92884f72010-04-20 15:03:56 +0000785 if (!isa<TerminatorInst>(&I) && !HasTailCall)
786 CopyToExportRegsIfNeeded(&I);
787
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000788 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000789}
790
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000791void SelectionDAGBuilder::visitPHI(const PHINode &) {
792 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
793}
794
Dan Gohman46510a72010-04-15 01:51:59 +0000795void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000796 // Note: this doesn't use InstVisitor, because it has to work with
797 // ConstantExpr's in addition to instructions.
798 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000799 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000800 // Build the switch statement using the Instruction.def file.
801#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000802 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000803#include "llvm/Instruction.def"
804 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000805
806 // Assign the ordering to the freshly created DAG nodes.
807 if (NodeMap.count(&I)) {
808 ++SDNodeOrder;
809 AssignOrderingToNode(getValue(&I).getNode());
810 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000811}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000812
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000813// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
814// generate the debug data structures now that we've seen its definition.
815void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
816 SDValue Val) {
817 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
818 if (DDI.getDI()) {
819 const DbgValueInst *DI = DDI.getDI();
820 DebugLoc dl = DDI.getdl();
821 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
822 MDNode *Variable = DI->getVariable();
823 uint64_t Offset = DI->getOffset();
824 SDDbgValue *SDV;
825 if (Val.getNode()) {
826 if (!EmitFuncArgumentDbgValue(*DI, V, Variable, Offset, Val)) {
827 SDV = DAG.getDbgValue(Variable, Val.getNode(),
828 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
829 DAG.AddDbgValue(SDV, Val.getNode(), false);
830 }
831 } else {
832 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
833 Offset, dl, SDNodeOrder);
834 DAG.AddDbgValue(SDV, 0, false);
835 }
836 DanglingDebugInfoMap[V] = DanglingDebugInfo();
837 }
838}
839
Dan Gohman28a17352010-07-01 01:59:43 +0000840// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +0000841SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +0000842 // If we already have an SDValue for this value, use it. It's important
843 // to do this first, so that we don't create a CopyFromReg if we already
844 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000845 SDValue &N = NodeMap[V];
846 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000847
Dan Gohman28a17352010-07-01 01:59:43 +0000848 // If there's a virtual register allocated and initialized for this
849 // value, use it.
850 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
851 if (It != FuncInfo.ValueMap.end()) {
852 unsigned InReg = It->second;
853 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
854 SDValue Chain = DAG.getEntryNode();
Eric Christopher723a05a2010-07-14 23:41:32 +0000855 return N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain,NULL);
Dan Gohman28a17352010-07-01 01:59:43 +0000856 }
857
858 // Otherwise create a new SDValue and remember it.
859 SDValue Val = getValueImpl(V);
860 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000861 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +0000862 return Val;
863}
864
865/// getNonRegisterValue - Return an SDValue for the given Value, but
866/// don't look in FuncInfo.ValueMap for a virtual register.
867SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
868 // If we already have an SDValue for this value, use it.
869 SDValue &N = NodeMap[V];
870 if (N.getNode()) return N;
871
872 // Otherwise create a new SDValue and remember it.
873 SDValue Val = getValueImpl(V);
874 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000875 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +0000876 return Val;
877}
878
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000879/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +0000880/// Create an SDValue for the given value.
881SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Dan Gohman383b5f62010-04-17 15:32:28 +0000882 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000883 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000884
Dan Gohman383b5f62010-04-17 15:32:28 +0000885 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000886 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000887
Dan Gohman383b5f62010-04-17 15:32:28 +0000888 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Devang Patel0d881da2010-07-06 22:08:15 +0000889 return DAG.getGlobalAddress(GV, getCurDebugLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000890
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000891 if (isa<ConstantPointerNull>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000892 return DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000893
Dan Gohman383b5f62010-04-17 15:32:28 +0000894 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000895 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000896
Nate Begeman9008ca62009-04-27 18:41:29 +0000897 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +0000898 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000899
Dan Gohman383b5f62010-04-17 15:32:28 +0000900 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000901 visit(CE->getOpcode(), *CE);
902 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000903 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000904 return N1;
905 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000906
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000907 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
908 SmallVector<SDValue, 4> Constants;
909 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
910 OI != OE; ++OI) {
911 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000912 // If the operand is an empty aggregate, there are no values.
913 if (!Val) continue;
914 // Add each leaf value from the operand to the Constants list
915 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000916 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
917 Constants.push_back(SDValue(Val, i));
918 }
Bill Wendling87710f02009-12-21 23:47:40 +0000919
Bill Wendling4533cac2010-01-28 21:51:40 +0000920 return DAG.getMergeValues(&Constants[0], Constants.size(),
921 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000922 }
923
Duncan Sands1df98592010-02-16 11:11:14 +0000924 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000925 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
926 "Unknown struct or array constant!");
927
Owen Andersone50ed302009-08-10 22:56:29 +0000928 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000929 ComputeValueVTs(TLI, C->getType(), ValueVTs);
930 unsigned NumElts = ValueVTs.size();
931 if (NumElts == 0)
932 return SDValue(); // empty struct
933 SmallVector<SDValue, 4> Constants(NumElts);
934 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000935 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000936 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000937 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000938 else if (EltVT.isFloatingPoint())
939 Constants[i] = DAG.getConstantFP(0, EltVT);
940 else
941 Constants[i] = DAG.getConstant(0, EltVT);
942 }
Bill Wendling87710f02009-12-21 23:47:40 +0000943
Bill Wendling4533cac2010-01-28 21:51:40 +0000944 return DAG.getMergeValues(&Constants[0], NumElts,
945 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000946 }
947
Dan Gohman383b5f62010-04-17 15:32:28 +0000948 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000949 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000950
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000951 const VectorType *VecTy = cast<VectorType>(V->getType());
952 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000953
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000954 // Now that we know the number and type of the elements, get that number of
955 // elements into the Ops array based on what kind of constant it is.
956 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000957 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000958 for (unsigned i = 0; i != NumElements; ++i)
959 Ops.push_back(getValue(CP->getOperand(i)));
960 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000961 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000962 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000963
964 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000965 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000966 Op = DAG.getConstantFP(0, EltVT);
967 else
968 Op = DAG.getConstant(0, EltVT);
969 Ops.assign(NumElements, Op);
970 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000971
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000972 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000973 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
974 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000975 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000976
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000977 // If this is a static alloca, generate it as the frameindex instead of
978 // computation.
979 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
980 DenseMap<const AllocaInst*, int>::iterator SI =
981 FuncInfo.StaticAllocaMap.find(AI);
982 if (SI != FuncInfo.StaticAllocaMap.end())
983 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
984 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000985
Dan Gohman28a17352010-07-01 01:59:43 +0000986 // If this is an instruction which fast-isel has deferred, select it now.
987 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000988 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
989 RegsForValue RFV(*DAG.getContext(), TLI, InReg, Inst->getType());
990 SDValue Chain = DAG.getEntryNode();
991 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL);
Dan Gohman28a17352010-07-01 01:59:43 +0000992 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000993
Dan Gohman28a17352010-07-01 01:59:43 +0000994 llvm_unreachable("Can't get register for value!");
995 return SDValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000996}
997
Dan Gohman46510a72010-04-15 01:51:59 +0000998void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000999 SDValue Chain = getControlRoot();
1000 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001001 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001002
Dan Gohman7451d3e2010-05-29 17:03:36 +00001003 if (!FuncInfo.CanLowerReturn) {
1004 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001005 const Function *F = I.getParent()->getParent();
1006
1007 // Emit a store of the return value through the virtual register.
1008 // Leave Outs empty so that LowerReturn won't try to load return
1009 // registers the usual way.
1010 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001011 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001012 PtrValueVTs);
1013
1014 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1015 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001016
Owen Andersone50ed302009-08-10 22:56:29 +00001017 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001018 SmallVector<uint64_t, 4> Offsets;
1019 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001020 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001021
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001022 SmallVector<SDValue, 4> Chains(NumValues);
1023 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +00001024 for (unsigned i = 0; i != NumValues; ++i) {
1025 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
1026 DAG.getConstant(Offsets[i], PtrVT));
1027 Chains[i] =
1028 DAG.getStore(Chain, getCurDebugLoc(),
1029 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00001030 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001031 }
1032
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001033 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
1034 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001035 } else if (I.getNumOperands() != 0) {
1036 SmallVector<EVT, 4> ValueVTs;
1037 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
1038 unsigned NumValues = ValueVTs.size();
1039 if (NumValues) {
1040 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001041 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1042 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001043
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001044 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001045
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001046 const Function *F = I.getParent()->getParent();
1047 if (F->paramHasAttr(0, Attribute::SExt))
1048 ExtendKind = ISD::SIGN_EXTEND;
1049 else if (F->paramHasAttr(0, Attribute::ZExt))
1050 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001051
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001052 // FIXME: C calling convention requires the return type to be promoted
1053 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001054 // conventions. The frontend should mark functions whose return values
1055 // require promoting with signext or zeroext attributes.
1056 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
1057 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
1058 if (VT.bitsLT(MinVT))
1059 VT = MinVT;
1060 }
1061
1062 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
1063 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
1064 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +00001065 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001066 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
1067 &Parts[0], NumParts, PartVT, ExtendKind);
1068
1069 // 'inreg' on function refers to return value
1070 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1071 if (F->paramHasAttr(0, Attribute::InReg))
1072 Flags.setInReg();
1073
1074 // Propagate extension type if any
1075 if (F->paramHasAttr(0, Attribute::SExt))
1076 Flags.setSExt();
1077 else if (F->paramHasAttr(0, Attribute::ZExt))
1078 Flags.setZExt();
1079
Dan Gohmanc9403652010-07-07 15:54:55 +00001080 for (unsigned i = 0; i < NumParts; ++i) {
1081 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
1082 /*isfixed=*/true));
1083 OutVals.push_back(Parts[i]);
1084 }
Evan Cheng3927f432009-03-25 20:20:11 +00001085 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001086 }
1087 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001088
1089 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001090 CallingConv::ID CallConv =
1091 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
Dan Gohmanc9403652010-07-07 15:54:55 +00001093 Outs, OutVals, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001094
1095 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001097 "LowerReturn didn't return a valid chain!");
1098
1099 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001100 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001101}
1102
Dan Gohmanad62f532009-04-23 23:13:24 +00001103/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1104/// created for it, emit nodes to copy the value into the virtual
1105/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001106void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +00001107 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1108 if (VMI != FuncInfo.ValueMap.end()) {
1109 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1110 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001111 }
1112}
1113
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001114/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1115/// the current basic block, add it to ValueMap now so that we'll get a
1116/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001117void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001118 // No need to export constants.
1119 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001120
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001121 // Already exported?
1122 if (FuncInfo.isExportedInst(V)) return;
1123
1124 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1125 CopyValueToVirtualRegister(V, Reg);
1126}
1127
Dan Gohman46510a72010-04-15 01:51:59 +00001128bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001129 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001130 // The operands of the setcc have to be in this block. We don't know
1131 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001132 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001133 // Can export from current BB.
1134 if (VI->getParent() == FromBB)
1135 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001136
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001137 // Is already exported, noop.
1138 return FuncInfo.isExportedInst(V);
1139 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001141 // If this is an argument, we can export it if the BB is the entry block or
1142 // if it is already exported.
1143 if (isa<Argument>(V)) {
1144 if (FromBB == &FromBB->getParent()->getEntryBlock())
1145 return true;
1146
1147 // Otherwise, can only export this if it is already exported.
1148 return FuncInfo.isExportedInst(V);
1149 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001150
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001151 // Otherwise, constants can always be exported.
1152 return true;
1153}
1154
1155static bool InBlock(const Value *V, const BasicBlock *BB) {
1156 if (const Instruction *I = dyn_cast<Instruction>(V))
1157 return I->getParent() == BB;
1158 return true;
1159}
1160
Dan Gohmanc2277342008-10-17 21:16:08 +00001161/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1162/// This function emits a branch and is used at the leaves of an OR or an
1163/// AND operator tree.
1164///
1165void
Dan Gohman46510a72010-04-15 01:51:59 +00001166SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001167 MachineBasicBlock *TBB,
1168 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001169 MachineBasicBlock *CurBB,
1170 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001171 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172
Dan Gohmanc2277342008-10-17 21:16:08 +00001173 // If the leaf of the tree is a comparison, merge the condition into
1174 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001175 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001176 // The operands of the cmp have to be in this block. We don't know
1177 // how to export them from some other block. If this is the first block
1178 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001179 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001180 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1181 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001182 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001183 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001184 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001185 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001186 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001187 } else {
1188 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001189 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001190 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001191
1192 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001193 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1194 SwitchCases.push_back(CB);
1195 return;
1196 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001197 }
1198
1199 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001200 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001201 NULL, TBB, FBB, CurBB);
1202 SwitchCases.push_back(CB);
1203}
1204
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001205/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001206void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001207 MachineBasicBlock *TBB,
1208 MachineBasicBlock *FBB,
1209 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001210 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001211 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001212 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001213 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001214 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001215 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1216 BOp->getParent() != CurBB->getBasicBlock() ||
1217 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1218 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001219 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001220 return;
1221 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 // Create TmpBB after CurBB.
1224 MachineFunction::iterator BBI = CurBB;
1225 MachineFunction &MF = DAG.getMachineFunction();
1226 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1227 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001228
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001229 if (Opc == Instruction::Or) {
1230 // Codegen X | Y as:
1231 // jmp_if_X TBB
1232 // jmp TmpBB
1233 // TmpBB:
1234 // jmp_if_Y TBB
1235 // jmp FBB
1236 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001237
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001238 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001239 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001240
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001241 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001242 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001243 } else {
1244 assert(Opc == Instruction::And && "Unknown merge op!");
1245 // Codegen X & Y as:
1246 // jmp_if_X TmpBB
1247 // jmp FBB
1248 // TmpBB:
1249 // jmp_if_Y TBB
1250 // jmp FBB
1251 //
1252 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001253
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001254 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001255 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001256
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001257 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001258 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001259 }
1260}
1261
1262/// If the set of cases should be emitted as a series of branches, return true.
1263/// If we should emit this as a bunch of and/or'd together conditions, return
1264/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001265bool
Dan Gohman2048b852009-11-23 18:04:58 +00001266SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001267 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001268
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001269 // If this is two comparisons of the same values or'd or and'd together, they
1270 // will get folded into a single comparison, so don't emit two blocks.
1271 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1272 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1273 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1274 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1275 return false;
1276 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001277
Chris Lattner133ce872010-01-02 00:00:03 +00001278 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1279 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1280 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1281 Cases[0].CC == Cases[1].CC &&
1282 isa<Constant>(Cases[0].CmpRHS) &&
1283 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1284 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1285 return false;
1286 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1287 return false;
1288 }
1289
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001290 return true;
1291}
1292
Dan Gohman46510a72010-04-15 01:51:59 +00001293void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001294 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001295
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001296 // Update machine-CFG edges.
1297 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1298
1299 // Figure out which block is immediately after the current one.
1300 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001301 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001302 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303 NextBlock = BBI;
1304
1305 if (I.isUnconditional()) {
1306 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001307 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001308
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001309 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001310 if (Succ0MBB != NextBlock)
1311 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001313 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 return;
1316 }
1317
1318 // If this condition is one of the special cases we handle, do special stuff
1319 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001320 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001321 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1322
1323 // If this is a series of conditions that are or'd or and'd together, emit
1324 // this as a sequence of branches instead of setcc's with and/or operations.
1325 // For example, instead of something like:
1326 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001327 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001328 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001329 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330 // or C, F
1331 // jnz foo
1332 // Emit:
1333 // cmp A, B
1334 // je foo
1335 // cmp D, E
1336 // jle foo
1337 //
Dan Gohman46510a72010-04-15 01:51:59 +00001338 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001339 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001340 (BOp->getOpcode() == Instruction::And ||
1341 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001342 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1343 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001344 // If the compares in later blocks need to use values not currently
1345 // exported from this block, export them now. This block should always
1346 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001347 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001348
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001349 // Allow some cases to be rejected.
1350 if (ShouldEmitAsBranches(SwitchCases)) {
1351 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1352 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1353 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1354 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001355
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001356 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001357 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001358 SwitchCases.erase(SwitchCases.begin());
1359 return;
1360 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001361
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001362 // Okay, we decided not to do this, remove any inserted MBB's and clear
1363 // SwitchCases.
1364 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001365 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001366
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001367 SwitchCases.clear();
1368 }
1369 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001370
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001371 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001372 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001373 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001374
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001375 // Use visitSwitchCase to actually insert the fast branch sequence for this
1376 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001377 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001378}
1379
1380/// visitSwitchCase - Emits the necessary code to represent a single node in
1381/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001382void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1383 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001384 SDValue Cond;
1385 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001386 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001387
1388 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001389 if (CB.CmpMHS == NULL) {
1390 // Fold "(X == true)" to X and "(X == false)" to !X to
1391 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001392 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001393 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001394 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001395 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001396 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001397 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001398 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001399 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001400 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001401 } else {
1402 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1403
Anton Korobeynikov23218582008-12-23 22:25:27 +00001404 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1405 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406
1407 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001408 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409
1410 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001412 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001413 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001414 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001415 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001417 DAG.getConstant(High-Low, VT), ISD::SETULE);
1418 }
1419 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001422 SwitchBB->addSuccessor(CB.TrueBB);
1423 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001425 // Set NextBlock to be the MBB immediately after the current one, if any.
1426 // This is used to avoid emitting unnecessary branches to the next block.
1427 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001428 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001429 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001431
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001432 // If the lhs block is the next block, invert the condition so that we can
1433 // fall through to the lhs instead of the rhs block.
1434 if (CB.TrueBB == NextBlock) {
1435 std::swap(CB.TrueBB, CB.FalseBB);
1436 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001437 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001438 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001439
Dale Johannesenf5d97892009-02-04 01:48:28 +00001440 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001442 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001443
Dan Gohmandeca0522010-06-24 17:08:31 +00001444 // Insert the false branch.
1445 if (CB.FalseBB != NextBlock)
1446 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1447 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001448
1449 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001450}
1451
1452/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001453void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454 // Emit the code for the jump table
1455 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001456 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001457 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1458 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001459 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001460 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1461 MVT::Other, Index.getValue(1),
1462 Table, Index);
1463 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001464}
1465
1466/// visitJumpTableHeader - This function emits necessary code to produce index
1467/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001468void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001469 JumpTableHeader &JTH,
1470 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001471 // Subtract the lowest switch case value from the value being switched on and
1472 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 // difference between smallest and largest cases.
1474 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001475 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001476 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001477 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001478
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001479 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001480 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001481 // can be used as an index into the jump table in a subsequent basic block.
1482 // This value may be smaller or larger than the target's pointer type, and
1483 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001484 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001485
Dan Gohman89496d02010-07-02 00:10:16 +00001486 unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001487 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1488 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001489 JT.Reg = JumpTableReg;
1490
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001491 // Emit the range check for the jump table, and branch to the default block
1492 // for the switch statement if the value being switched on exceeds the largest
1493 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001494 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001495 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001496 DAG.getConstant(JTH.Last-JTH.First,VT),
1497 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001498
1499 // Set NextBlock to be the MBB immediately after the current one, if any.
1500 // This is used to avoid emitting unnecessary branches to the next block.
1501 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001502 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001503
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001504 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001505 NextBlock = BBI;
1506
Dale Johannesen66978ee2009-01-31 02:22:37 +00001507 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001508 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001509 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001510
Bill Wendling4533cac2010-01-28 21:51:40 +00001511 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001512 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1513 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001514
Bill Wendling87710f02009-12-21 23:47:40 +00001515 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001516}
1517
1518/// visitBitTestHeader - This function emits necessary code to produce value
1519/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001520void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1521 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001522 // Subtract the minimum value
1523 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001524 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001525 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001526 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001527
1528 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001529 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001530 TLI.getSetCCResultType(Sub.getValueType()),
1531 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001532 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001533
Bill Wendling87710f02009-12-21 23:47:40 +00001534 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1535 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001536
Dan Gohman89496d02010-07-02 00:10:16 +00001537 B.Reg = FuncInfo.CreateReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001538 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1539 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001540
1541 // Set NextBlock to be the MBB immediately after the current one, if any.
1542 // This is used to avoid emitting unnecessary branches to the next block.
1543 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001544 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001545 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 NextBlock = BBI;
1547
1548 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1549
Dan Gohman99be8ae2010-04-19 22:41:47 +00001550 SwitchBB->addSuccessor(B.Default);
1551 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552
Dale Johannesen66978ee2009-01-31 02:22:37 +00001553 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001555 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001556
Bill Wendling4533cac2010-01-28 21:51:40 +00001557 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001558 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1559 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001560
Bill Wendling87710f02009-12-21 23:47:40 +00001561 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562}
1563
1564/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001565void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1566 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001567 BitTestCase &B,
1568 MachineBasicBlock *SwitchBB) {
Dale Johannesena04b7572009-02-03 23:04:43 +00001569 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001570 TLI.getPointerTy());
Dan Gohman8e0163a2010-06-24 02:06:24 +00001571 SDValue Cmp;
1572 if (CountPopulation_64(B.Mask) == 1) {
1573 // Testing for a single bit; just compare the shift count with what it
1574 // would need to be to shift a 1 bit in that position.
1575 Cmp = DAG.getSetCC(getCurDebugLoc(),
1576 TLI.getSetCCResultType(ShiftOp.getValueType()),
1577 ShiftOp,
1578 DAG.getConstant(CountTrailingZeros_64(B.Mask),
1579 TLI.getPointerTy()),
1580 ISD::SETEQ);
1581 } else {
1582 // Make desired shift
1583 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
1584 TLI.getPointerTy(),
1585 DAG.getConstant(1, TLI.getPointerTy()),
1586 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001587
Dan Gohman8e0163a2010-06-24 02:06:24 +00001588 // Emit bit tests and jumps
1589 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
1590 TLI.getPointerTy(), SwitchVal,
1591 DAG.getConstant(B.Mask, TLI.getPointerTy()));
1592 Cmp = DAG.getSetCC(getCurDebugLoc(),
1593 TLI.getSetCCResultType(AndOp.getValueType()),
1594 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
1595 ISD::SETNE);
1596 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001597
Dan Gohman99be8ae2010-04-19 22:41:47 +00001598 SwitchBB->addSuccessor(B.TargetBB);
1599 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001600
Dale Johannesen66978ee2009-01-31 02:22:37 +00001601 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001603 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001604
1605 // Set NextBlock to be the MBB immediately after the current one, if any.
1606 // This is used to avoid emitting unnecessary branches to the next block.
1607 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001608 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001609 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610 NextBlock = BBI;
1611
Bill Wendling4533cac2010-01-28 21:51:40 +00001612 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001613 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1614 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001615
Bill Wendling87710f02009-12-21 23:47:40 +00001616 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001617}
1618
Dan Gohman46510a72010-04-15 01:51:59 +00001619void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001620 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001621
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622 // Retrieve successors.
1623 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1624 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1625
Gabor Greifb67e6b32009-01-15 11:10:44 +00001626 const Value *Callee(I.getCalledValue());
1627 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001628 visitInlineAsm(&I);
1629 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001630 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001631
1632 // If the value of the invoke is used outside of its defining block, make it
1633 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001634 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001635
1636 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001637 InvokeMBB->addSuccessor(Return);
1638 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001639
1640 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001641 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1642 MVT::Other, getControlRoot(),
1643 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001644}
1645
Dan Gohman46510a72010-04-15 01:51:59 +00001646void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001647}
1648
1649/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1650/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001651bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1652 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001653 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001654 MachineBasicBlock *Default,
1655 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001657
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001658 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001659 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001660 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001661 return false;
1662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001663 // Get the MachineFunction which holds the current MBB. This is used when
1664 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001665 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001666
1667 // Figure out which block is immediately after the current one.
1668 MachineBasicBlock *NextBlock = 0;
1669 MachineFunction::iterator BBI = CR.CaseBB;
1670
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001671 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001672 NextBlock = BBI;
1673
1674 // TODO: If any two of the cases has the same destination, and if one value
1675 // is the same as the other, but has one bit unset that the other has set,
1676 // use bit manipulation to do two compares at once. For example:
1677 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001679 // Rearrange the case blocks so that the last one falls through if possible.
1680 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1681 // The last case block won't fall through into 'NextBlock' if we emit the
1682 // branches in this order. See if rearranging a case value would help.
1683 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1684 if (I->BB == NextBlock) {
1685 std::swap(*I, BackCase);
1686 break;
1687 }
1688 }
1689 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001691 // Create a CaseBlock record representing a conditional branch to
1692 // the Case's target mbb if the value being switched on SV is equal
1693 // to C.
1694 MachineBasicBlock *CurBlock = CR.CaseBB;
1695 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1696 MachineBasicBlock *FallThrough;
1697 if (I != E-1) {
1698 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1699 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001700
1701 // Put SV in a virtual register to make it available from the new blocks.
1702 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001703 } else {
1704 // If the last case doesn't match, go to the default block.
1705 FallThrough = Default;
1706 }
1707
Dan Gohman46510a72010-04-15 01:51:59 +00001708 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001709 ISD::CondCode CC;
1710 if (I->High == I->Low) {
1711 // This is just small small case range :) containing exactly 1 case
1712 CC = ISD::SETEQ;
1713 LHS = SV; RHS = I->High; MHS = NULL;
1714 } else {
1715 CC = ISD::SETLE;
1716 LHS = I->Low; MHS = SV; RHS = I->High;
1717 }
1718 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001719
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001720 // If emitting the first comparison, just call visitSwitchCase to emit the
1721 // code into the current block. Otherwise, push the CaseBlock onto the
1722 // vector to be later processed by SDISel, and insert the node's MBB
1723 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001724 if (CurBlock == SwitchBB)
1725 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 else
1727 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001728
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001729 CurBlock = FallThrough;
1730 }
1731
1732 return true;
1733}
1734
1735static inline bool areJTsAllowed(const TargetLowering &TLI) {
1736 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001737 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1738 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001739}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001740
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001741static APInt ComputeRange(const APInt &First, const APInt &Last) {
1742 APInt LastExt(Last), FirstExt(First);
1743 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1744 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1745 return (LastExt - FirstExt + 1ULL);
1746}
1747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001749bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1750 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001751 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001752 MachineBasicBlock* Default,
1753 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001754 Case& FrontCase = *CR.Range.first;
1755 Case& BackCase = *(CR.Range.second-1);
1756
Chris Lattnere880efe2009-11-07 07:50:34 +00001757 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1758 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001759
Chris Lattnere880efe2009-11-07 07:50:34 +00001760 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001761 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1762 I!=E; ++I)
1763 TSize += I->size();
1764
Dan Gohmane0567812010-04-08 23:03:40 +00001765 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001766 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001767
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001768 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001769 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001770 if (Density < 0.4)
1771 return false;
1772
David Greene4b69d992010-01-05 01:24:57 +00001773 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001774 << "First entry: " << First << ". Last entry: " << Last << '\n'
1775 << "Range: " << Range
1776 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001777
1778 // Get the MachineFunction which holds the current MBB. This is used when
1779 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001780 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781
1782 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001783 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001784 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001785
1786 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1787
1788 // Create a new basic block to hold the code for loading the address
1789 // of the jump table, and jumping to it. Update successor information;
1790 // we will either branch to the default case for the switch, or the jump
1791 // table.
1792 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1793 CurMF->insert(BBI, JumpTableBB);
1794 CR.CaseBB->addSuccessor(Default);
1795 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001796
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001797 // Build a vector of destination BBs, corresponding to each target
1798 // of the jump table. If the value of the jump table slot corresponds to
1799 // a case statement, push the case's BB onto the vector, otherwise, push
1800 // the default BB.
1801 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001802 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001803 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001804 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1805 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001806
1807 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 DestBBs.push_back(I->BB);
1809 if (TEI==High)
1810 ++I;
1811 } else {
1812 DestBBs.push_back(Default);
1813 }
1814 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001815
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001817 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1818 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001819 E = DestBBs.end(); I != E; ++I) {
1820 if (!SuccsHandled[(*I)->getNumber()]) {
1821 SuccsHandled[(*I)->getNumber()] = true;
1822 JumpTableBB->addSuccessor(*I);
1823 }
1824 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001825
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001826 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001827 unsigned JTEncoding = TLI.getJumpTableEncoding();
1828 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001829 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001830
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001831 // Set the jump table information so that we can codegen it as a second
1832 // MachineBasicBlock
1833 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001834 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1835 if (CR.CaseBB == SwitchBB)
1836 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001837
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001838 JTCases.push_back(JumpTableBlock(JTH, JT));
1839
1840 return true;
1841}
1842
1843/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1844/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001845bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1846 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001847 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001848 MachineBasicBlock *Default,
1849 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001850 // Get the MachineFunction which holds the current MBB. This is used when
1851 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001852 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001853
1854 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001856 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001857
1858 Case& FrontCase = *CR.Range.first;
1859 Case& BackCase = *(CR.Range.second-1);
1860 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1861
1862 // Size is the number of Cases represented by this range.
1863 unsigned Size = CR.Range.second - CR.Range.first;
1864
Chris Lattnere880efe2009-11-07 07:50:34 +00001865 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1866 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001867 double FMetric = 0;
1868 CaseItr Pivot = CR.Range.first + Size/2;
1869
1870 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1871 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001872 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001873 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1874 I!=E; ++I)
1875 TSize += I->size();
1876
Chris Lattnere880efe2009-11-07 07:50:34 +00001877 APInt LSize = FrontCase.size();
1878 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001879 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001880 << "First: " << First << ", Last: " << Last <<'\n'
1881 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1883 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001884 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1885 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001886 APInt Range = ComputeRange(LEnd, RBegin);
1887 assert((Range - 2ULL).isNonNegative() &&
1888 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001889 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001890 (LEnd - First + 1ULL).roundToDouble();
1891 double RDensity = (double)RSize.roundToDouble() /
1892 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001893 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001894 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001895 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001896 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1897 << "LDensity: " << LDensity
1898 << ", RDensity: " << RDensity << '\n'
1899 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001900 if (FMetric < Metric) {
1901 Pivot = J;
1902 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001903 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001904 }
1905
1906 LSize += J->size();
1907 RSize -= J->size();
1908 }
1909 if (areJTsAllowed(TLI)) {
1910 // If our case is dense we *really* should handle it earlier!
1911 assert((FMetric > 0) && "Should handle dense range earlier!");
1912 } else {
1913 Pivot = CR.Range.first + Size/2;
1914 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001915
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001916 CaseRange LHSR(CR.Range.first, Pivot);
1917 CaseRange RHSR(Pivot, CR.Range.second);
1918 Constant *C = Pivot->Low;
1919 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001920
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001922 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001923 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001924 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001925 // Pivot's Value, then we can branch directly to the LHS's Target,
1926 // rather than creating a leaf node for it.
1927 if ((LHSR.second - LHSR.first) == 1 &&
1928 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001929 cast<ConstantInt>(C)->getValue() ==
1930 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001931 TrueBB = LHSR.first->BB;
1932 } else {
1933 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1934 CurMF->insert(BBI, TrueBB);
1935 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001936
1937 // Put SV in a virtual register to make it available from the new blocks.
1938 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001939 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001940
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941 // Similar to the optimization above, if the Value being switched on is
1942 // known to be less than the Constant CR.LT, and the current Case Value
1943 // is CR.LT - 1, then we can branch directly to the target block for
1944 // the current Case Value, rather than emitting a RHS leaf node for it.
1945 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001946 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1947 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001948 FalseBB = RHSR.first->BB;
1949 } else {
1950 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1951 CurMF->insert(BBI, FalseBB);
1952 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001953
1954 // Put SV in a virtual register to make it available from the new blocks.
1955 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001956 }
1957
1958 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001959 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001960 // Otherwise, branch to LHS.
1961 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1962
Dan Gohman99be8ae2010-04-19 22:41:47 +00001963 if (CR.CaseBB == SwitchBB)
1964 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001965 else
1966 SwitchCases.push_back(CB);
1967
1968 return true;
1969}
1970
1971/// handleBitTestsSwitchCase - if current case range has few destination and
1972/// range span less, than machine word bitwidth, encode case range into series
1973/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001974bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1975 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001976 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001977 MachineBasicBlock* Default,
1978 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001979 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001980 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001981
1982 Case& FrontCase = *CR.Range.first;
1983 Case& BackCase = *(CR.Range.second-1);
1984
1985 // Get the MachineFunction which holds the current MBB. This is used when
1986 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001987 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001988
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001989 // If target does not have legal shift left, do not emit bit tests at all.
1990 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1991 return false;
1992
Anton Korobeynikov23218582008-12-23 22:25:27 +00001993 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001994 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1995 I!=E; ++I) {
1996 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001997 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001998 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001999
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002000 // Count unique destinations
2001 SmallSet<MachineBasicBlock*, 4> Dests;
2002 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2003 Dests.insert(I->BB);
2004 if (Dests.size() > 3)
2005 // Don't bother the code below, if there are too much unique destinations
2006 return false;
2007 }
David Greene4b69d992010-01-05 01:24:57 +00002008 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002009 << Dests.size() << '\n'
2010 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002011
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002012 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002013 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2014 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002015 APInt cmpRange = maxValue - minValue;
2016
David Greene4b69d992010-01-05 01:24:57 +00002017 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002018 << "Low bound: " << minValue << '\n'
2019 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002020
Dan Gohmane0567812010-04-08 23:03:40 +00002021 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002022 (!(Dests.size() == 1 && numCmps >= 3) &&
2023 !(Dests.size() == 2 && numCmps >= 5) &&
2024 !(Dests.size() >= 3 && numCmps >= 6)))
2025 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002026
David Greene4b69d992010-01-05 01:24:57 +00002027 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002028 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002030 // Optimize the case where all the case values fit in a
2031 // word without having to subtract minValue. In this case,
2032 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00002033 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002034 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002035 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002036 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002037 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002038
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039 CaseBitsVector CasesBits;
2040 unsigned i, count = 0;
2041
2042 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2043 MachineBasicBlock* Dest = I->BB;
2044 for (i = 0; i < count; ++i)
2045 if (Dest == CasesBits[i].BB)
2046 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002047
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002048 if (i == count) {
2049 assert((count < 3) && "Too much destinations to test!");
2050 CasesBits.push_back(CaseBits(0, Dest, 0));
2051 count++;
2052 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002053
2054 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2055 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2056
2057 uint64_t lo = (lowValue - lowBound).getZExtValue();
2058 uint64_t hi = (highValue - lowBound).getZExtValue();
2059
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002060 for (uint64_t j = lo; j <= hi; j++) {
2061 CasesBits[i].Mask |= 1ULL << j;
2062 CasesBits[i].Bits++;
2063 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002064
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002065 }
2066 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002068 BitTestInfo BTC;
2069
2070 // Figure out which block is immediately after the current one.
2071 MachineFunction::iterator BBI = CR.CaseBB;
2072 ++BBI;
2073
2074 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2075
David Greene4b69d992010-01-05 01:24:57 +00002076 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002077 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002078 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002079 << ", Bits: " << CasesBits[i].Bits
2080 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081
2082 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2083 CurMF->insert(BBI, CaseBB);
2084 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2085 CaseBB,
2086 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002087
2088 // Put SV in a virtual register to make it available from the new blocks.
2089 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002090 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002091
2092 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002093 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002094 CR.CaseBB, Default, BTC);
2095
Dan Gohman99be8ae2010-04-19 22:41:47 +00002096 if (CR.CaseBB == SwitchBB)
2097 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002099 BitTestCases.push_back(BTB);
2100
2101 return true;
2102}
2103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002104/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002105size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2106 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002107 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002108
2109 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002110 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002111 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2112 Cases.push_back(Case(SI.getSuccessorValue(i),
2113 SI.getSuccessorValue(i),
2114 SMBB));
2115 }
2116 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2117
2118 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002119 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002120 // Must recompute end() each iteration because it may be
2121 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002122 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2123 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2124 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002125 MachineBasicBlock* nextBB = J->BB;
2126 MachineBasicBlock* currentBB = I->BB;
2127
2128 // If the two neighboring cases go to the same destination, merge them
2129 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002130 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002131 I->High = J->High;
2132 J = Cases.erase(J);
2133 } else {
2134 I = J++;
2135 }
2136 }
2137
2138 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2139 if (I->Low != I->High)
2140 // A range counts double, since it requires two compares.
2141 ++numCmps;
2142 }
2143
2144 return numCmps;
2145}
2146
Dan Gohman46510a72010-04-15 01:51:59 +00002147void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002148 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002149
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002150 // Figure out which block is immediately after the current one.
2151 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002152 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2153
2154 // If there is only the default destination, branch to it if it is not the
2155 // next basic block. Otherwise, just fall through.
2156 if (SI.getNumOperands() == 2) {
2157 // Update machine-CFG edges.
2158
2159 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002160 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002161 if (Default != NextBlock)
2162 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
2163 MVT::Other, getControlRoot(),
2164 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166 return;
2167 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002169 // If there are any non-default case statements, create a vector of Cases
2170 // representing each one, and sort the vector so that we can efficiently
2171 // create a binary search tree from them.
2172 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002173 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002174 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002175 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002176 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002177
2178 // Get the Value to be switched on and default basic blocks, which will be
2179 // inserted into CaseBlock records, representing basic blocks in the binary
2180 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002181 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002182
2183 // Push the initial CaseRec onto the worklist
2184 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002185 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2186 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187
2188 while (!WorkList.empty()) {
2189 // Grab a record representing a case range to process off the worklist
2190 CaseRec CR = WorkList.back();
2191 WorkList.pop_back();
2192
Dan Gohman99be8ae2010-04-19 22:41:47 +00002193 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002194 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002196 // If the range has few cases (two or less) emit a series of specific
2197 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002198 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002199 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002200
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002201 // If the switch has more than 5 blocks, and at least 40% dense, and the
2202 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002203 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002204 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002206
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2208 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002209 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002210 }
2211}
2212
Dan Gohman46510a72010-04-15 01:51:59 +00002213void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002214 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002215
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002216 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002217 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002218 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002219 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002220 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002221 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002222 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2223 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002224 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002225
Bill Wendling4533cac2010-01-28 21:51:40 +00002226 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2227 MVT::Other, getControlRoot(),
2228 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002229}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002230
Dan Gohman46510a72010-04-15 01:51:59 +00002231void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002232 // -0.0 - X --> fneg
2233 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002234 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2236 const VectorType *DestTy = cast<VectorType>(I.getType());
2237 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002238 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002239 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002240 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002241 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002242 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002243 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2244 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002245 return;
2246 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002247 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002248 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002249
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002250 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002251 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002252 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002253 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2254 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002255 return;
2256 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002257
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002258 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002259}
2260
Dan Gohman46510a72010-04-15 01:51:59 +00002261void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002262 SDValue Op1 = getValue(I.getOperand(0));
2263 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002264 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2265 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002266}
2267
Dan Gohman46510a72010-04-15 01:51:59 +00002268void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002269 SDValue Op1 = getValue(I.getOperand(0));
2270 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002271 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002272 Op2.getValueType() != TLI.getShiftAmountTy()) {
2273 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002274 EVT PTy = TLI.getPointerTy();
2275 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002276 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002277 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2278 TLI.getShiftAmountTy(), Op2);
2279 // If the operand is larger than the shift count type but the shift
2280 // count type has enough bits to represent any shift value, truncate
2281 // it now. This is a common case and it exposes the truncate to
2282 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002283 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002284 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2285 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2286 TLI.getShiftAmountTy(), Op2);
2287 // Otherwise we'll need to temporarily settle for some other
2288 // convenient type; type legalization will make adjustments as
2289 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002290 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002291 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002292 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002293 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002294 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002295 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002296 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002297
Bill Wendling4533cac2010-01-28 21:51:40 +00002298 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2299 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002300}
2301
Dan Gohman46510a72010-04-15 01:51:59 +00002302void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002303 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002304 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002305 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002306 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307 predicate = ICmpInst::Predicate(IC->getPredicate());
2308 SDValue Op1 = getValue(I.getOperand(0));
2309 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002310 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002311
Owen Andersone50ed302009-08-10 22:56:29 +00002312 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002313 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002314}
2315
Dan Gohman46510a72010-04-15 01:51:59 +00002316void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002317 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002318 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002319 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002320 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002321 predicate = FCmpInst::Predicate(FC->getPredicate());
2322 SDValue Op1 = getValue(I.getOperand(0));
2323 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002324 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002325 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002326 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002327}
2328
Dan Gohman46510a72010-04-15 01:51:59 +00002329void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002330 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002331 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2332 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002333 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002334
Bill Wendling49fcff82009-12-21 22:30:11 +00002335 SmallVector<SDValue, 4> Values(NumValues);
2336 SDValue Cond = getValue(I.getOperand(0));
2337 SDValue TrueVal = getValue(I.getOperand(1));
2338 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002339
Bill Wendling4533cac2010-01-28 21:51:40 +00002340 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002341 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002342 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2343 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002344 SDValue(TrueVal.getNode(),
2345 TrueVal.getResNo() + i),
2346 SDValue(FalseVal.getNode(),
2347 FalseVal.getResNo() + i));
2348
Bill Wendling4533cac2010-01-28 21:51:40 +00002349 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2350 DAG.getVTList(&ValueVTs[0], NumValues),
2351 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002352}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002353
Dan Gohman46510a72010-04-15 01:51:59 +00002354void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002355 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2356 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002357 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002358 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002359}
2360
Dan Gohman46510a72010-04-15 01:51:59 +00002361void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002362 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2363 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2364 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002365 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002366 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002367}
2368
Dan Gohman46510a72010-04-15 01:51:59 +00002369void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002370 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2371 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2372 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002373 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002374 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002375}
2376
Dan Gohman46510a72010-04-15 01:51:59 +00002377void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002378 // FPTrunc is never a no-op cast, no need to check
2379 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002380 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002381 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2382 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002383}
2384
Dan Gohman46510a72010-04-15 01:51:59 +00002385void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002386 // FPTrunc is never a no-op cast, no need to check
2387 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002388 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002389 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002390}
2391
Dan Gohman46510a72010-04-15 01:51:59 +00002392void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002393 // FPToUI is never a no-op cast, no need to check
2394 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002395 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002396 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002397}
2398
Dan Gohman46510a72010-04-15 01:51:59 +00002399void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002400 // FPToSI is never a no-op cast, no need to check
2401 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002402 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002403 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002404}
2405
Dan Gohman46510a72010-04-15 01:51:59 +00002406void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002407 // UIToFP is never a no-op cast, no need to check
2408 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002409 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002410 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002411}
2412
Dan Gohman46510a72010-04-15 01:51:59 +00002413void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002414 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002415 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002416 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002417 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002418}
2419
Dan Gohman46510a72010-04-15 01:51:59 +00002420void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002421 // What to do depends on the size of the integer and the size of the pointer.
2422 // We can either truncate, zero extend, or no-op, accordingly.
2423 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002424 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002425 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002426}
2427
Dan Gohman46510a72010-04-15 01:51:59 +00002428void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002429 // What to do depends on the size of the integer and the size of the pointer.
2430 // We can either truncate, zero extend, or no-op, accordingly.
2431 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002432 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002433 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002434}
2435
Dan Gohman46510a72010-04-15 01:51:59 +00002436void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002437 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002438 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002439
Bill Wendling49fcff82009-12-21 22:30:11 +00002440 // BitCast assures us that source and destination are the same size so this is
2441 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002442 if (DestVT != N.getValueType())
2443 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2444 DestVT, N)); // convert types.
2445 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002446 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002447}
2448
Dan Gohman46510a72010-04-15 01:51:59 +00002449void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002450 SDValue InVec = getValue(I.getOperand(0));
2451 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002452 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002453 TLI.getPointerTy(),
2454 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002455 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2456 TLI.getValueType(I.getType()),
2457 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002458}
2459
Dan Gohman46510a72010-04-15 01:51:59 +00002460void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002461 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002462 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002463 TLI.getPointerTy(),
2464 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002465 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2466 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002467}
2468
Mon P Wangaeb06d22008-11-10 04:46:22 +00002469// Utility for visitShuffleVector - Returns true if the mask is mask starting
2470// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002471static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2472 unsigned MaskNumElts = Mask.size();
2473 for (unsigned i = 0; i != MaskNumElts; ++i)
2474 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002475 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002476 return true;
2477}
2478
Dan Gohman46510a72010-04-15 01:51:59 +00002479void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002480 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002481 SDValue Src1 = getValue(I.getOperand(0));
2482 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002483
Nate Begeman9008ca62009-04-27 18:41:29 +00002484 // Convert the ConstantVector mask operand into an array of ints, with -1
2485 // representing undef values.
2486 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002487 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002488 unsigned MaskNumElts = MaskElts.size();
2489 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002490 if (isa<UndefValue>(MaskElts[i]))
2491 Mask.push_back(-1);
2492 else
2493 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2494 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002495
Owen Andersone50ed302009-08-10 22:56:29 +00002496 EVT VT = TLI.getValueType(I.getType());
2497 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002498 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002499
Mon P Wangc7849c22008-11-16 05:06:27 +00002500 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002501 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2502 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002503 return;
2504 }
2505
2506 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002507 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2508 // Mask is longer than the source vectors and is a multiple of the source
2509 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002510 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002511 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2512 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002513 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2514 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002515 return;
2516 }
2517
Mon P Wangc7849c22008-11-16 05:06:27 +00002518 // Pad both vectors with undefs to make them the same length as the mask.
2519 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002520 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2521 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002522 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002523
Nate Begeman9008ca62009-04-27 18:41:29 +00002524 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2525 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002526 MOps1[0] = Src1;
2527 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002528
2529 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2530 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002531 &MOps1[0], NumConcat);
2532 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002533 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002534 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002535
Mon P Wangaeb06d22008-11-10 04:46:22 +00002536 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002537 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002538 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002539 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002540 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002541 MappedOps.push_back(Idx);
2542 else
2543 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002544 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002545
Bill Wendling4533cac2010-01-28 21:51:40 +00002546 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2547 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002548 return;
2549 }
2550
Mon P Wangc7849c22008-11-16 05:06:27 +00002551 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002552 // Analyze the access pattern of the vector to see if we can extract
2553 // two subvectors and do the shuffle. The analysis is done by calculating
2554 // the range of elements the mask access on both vectors.
2555 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2556 int MaxRange[2] = {-1, -1};
2557
Nate Begeman5a5ca152009-04-29 05:20:52 +00002558 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002559 int Idx = Mask[i];
2560 int Input = 0;
2561 if (Idx < 0)
2562 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002563
Nate Begeman5a5ca152009-04-29 05:20:52 +00002564 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002565 Input = 1;
2566 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002567 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002568 if (Idx > MaxRange[Input])
2569 MaxRange[Input] = Idx;
2570 if (Idx < MinRange[Input])
2571 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002572 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002573
Mon P Wangc7849c22008-11-16 05:06:27 +00002574 // Check if the access is smaller than the vector size and can we find
2575 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002576 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2577 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002578 int StartIdx[2]; // StartIdx to extract from
2579 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002580 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002581 RangeUse[Input] = 0; // Unused
2582 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002583 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002584 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002585 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002586 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002587 RangeUse[Input] = 1; // Extract from beginning of the vector
2588 StartIdx[Input] = 0;
2589 } else {
2590 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002591 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002592 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002593 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002594 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002595 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002596 }
2597
Bill Wendling636e2582009-08-21 18:16:06 +00002598 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002599 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002600 return;
2601 }
2602 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2603 // Extract appropriate subvector and generate a vector shuffle
2604 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002605 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002606 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002607 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002608 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002609 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002610 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002611 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002612
Mon P Wangc7849c22008-11-16 05:06:27 +00002613 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002614 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002615 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002616 int Idx = Mask[i];
2617 if (Idx < 0)
2618 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002619 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002620 MappedOps.push_back(Idx - StartIdx[0]);
2621 else
2622 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002623 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002624
Bill Wendling4533cac2010-01-28 21:51:40 +00002625 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2626 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002627 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002628 }
2629 }
2630
Mon P Wangc7849c22008-11-16 05:06:27 +00002631 // We can't use either concat vectors or extract subvectors so fall back to
2632 // replacing the shuffle with extract and build vector.
2633 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002634 EVT EltVT = VT.getVectorElementType();
2635 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002636 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002637 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002638 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002639 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002640 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002641 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002642 SDValue Res;
2643
Nate Begeman5a5ca152009-04-29 05:20:52 +00002644 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002645 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2646 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002647 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002648 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2649 EltVT, Src2,
2650 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2651
2652 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002653 }
2654 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002655
Bill Wendling4533cac2010-01-28 21:51:40 +00002656 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2657 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002658}
2659
Dan Gohman46510a72010-04-15 01:51:59 +00002660void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002661 const Value *Op0 = I.getOperand(0);
2662 const Value *Op1 = I.getOperand(1);
2663 const Type *AggTy = I.getType();
2664 const Type *ValTy = Op1->getType();
2665 bool IntoUndef = isa<UndefValue>(Op0);
2666 bool FromUndef = isa<UndefValue>(Op1);
2667
2668 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2669 I.idx_begin(), I.idx_end());
2670
Owen Andersone50ed302009-08-10 22:56:29 +00002671 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002672 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002673 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002674 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2675
2676 unsigned NumAggValues = AggValueVTs.size();
2677 unsigned NumValValues = ValValueVTs.size();
2678 SmallVector<SDValue, 4> Values(NumAggValues);
2679
2680 SDValue Agg = getValue(Op0);
2681 SDValue Val = getValue(Op1);
2682 unsigned i = 0;
2683 // Copy the beginning value(s) from the original aggregate.
2684 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002685 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002686 SDValue(Agg.getNode(), Agg.getResNo() + i);
2687 // Copy values from the inserted value(s).
2688 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002689 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002690 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2691 // Copy remaining value(s) from the original aggregate.
2692 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002693 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002694 SDValue(Agg.getNode(), Agg.getResNo() + i);
2695
Bill Wendling4533cac2010-01-28 21:51:40 +00002696 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2697 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2698 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002699}
2700
Dan Gohman46510a72010-04-15 01:51:59 +00002701void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002702 const Value *Op0 = I.getOperand(0);
2703 const Type *AggTy = Op0->getType();
2704 const Type *ValTy = I.getType();
2705 bool OutOfUndef = isa<UndefValue>(Op0);
2706
2707 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2708 I.idx_begin(), I.idx_end());
2709
Owen Andersone50ed302009-08-10 22:56:29 +00002710 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2712
2713 unsigned NumValValues = ValValueVTs.size();
2714 SmallVector<SDValue, 4> Values(NumValValues);
2715
2716 SDValue Agg = getValue(Op0);
2717 // Copy out the selected value(s).
2718 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2719 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002720 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002721 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002722 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002723
Bill Wendling4533cac2010-01-28 21:51:40 +00002724 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2725 DAG.getVTList(&ValValueVTs[0], NumValValues),
2726 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002727}
2728
Dan Gohman46510a72010-04-15 01:51:59 +00002729void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002730 SDValue N = getValue(I.getOperand(0));
2731 const Type *Ty = I.getOperand(0)->getType();
2732
Dan Gohman46510a72010-04-15 01:51:59 +00002733 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002734 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002735 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002736 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2737 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2738 if (Field) {
2739 // N = N + Offset
2740 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002741 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002742 DAG.getIntPtrConstant(Offset));
2743 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002744
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002745 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002746 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2747 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2748
2749 // Offset canonically 0 for unions, but type changes
2750 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002751 } else {
2752 Ty = cast<SequentialType>(Ty)->getElementType();
2753
2754 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002755 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00002756 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002757 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002758 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002759 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002760 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002761 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002762 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002763 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2764 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002765 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002766 else
Evan Chengb1032a82009-02-09 20:54:38 +00002767 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002768
Dale Johannesen66978ee2009-01-31 02:22:37 +00002769 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002770 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002771 continue;
2772 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002773
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002774 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002775 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2776 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002777 SDValue IdxN = getValue(Idx);
2778
2779 // If the index is smaller or larger than intptr_t, truncate or extend
2780 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002781 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002782
2783 // If this is a multiply by a power of two, turn it into a shl
2784 // immediately. This is a very common case.
2785 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002786 if (ElementSize.isPowerOf2()) {
2787 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002788 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002789 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002790 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002791 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002792 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002793 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002794 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002795 }
2796 }
2797
Scott Michelfdc40a02009-02-17 22:15:04 +00002798 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002799 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002800 }
2801 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002802
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002803 setValue(&I, N);
2804}
2805
Dan Gohman46510a72010-04-15 01:51:59 +00002806void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807 // If this is a fixed sized alloca in the entry block of the function,
2808 // allocate it statically on the stack.
2809 if (FuncInfo.StaticAllocaMap.count(&I))
2810 return; // getValue will auto-populate this.
2811
2812 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002813 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002814 unsigned Align =
2815 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2816 I.getAlignment());
2817
2818 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002819
Owen Andersone50ed302009-08-10 22:56:29 +00002820 EVT IntPtr = TLI.getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00002821 if (AllocSize.getValueType() != IntPtr)
2822 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
2823
2824 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr,
2825 AllocSize,
2826 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002828 // Handle alignment. If the requested alignment is less than or equal to
2829 // the stack alignment, ignore it. If the size is greater than or equal to
2830 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002831 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 if (Align <= StackAlign)
2833 Align = 0;
2834
2835 // Round the size of the allocation up to the stack alignment size
2836 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002837 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002838 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002839 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002840
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002841 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002842 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002843 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002844 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2845
2846 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002847 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002848 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002849 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002850 setValue(&I, DSA);
2851 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002852
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002853 // Inform the Frame Information that we have just allocated a variable-sized
2854 // object.
Eric Christopher2b8271e2010-07-17 00:28:22 +00002855 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002856}
2857
Dan Gohman46510a72010-04-15 01:51:59 +00002858void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002859 const Value *SV = I.getOperand(0);
2860 SDValue Ptr = getValue(SV);
2861
2862 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002863
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002864 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002865 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002866 unsigned Alignment = I.getAlignment();
2867
Owen Andersone50ed302009-08-10 22:56:29 +00002868 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869 SmallVector<uint64_t, 4> Offsets;
2870 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2871 unsigned NumValues = ValueVTs.size();
2872 if (NumValues == 0)
2873 return;
2874
2875 SDValue Root;
2876 bool ConstantMemory = false;
2877 if (I.isVolatile())
2878 // Serialize volatile loads with other side effects.
2879 Root = getRoot();
2880 else if (AA->pointsToConstantMemory(SV)) {
2881 // Do not serialize (non-volatile) loads of constant memory with anything.
2882 Root = DAG.getEntryNode();
2883 ConstantMemory = true;
2884 } else {
2885 // Do not serialize non-volatile loads against each other.
2886 Root = DAG.getRoot();
2887 }
2888
2889 SmallVector<SDValue, 4> Values(NumValues);
2890 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002891 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002892 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002893 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2894 PtrVT, Ptr,
2895 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002896 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002897 A, SV, Offsets[i], isVolatile,
2898 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002899
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 Values[i] = L;
2901 Chains[i] = L.getValue(1);
2902 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002903
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002904 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002905 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002906 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002907 if (isVolatile)
2908 DAG.setRoot(Chain);
2909 else
2910 PendingLoads.push_back(Chain);
2911 }
2912
Bill Wendling4533cac2010-01-28 21:51:40 +00002913 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2914 DAG.getVTList(&ValueVTs[0], NumValues),
2915 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002916}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002917
Dan Gohman46510a72010-04-15 01:51:59 +00002918void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2919 const Value *SrcV = I.getOperand(0);
2920 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002921
Owen Andersone50ed302009-08-10 22:56:29 +00002922 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002923 SmallVector<uint64_t, 4> Offsets;
2924 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2925 unsigned NumValues = ValueVTs.size();
2926 if (NumValues == 0)
2927 return;
2928
2929 // Get the lowered operands. Note that we do this after
2930 // checking if NumResults is zero, because with zero results
2931 // the operands won't have values in the map.
2932 SDValue Src = getValue(SrcV);
2933 SDValue Ptr = getValue(PtrV);
2934
2935 SDValue Root = getRoot();
2936 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002937 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002938 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002939 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002940 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002941
2942 for (unsigned i = 0; i != NumValues; ++i) {
2943 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2944 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002945 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002946 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002947 Add, PtrV, Offsets[i], isVolatile,
2948 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002949 }
2950
Bill Wendling4533cac2010-01-28 21:51:40 +00002951 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2952 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002953}
2954
2955/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2956/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002957void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002958 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002959 bool HasChain = !I.doesNotAccessMemory();
2960 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2961
2962 // Build the operand list.
2963 SmallVector<SDValue, 8> Ops;
2964 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2965 if (OnlyLoad) {
2966 // We don't need to serialize loads against other loads.
2967 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002968 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002969 Ops.push_back(getRoot());
2970 }
2971 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002972
2973 // Info is set by getTgtMemInstrinsic
2974 TargetLowering::IntrinsicInfo Info;
2975 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2976
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002977 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002978 if (!IsTgtIntrinsic)
2979 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002980
2981 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00002982 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
2983 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002984 assert(TLI.isTypeLegal(Op.getValueType()) &&
2985 "Intrinsic uses a non-legal type?");
2986 Ops.push_back(Op);
2987 }
2988
Owen Andersone50ed302009-08-10 22:56:29 +00002989 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002990 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2991#ifndef NDEBUG
2992 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2993 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2994 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002995 }
Bob Wilson8d919552009-07-31 22:41:21 +00002996#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002997
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002998 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002999 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003000
Bob Wilson8d919552009-07-31 22:41:21 +00003001 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003002
3003 // Create the node.
3004 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003005 if (IsTgtIntrinsic) {
3006 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003007 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003008 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003009 Info.memVT, Info.ptrVal, Info.offset,
3010 Info.align, Info.vol,
3011 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003012 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003013 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003014 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003015 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003016 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003017 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003018 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003019 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003020 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003021 }
3022
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003023 if (HasChain) {
3024 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3025 if (OnlyLoad)
3026 PendingLoads.push_back(Chain);
3027 else
3028 DAG.setRoot(Chain);
3029 }
Bill Wendling856ff412009-12-22 00:12:37 +00003030
Benjamin Kramerf0127052010-01-05 13:12:22 +00003031 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003032 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003033 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003034 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003035 }
Bill Wendling856ff412009-12-22 00:12:37 +00003036
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003037 setValue(&I, Result);
3038 }
3039}
3040
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003041/// GetSignificand - Get the significand and build it into a floating-point
3042/// number with exponent of 1:
3043///
3044/// Op = (Op & 0x007fffff) | 0x3f800000;
3045///
3046/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003047static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00003048GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3050 DAG.getConstant(0x007fffff, MVT::i32));
3051 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3052 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003053 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003054}
3055
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003056/// GetExponent - Get the exponent:
3057///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003058/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003059///
3060/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003061static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003062GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00003063 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003064 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3065 DAG.getConstant(0x7f800000, MVT::i32));
3066 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003067 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003068 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3069 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003070 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003071}
3072
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003073/// getF32Constant - Get 32-bit floating point constant.
3074static SDValue
3075getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003076 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003077}
3078
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003079/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003080/// visitIntrinsicCall: I is a call instruction
3081/// Op is the associated NodeType for I
3082const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003083SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
3084 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003085 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003086 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003087 DAG.getAtomic(Op, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00003088 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003089 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00003090 getValue(I.getArgOperand(0)),
3091 getValue(I.getArgOperand(1)),
3092 I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003093 setValue(&I, L);
3094 DAG.setRoot(L.getValue(1));
3095 return 0;
3096}
3097
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003098// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003099const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003100SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Gabor Greif0635f352010-06-25 09:38:13 +00003101 SDValue Op1 = getValue(I.getArgOperand(0));
3102 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling74c37652008-12-09 22:08:41 +00003103
Owen Anderson825b72b2009-08-11 20:47:22 +00003104 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00003105 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003106 return 0;
3107}
Bill Wendling74c37652008-12-09 22:08:41 +00003108
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003109/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3110/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003111void
Dan Gohman46510a72010-04-15 01:51:59 +00003112SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003113 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003114 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003115
Gabor Greif0635f352010-06-25 09:38:13 +00003116 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003117 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003118 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003119
3120 // Put the exponent in the right bit position for later addition to the
3121 // final result:
3122 //
3123 // #define LOG2OFe 1.4426950f
3124 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003126 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003128
3129 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3131 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003132
3133 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003134 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003135 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003136
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003137 if (LimitFloatPrecision <= 6) {
3138 // For floating-point precision of 6:
3139 //
3140 // TwoToFractionalPartOfX =
3141 // 0.997535578f +
3142 // (0.735607626f + 0.252464424f * x) * x;
3143 //
3144 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003145 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003146 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003148 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3150 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003151 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003153
3154 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003156 TwoToFracPartOfX, IntegerPartOfX);
3157
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003159 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3160 // For floating-point precision of 12:
3161 //
3162 // TwoToFractionalPartOfX =
3163 // 0.999892986f +
3164 // (0.696457318f +
3165 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3166 //
3167 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003169 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003171 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003172 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3173 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003174 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3176 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003177 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003178 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003179
3180 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003181 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003182 TwoToFracPartOfX, IntegerPartOfX);
3183
Owen Anderson825b72b2009-08-11 20:47:22 +00003184 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003185 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3186 // For floating-point precision of 18:
3187 //
3188 // TwoToFractionalPartOfX =
3189 // 0.999999982f +
3190 // (0.693148872f +
3191 // (0.240227044f +
3192 // (0.554906021e-1f +
3193 // (0.961591928e-2f +
3194 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3195 //
3196 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003197 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003198 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003200 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003201 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3202 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003203 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003204 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3205 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003206 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003207 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3208 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003209 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003210 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3211 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003212 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003213 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3214 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003215 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003216 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003218
3219 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003220 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003221 TwoToFracPartOfX, IntegerPartOfX);
3222
Owen Anderson825b72b2009-08-11 20:47:22 +00003223 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003224 }
3225 } else {
3226 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003227 result = DAG.getNode(ISD::FEXP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003228 getValue(I.getArgOperand(0)).getValueType(),
3229 getValue(I.getArgOperand(0)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003230 }
3231
Dale Johannesen59e577f2008-09-05 18:38:42 +00003232 setValue(&I, result);
3233}
3234
Bill Wendling39150252008-09-09 20:39:27 +00003235/// visitLog - Lower a log intrinsic. Handles the special sequences for
3236/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003237void
Dan Gohman46510a72010-04-15 01:51:59 +00003238SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003239 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003240 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003241
Gabor Greif0635f352010-06-25 09:38:13 +00003242 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003243 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003244 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003246
3247 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003248 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003249 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003250 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003251
3252 // Get the significand and build it into a floating-point number with
3253 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003254 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003255
3256 if (LimitFloatPrecision <= 6) {
3257 // For floating-point precision of 6:
3258 //
3259 // LogofMantissa =
3260 // -1.1609546f +
3261 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003262 //
Bill Wendling39150252008-09-09 20:39:27 +00003263 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003264 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003265 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003266 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003267 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003268 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3269 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003270 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003271
Scott Michelfdc40a02009-02-17 22:15:04 +00003272 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003273 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003274 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3275 // For floating-point precision of 12:
3276 //
3277 // LogOfMantissa =
3278 // -1.7417939f +
3279 // (2.8212026f +
3280 // (-1.4699568f +
3281 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3282 //
3283 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003284 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003285 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003286 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003287 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003288 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3289 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003290 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3292 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003293 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3295 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003296 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003297
Scott Michelfdc40a02009-02-17 22:15:04 +00003298 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003299 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003300 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3301 // For floating-point precision of 18:
3302 //
3303 // LogOfMantissa =
3304 // -2.1072184f +
3305 // (4.2372794f +
3306 // (-3.7029485f +
3307 // (2.2781945f +
3308 // (-0.87823314f +
3309 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3310 //
3311 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003312 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003313 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003315 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3317 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003318 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3320 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003321 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003322 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3323 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003324 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003325 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3326 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003327 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3329 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003330 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003331
Scott Michelfdc40a02009-02-17 22:15:04 +00003332 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003333 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003334 }
3335 } else {
3336 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003337 result = DAG.getNode(ISD::FLOG, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003338 getValue(I.getArgOperand(0)).getValueType(),
3339 getValue(I.getArgOperand(0)));
Bill Wendling39150252008-09-09 20:39:27 +00003340 }
3341
Dale Johannesen59e577f2008-09-05 18:38:42 +00003342 setValue(&I, result);
3343}
3344
Bill Wendling3eb59402008-09-09 00:28:24 +00003345/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3346/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003347void
Dan Gohman46510a72010-04-15 01:51:59 +00003348SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003349 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003350 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003351
Gabor Greif0635f352010-06-25 09:38:13 +00003352 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003353 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003354 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003356
Bill Wendling39150252008-09-09 20:39:27 +00003357 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003358 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003359
Bill Wendling3eb59402008-09-09 00:28:24 +00003360 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003361 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003362 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003363
Bill Wendling3eb59402008-09-09 00:28:24 +00003364 // Different possible minimax approximations of significand in
3365 // floating-point for various degrees of accuracy over [1,2].
3366 if (LimitFloatPrecision <= 6) {
3367 // For floating-point precision of 6:
3368 //
3369 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3370 //
3371 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003373 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003374 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003375 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3377 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003378 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003379
Scott Michelfdc40a02009-02-17 22:15:04 +00003380 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003381 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003382 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3383 // For floating-point precision of 12:
3384 //
3385 // Log2ofMantissa =
3386 // -2.51285454f +
3387 // (4.07009056f +
3388 // (-2.12067489f +
3389 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003390 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003391 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003393 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003394 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003395 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3397 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003398 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3400 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003401 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3403 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003404 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003405
Scott Michelfdc40a02009-02-17 22:15:04 +00003406 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003407 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003408 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3409 // For floating-point precision of 18:
3410 //
3411 // Log2ofMantissa =
3412 // -3.0400495f +
3413 // (6.1129976f +
3414 // (-5.3420409f +
3415 // (3.2865683f +
3416 // (-1.2669343f +
3417 // (0.27515199f -
3418 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3419 //
3420 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003422 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003424 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3426 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003427 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003428 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3429 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003430 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003431 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3432 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003433 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3435 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003436 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3438 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003439 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003440
Scott Michelfdc40a02009-02-17 22:15:04 +00003441 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003442 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003443 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003444 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003445 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003446 result = DAG.getNode(ISD::FLOG2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003447 getValue(I.getArgOperand(0)).getValueType(),
3448 getValue(I.getArgOperand(0)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003449 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003450
Dale Johannesen59e577f2008-09-05 18:38:42 +00003451 setValue(&I, result);
3452}
3453
Bill Wendling3eb59402008-09-09 00:28:24 +00003454/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3455/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003456void
Dan Gohman46510a72010-04-15 01:51:59 +00003457SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003458 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003459 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003460
Gabor Greif0635f352010-06-25 09:38:13 +00003461 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003462 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003463 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003465
Bill Wendling39150252008-09-09 20:39:27 +00003466 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003467 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003468 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003469 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003470
3471 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003472 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003473 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003474
3475 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003476 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003477 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003478 // Log10ofMantissa =
3479 // -0.50419619f +
3480 // (0.60948995f - 0.10380950f * x) * x;
3481 //
3482 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003484 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003485 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003486 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003487 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3488 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003489 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003490
Scott Michelfdc40a02009-02-17 22:15:04 +00003491 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003492 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003493 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3494 // For floating-point precision of 12:
3495 //
3496 // Log10ofMantissa =
3497 // -0.64831180f +
3498 // (0.91751397f +
3499 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3500 //
3501 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003502 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003503 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003505 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003506 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3507 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003508 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003509 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3510 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003511 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003512
Scott Michelfdc40a02009-02-17 22:15:04 +00003513 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003515 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003516 // For floating-point precision of 18:
3517 //
3518 // Log10ofMantissa =
3519 // -0.84299375f +
3520 // (1.5327582f +
3521 // (-1.0688956f +
3522 // (0.49102474f +
3523 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3524 //
3525 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003527 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003528 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003529 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3531 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003532 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3534 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003535 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3537 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003538 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3540 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003541 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003542
Scott Michelfdc40a02009-02-17 22:15:04 +00003543 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003545 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003546 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003547 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003548 result = DAG.getNode(ISD::FLOG10, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003549 getValue(I.getArgOperand(0)).getValueType(),
3550 getValue(I.getArgOperand(0)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003551 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003552
Dale Johannesen59e577f2008-09-05 18:38:42 +00003553 setValue(&I, result);
3554}
3555
Bill Wendlinge10c8142008-09-09 22:39:21 +00003556/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3557/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003558void
Dan Gohman46510a72010-04-15 01:51:59 +00003559SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003560 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003561 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003562
Gabor Greif0635f352010-06-25 09:38:13 +00003563 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003564 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003565 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003566
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003568
3569 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3571 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003572
3573 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003575 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003576
3577 if (LimitFloatPrecision <= 6) {
3578 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003579 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003580 // TwoToFractionalPartOfX =
3581 // 0.997535578f +
3582 // (0.735607626f + 0.252464424f * x) * x;
3583 //
3584 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003585 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003586 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003587 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003588 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003589 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3590 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003591 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003593 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003595
Scott Michelfdc40a02009-02-17 22:15:04 +00003596 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003598 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3599 // For floating-point precision of 12:
3600 //
3601 // TwoToFractionalPartOfX =
3602 // 0.999892986f +
3603 // (0.696457318f +
3604 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3605 //
3606 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003607 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003608 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003610 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003611 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3612 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003613 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3615 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003616 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003617 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003618 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003619 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003620
Scott Michelfdc40a02009-02-17 22:15:04 +00003621 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003622 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003623 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3624 // For floating-point precision of 18:
3625 //
3626 // TwoToFractionalPartOfX =
3627 // 0.999999982f +
3628 // (0.693148872f +
3629 // (0.240227044f +
3630 // (0.554906021e-1f +
3631 // (0.961591928e-2f +
3632 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3633 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003635 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003636 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003637 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003638 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3639 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003640 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003641 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3642 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003643 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003644 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3645 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003646 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3648 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003649 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003650 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3651 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003652 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003653 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003654 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003656
Scott Michelfdc40a02009-02-17 22:15:04 +00003657 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003658 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003659 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003660 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003661 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003662 result = DAG.getNode(ISD::FEXP2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003663 getValue(I.getArgOperand(0)).getValueType(),
3664 getValue(I.getArgOperand(0)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003665 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003666
Dale Johannesen601d3c02008-09-05 01:48:15 +00003667 setValue(&I, result);
3668}
3669
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003670/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3671/// limited-precision mode with x == 10.0f.
3672void
Dan Gohman46510a72010-04-15 01:51:59 +00003673SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003674 SDValue result;
Gabor Greif0635f352010-06-25 09:38:13 +00003675 const Value *Val = I.getArgOperand(0);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003676 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003677 bool IsExp10 = false;
3678
Owen Anderson825b72b2009-08-11 20:47:22 +00003679 if (getValue(Val).getValueType() == MVT::f32 &&
Gabor Greif0635f352010-06-25 09:38:13 +00003680 getValue(I.getArgOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003681 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3682 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3683 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3684 APFloat Ten(10.0f);
3685 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3686 }
3687 }
3688 }
3689
3690 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003691 SDValue Op = getValue(I.getArgOperand(1));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003692
3693 // Put the exponent in the right bit position for later addition to the
3694 // final result:
3695 //
3696 // #define LOG2OF10 3.3219281f
3697 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003698 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003699 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003700 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003701
3702 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003703 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3704 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003705
3706 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003707 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003708 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003709
3710 if (LimitFloatPrecision <= 6) {
3711 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003712 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003713 // twoToFractionalPartOfX =
3714 // 0.997535578f +
3715 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003716 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003717 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003718 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003719 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003720 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003721 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003722 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3723 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003724 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003725 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003726 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003728
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003729 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003730 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003731 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3732 // For floating-point precision of 12:
3733 //
3734 // TwoToFractionalPartOfX =
3735 // 0.999892986f +
3736 // (0.696457318f +
3737 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3738 //
3739 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003740 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003741 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003742 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003743 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003744 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3745 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003746 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003747 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3748 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003749 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003750 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003751 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003752 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003753
Scott Michelfdc40a02009-02-17 22:15:04 +00003754 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003755 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003756 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3757 // For floating-point precision of 18:
3758 //
3759 // TwoToFractionalPartOfX =
3760 // 0.999999982f +
3761 // (0.693148872f +
3762 // (0.240227044f +
3763 // (0.554906021e-1f +
3764 // (0.961591928e-2f +
3765 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3766 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003768 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003769 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003770 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003771 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3772 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003773 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003774 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3775 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003776 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003777 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3778 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003779 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003780 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3781 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003782 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003783 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3784 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003785 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003786 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003787 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003788 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003789
Scott Michelfdc40a02009-02-17 22:15:04 +00003790 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003791 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003792 }
3793 } else {
3794 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003795 result = DAG.getNode(ISD::FPOW, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003796 getValue(I.getArgOperand(0)).getValueType(),
3797 getValue(I.getArgOperand(0)),
3798 getValue(I.getArgOperand(1)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003799 }
3800
3801 setValue(&I, result);
3802}
3803
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003804
3805/// ExpandPowI - Expand a llvm.powi intrinsic.
3806static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3807 SelectionDAG &DAG) {
3808 // If RHS is a constant, we can expand this out to a multiplication tree,
3809 // otherwise we end up lowering to a call to __powidf2 (for example). When
3810 // optimizing for size, we only want to do this if the expansion would produce
3811 // a small number of multiplies, otherwise we do the full expansion.
3812 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3813 // Get the exponent as a positive value.
3814 unsigned Val = RHSC->getSExtValue();
3815 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003816
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003817 // powi(x, 0) -> 1.0
3818 if (Val == 0)
3819 return DAG.getConstantFP(1.0, LHS.getValueType());
3820
Dan Gohmanae541aa2010-04-15 04:33:49 +00003821 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003822 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3823 // If optimizing for size, don't insert too many multiplies. This
3824 // inserts up to 5 multiplies.
3825 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3826 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003827 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003828 // powi(x,15) generates one more multiply than it should), but this has
3829 // the benefit of being both really simple and much better than a libcall.
3830 SDValue Res; // Logically starts equal to 1.0
3831 SDValue CurSquare = LHS;
3832 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003833 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003834 if (Res.getNode())
3835 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3836 else
3837 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003838 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003839
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003840 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3841 CurSquare, CurSquare);
3842 Val >>= 1;
3843 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003844
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003845 // If the original was negative, invert the result, producing 1/(x*x*x).
3846 if (RHSC->getSExtValue() < 0)
3847 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3848 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3849 return Res;
3850 }
3851 }
3852
3853 // Otherwise, expand to a libcall.
3854 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3855}
3856
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003857/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
3858/// argument, create the corresponding DBG_VALUE machine instruction for it now.
3859/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003860bool
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003861SelectionDAGBuilder::EmitFuncArgumentDbgValue(const DbgValueInst &DI,
3862 const Value *V, MDNode *Variable,
Dan Gohman5d11ea32010-05-01 00:33:16 +00003863 uint64_t Offset,
3864 const SDValue &N) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003865 if (!isa<Argument>(V))
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003866 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003867
Devang Patel719f6a92010-04-29 20:40:36 +00003868 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela83ce982010-04-29 18:50:36 +00003869 // Ignore inlined function arguments here.
3870 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00003871 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00003872 return false;
3873
Dan Gohman84023e02010-07-10 09:00:22 +00003874 MachineBasicBlock *MBB = FuncInfo.MBB;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003875 if (MBB != &MF.front())
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003876 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003877
3878 unsigned Reg = 0;
3879 if (N.getOpcode() == ISD::CopyFromReg) {
3880 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Evan Cheng1deef272010-04-29 00:59:34 +00003881 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003882 MachineRegisterInfo &RegInfo = MF.getRegInfo();
3883 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
3884 if (PR)
3885 Reg = PR;
3886 }
3887 }
3888
Evan Chenga36acad2010-04-29 06:33:38 +00003889 if (!Reg) {
3890 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
3891 if (VMI == FuncInfo.ValueMap.end())
3892 return false;
3893 Reg = VMI->second;
3894 }
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003895
3896 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
3897 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
3898 TII->get(TargetOpcode::DBG_VALUE))
Evan Chenga36acad2010-04-29 06:33:38 +00003899 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003900 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003901 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003902}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003903
Douglas Gregor7d9663c2010-05-11 06:17:44 +00003904// VisualStudio defines setjmp as _setjmp
3905#if defined(_MSC_VER) && defined(setjmp)
3906#define setjmp_undefined_for_visual_studio
3907#undef setjmp
3908#endif
3909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003910/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3911/// we want to emit this as a call to a named external function, return the name
3912/// otherwise lower it and return null.
3913const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003914SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003915 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003916 SDValue Res;
3917
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003918 switch (Intrinsic) {
3919 default:
3920 // By default, turn this into a target intrinsic node.
3921 visitTargetIntrinsic(I, Intrinsic);
3922 return 0;
3923 case Intrinsic::vastart: visitVAStart(I); return 0;
3924 case Intrinsic::vaend: visitVAEnd(I); return 0;
3925 case Intrinsic::vacopy: visitVACopy(I); return 0;
3926 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003927 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00003928 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003929 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003930 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003931 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00003932 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003933 return 0;
3934 case Intrinsic::setjmp:
3935 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003936 case Intrinsic::longjmp:
3937 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003938 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003939 // Assert for address < 256 since we support only user defined address
3940 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00003941 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003942 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00003943 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003944 < 256 &&
3945 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00003946 SDValue Op1 = getValue(I.getArgOperand(0));
3947 SDValue Op2 = getValue(I.getArgOperand(1));
3948 SDValue Op3 = getValue(I.getArgOperand(2));
3949 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
3950 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003951 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Gabor Greif0635f352010-06-25 09:38:13 +00003952 I.getArgOperand(0), 0, I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003953 return 0;
3954 }
Chris Lattner824b9582008-11-21 16:42:48 +00003955 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003956 // Assert for address < 256 since we support only user defined address
3957 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00003958 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003959 < 256 &&
3960 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00003961 SDValue Op1 = getValue(I.getArgOperand(0));
3962 SDValue Op2 = getValue(I.getArgOperand(1));
3963 SDValue Op3 = getValue(I.getArgOperand(2));
3964 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
3965 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003966 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Gabor Greif0635f352010-06-25 09:38:13 +00003967 I.getArgOperand(0), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003968 return 0;
3969 }
Chris Lattner824b9582008-11-21 16:42:48 +00003970 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003971 // Assert for address < 256 since we support only user defined address
3972 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00003973 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003974 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00003975 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003976 < 256 &&
3977 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00003978 SDValue Op1 = getValue(I.getArgOperand(0));
3979 SDValue Op2 = getValue(I.getArgOperand(1));
3980 SDValue Op3 = getValue(I.getArgOperand(2));
3981 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
3982 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003983
3984 // If the source and destination are known to not be aliases, we can
3985 // lower memmove as memcpy.
3986 uint64_t Size = -1ULL;
3987 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003988 Size = C->getZExtValue();
Gabor Greif0635f352010-06-25 09:38:13 +00003989 if (AA->alias(I.getArgOperand(0), Size, I.getArgOperand(1), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003990 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003991 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher723a05a2010-07-14 23:41:32 +00003992 false, I.getArgOperand(0), 0,
3993 I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003994 return 0;
3995 }
3996
Mon P Wang20adc9d2010-04-04 03:10:48 +00003997 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Gabor Greif0635f352010-06-25 09:38:13 +00003998 I.getArgOperand(0), 0, I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003999 return 0;
4000 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004001 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004002 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004003 if (!DIVariable(DI.getVariable()).Verify())
Devang Patel7e1e31f2009-07-02 22:43:26 +00004004 return 0;
4005
Devang Patelac1ceb32009-10-09 22:42:28 +00004006 MDNode *Variable = DI.getVariable();
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004007 // Parameters are handled specially.
Devang Patelf38c6c82010-04-28 23:24:13 +00004008 bool isParameter =
4009 DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable;
Dan Gohman46510a72010-04-15 01:51:59 +00004010 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004011 if (!Address)
4012 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00004013 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00004014 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004015 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004016
4017 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4018 // but do not always have a corresponding SDNode built. The SDNodeOrder
4019 // absolute, but not relative, values are different depending on whether
4020 // debug info exists.
4021 ++SDNodeOrder;
4022 SDValue &N = NodeMap[Address];
4023 SDDbgValue *SDV;
4024 if (N.getNode()) {
4025 if (isParameter && !AI) {
4026 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4027 if (FINode)
4028 // Byval parameter. We have a frame index at this point.
4029 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4030 0, dl, SDNodeOrder);
4031 else
4032 // Can't do anything with other non-AI cases yet. This might be a
4033 // parameter of a callee function that got inlined, for example.
4034 return 0;
4035 } else if (AI)
4036 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4037 0, dl, SDNodeOrder);
4038 else
4039 // Can't do anything with other non-AI cases yet.
4040 return 0;
4041 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4042 } else {
4043 // This isn't useful, but it shows what we're missing.
4044 SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()),
4045 0, dl, SDNodeOrder);
4046 DAG.AddDbgValue(SDV, 0, isParameter);
4047 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004048 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004049 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004050 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004051 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004052 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004053 return 0;
4054
4055 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004056 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004057 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004058 if (!V)
4059 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004060
4061 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4062 // but do not always have a corresponding SDNode built. The SDNodeOrder
4063 // absolute, but not relative, values are different depending on whether
4064 // debug info exists.
4065 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004066 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00004067 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004068 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4069 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004070 } else {
Devang Pateld47f3c82010-05-05 22:29:00 +00004071 bool createUndef = false;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004072 // Do not use getValue() in here; we don't want to generate code at
4073 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004074 SDValue N = NodeMap[V];
4075 if (!N.getNode() && isa<Argument>(V))
4076 // Check unused arguments map.
4077 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004078 if (N.getNode()) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004079 if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) {
4080 SDV = DAG.getDbgValue(Variable, N.getNode(),
4081 N.getResNo(), Offset, dl, SDNodeOrder);
4082 DAG.AddDbgValue(SDV, N.getNode(), false);
4083 }
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004084 } else if (isa<PHINode>(V) && !V->use_empty() ) {
4085 // Do not call getValue(V) yet, as we don't want to generate code.
4086 // Remember it for later.
4087 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4088 DanglingDebugInfoMap[V] = DDI;
Devang Pateld47f3c82010-05-05 22:29:00 +00004089 } else
4090 createUndef = true;
4091 if (createUndef) {
Devang Patel00190342010-03-15 19:15:44 +00004092 // We may expand this to cover more cases. One case where we have no
4093 // data available is an unreferenced parameter; we need this fallback.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004094 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
4095 Offset, dl, SDNodeOrder);
4096 DAG.AddDbgValue(SDV, 0, false);
4097 }
Devang Patel00190342010-03-15 19:15:44 +00004098 }
4099
4100 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004101 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004102 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004103 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004104 // Don't handle byval struct arguments or VLAs, for example.
4105 if (!AI)
4106 return 0;
4107 DenseMap<const AllocaInst*, int>::iterator SI =
4108 FuncInfo.StaticAllocaMap.find(AI);
4109 if (SI == FuncInfo.StaticAllocaMap.end())
4110 return 0; // VLAs.
4111 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00004112
Chris Lattner512063d2010-04-05 06:19:28 +00004113 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4114 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4115 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004116 return 0;
4117 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004118 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004119 // Insert the EXCEPTIONADDR instruction.
Dan Gohman84023e02010-07-10 09:00:22 +00004120 assert(FuncInfo.MBB->isLandingPad() &&
Dan Gohman99be8ae2010-04-19 22:41:47 +00004121 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004122 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004123 SDValue Ops[1];
4124 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004125 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004126 setValue(&I, Op);
4127 DAG.setRoot(Op.getValue(1));
4128 return 0;
4129 }
4130
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004131 case Intrinsic::eh_selector: {
Dan Gohman84023e02010-07-10 09:00:22 +00004132 MachineBasicBlock *CallMBB = FuncInfo.MBB;
Chris Lattner512063d2010-04-05 06:19:28 +00004133 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00004134 if (CallMBB->isLandingPad())
4135 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004136 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004137#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004138 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004139#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004140 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4141 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman84023e02010-07-10 09:00:22 +00004142 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004143 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004144
Chris Lattner3a5815f2009-09-17 23:54:54 +00004145 // Insert the EHSELECTION instruction.
4146 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4147 SDValue Ops[2];
Gabor Greif0635f352010-06-25 09:38:13 +00004148 Ops[0] = getValue(I.getArgOperand(0));
Chris Lattner3a5815f2009-09-17 23:54:54 +00004149 Ops[1] = getRoot();
4150 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004151 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004152 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004153 return 0;
4154 }
4155
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004156 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004157 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004158 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004159 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4160 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004161 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004162 return 0;
4163 }
4164
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004165 case Intrinsic::eh_return_i32:
4166 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004167 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
4168 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
4169 MVT::Other,
4170 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004171 getValue(I.getArgOperand(0)),
4172 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004173 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004174 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004175 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004176 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004177 case Intrinsic::eh_dwarf_cfa: {
Gabor Greif0635f352010-06-25 09:38:13 +00004178 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004179 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004180 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004181 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004182 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004183 TLI.getPointerTy()),
4184 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004185 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004186 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004187 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00004188 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4189 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004190 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004191 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004192 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004193 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004194 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004195 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004196 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004197
Chris Lattner512063d2010-04-05 06:19:28 +00004198 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004199 return 0;
4200 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004201 case Intrinsic::eh_sjlj_setjmp: {
4202 setValue(&I, DAG.getNode(ISD::EH_SJLJ_SETJMP, dl, MVT::i32, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004203 getValue(I.getArgOperand(0))));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004204 return 0;
4205 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004206 case Intrinsic::eh_sjlj_longjmp: {
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004207 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, dl, MVT::Other,
4208 getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004209 getValue(I.getArgOperand(0))));
Jim Grosbach5eb19512010-05-22 01:06:18 +00004210 return 0;
4211 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004212
Mon P Wang77cdf302008-11-10 20:54:11 +00004213 case Intrinsic::convertff:
4214 case Intrinsic::convertfsi:
4215 case Intrinsic::convertfui:
4216 case Intrinsic::convertsif:
4217 case Intrinsic::convertuif:
4218 case Intrinsic::convertss:
4219 case Intrinsic::convertsu:
4220 case Intrinsic::convertus:
4221 case Intrinsic::convertuu: {
4222 ISD::CvtCode Code = ISD::CVT_INVALID;
4223 switch (Intrinsic) {
4224 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4225 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4226 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4227 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4228 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4229 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4230 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4231 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4232 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4233 }
Owen Andersone50ed302009-08-10 22:56:29 +00004234 EVT DestVT = TLI.getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004235 const Value *Op1 = I.getArgOperand(0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004236 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4237 DAG.getValueType(DestVT),
4238 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004239 getValue(I.getArgOperand(1)),
4240 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004241 Code);
4242 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004243 return 0;
4244 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004245 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004246 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004247 getValue(I.getArgOperand(0)).getValueType(),
4248 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004249 return 0;
4250 case Intrinsic::powi:
Gabor Greif0635f352010-06-25 09:38:13 +00004251 setValue(&I, ExpandPowI(dl, getValue(I.getArgOperand(0)),
4252 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004253 return 0;
4254 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004255 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004256 getValue(I.getArgOperand(0)).getValueType(),
4257 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004258 return 0;
4259 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004260 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004261 getValue(I.getArgOperand(0)).getValueType(),
4262 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004263 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004264 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004265 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004266 return 0;
4267 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004268 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004269 return 0;
4270 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004271 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004272 return 0;
4273 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004274 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004275 return 0;
4276 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004277 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004278 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004279 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004280 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004281 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004282 case Intrinsic::convert_to_fp16:
4283 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004284 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004285 return 0;
4286 case Intrinsic::convert_from_fp16:
4287 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004288 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004289 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004290 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004291 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004292 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004293 return 0;
4294 }
4295 case Intrinsic::readcyclecounter: {
4296 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004297 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4298 DAG.getVTList(MVT::i64, MVT::Other),
4299 &Op, 1);
4300 setValue(&I, Res);
4301 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004302 return 0;
4303 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004304 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004305 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004306 getValue(I.getArgOperand(0)).getValueType(),
4307 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004308 return 0;
4309 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004310 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004311 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004312 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004313 return 0;
4314 }
4315 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004316 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004317 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004318 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004319 return 0;
4320 }
4321 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00004322 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004323 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004324 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004325 return 0;
4326 }
4327 case Intrinsic::stacksave: {
4328 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004329 Res = DAG.getNode(ISD::STACKSAVE, dl,
4330 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4331 setValue(&I, Res);
4332 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004333 return 0;
4334 }
4335 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00004336 Res = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004337 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004338 return 0;
4339 }
Bill Wendling57344502008-11-18 11:01:33 +00004340 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004341 // Emit code into the DAG to store the stack guard onto the stack.
4342 MachineFunction &MF = DAG.getMachineFunction();
4343 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004344 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004345
Gabor Greif0635f352010-06-25 09:38:13 +00004346 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
4347 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004348
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004349 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004350 MFI->setStackProtectorIndex(FI);
4351
4352 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4353
4354 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004355 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4356 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004357 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004358 setValue(&I, Res);
4359 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004360 return 0;
4361 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004362 case Intrinsic::objectsize: {
4363 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00004364 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004365
4366 assert(CI && "Non-constant type in __builtin_object_size?");
4367
Gabor Greif0635f352010-06-25 09:38:13 +00004368 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004369 EVT Ty = Arg.getValueType();
4370
Dan Gohmane368b462010-06-18 14:22:04 +00004371 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004372 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004373 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004374 Res = DAG.getConstant(0, Ty);
4375
4376 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004377 return 0;
4378 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004379 case Intrinsic::var_annotation:
4380 // Discard annotate attributes
4381 return 0;
4382
4383 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00004384 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004385
4386 SDValue Ops[6];
4387 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004388 Ops[1] = getValue(I.getArgOperand(0));
4389 Ops[2] = getValue(I.getArgOperand(1));
4390 Ops[3] = getValue(I.getArgOperand(2));
4391 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392 Ops[5] = DAG.getSrcValue(F);
4393
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004394 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4395 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4396 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004398 setValue(&I, Res);
4399 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004400 return 0;
4401 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004402 case Intrinsic::gcroot:
4403 if (GFI) {
Gabor Greif0635f352010-06-25 09:38:13 +00004404 const Value *Alloca = I.getArgOperand(0);
4405 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004406
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004407 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4408 GFI->addStackRoot(FI->getIndex(), TypeMap);
4409 }
4410 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004411 case Intrinsic::gcread:
4412 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004413 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004414 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004415 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004416 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004417 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004418 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004419 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004420 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004421 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004422 return implVisitAluOverflow(I, ISD::UADDO);
4423 case Intrinsic::sadd_with_overflow:
4424 return implVisitAluOverflow(I, ISD::SADDO);
4425 case Intrinsic::usub_with_overflow:
4426 return implVisitAluOverflow(I, ISD::USUBO);
4427 case Intrinsic::ssub_with_overflow:
4428 return implVisitAluOverflow(I, ISD::SSUBO);
4429 case Intrinsic::umul_with_overflow:
4430 return implVisitAluOverflow(I, ISD::UMULO);
4431 case Intrinsic::smul_with_overflow:
4432 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004433
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004434 case Intrinsic::prefetch: {
4435 SDValue Ops[4];
4436 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004437 Ops[1] = getValue(I.getArgOperand(0));
4438 Ops[2] = getValue(I.getArgOperand(1));
4439 Ops[3] = getValue(I.getArgOperand(2));
Bill Wendling4533cac2010-01-28 21:51:40 +00004440 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004441 return 0;
4442 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004443
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004444 case Intrinsic::memory_barrier: {
4445 SDValue Ops[6];
4446 Ops[0] = getRoot();
4447 for (int x = 1; x < 6; ++x)
Gabor Greif0635f352010-06-25 09:38:13 +00004448 Ops[x] = getValue(I.getArgOperand(x - 1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004449
Bill Wendling4533cac2010-01-28 21:51:40 +00004450 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004451 return 0;
4452 }
4453 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004454 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004455 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004456 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00004457 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004458 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00004459 getValue(I.getArgOperand(0)),
4460 getValue(I.getArgOperand(1)),
4461 getValue(I.getArgOperand(2)),
4462 I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004463 setValue(&I, L);
4464 DAG.setRoot(L.getValue(1));
4465 return 0;
4466 }
4467 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004468 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004469 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004470 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004471 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004472 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004473 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004474 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004475 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004476 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004477 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004478 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004479 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004480 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004481 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004482 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004483 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004484 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004485 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004486 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004487 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004488 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004489
4490 case Intrinsic::invariant_start:
4491 case Intrinsic::lifetime_start:
4492 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004493 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004494 return 0;
4495 case Intrinsic::invariant_end:
4496 case Intrinsic::lifetime_end:
4497 // Discard region information.
4498 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004499 }
4500}
4501
Dan Gohman46510a72010-04-15 01:51:59 +00004502void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004503 bool isTailCall,
4504 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004505 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4506 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004507 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004508 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004509 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004510
4511 TargetLowering::ArgListTy Args;
4512 TargetLowering::ArgListEntry Entry;
4513 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004514
4515 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00004516 SmallVector<ISD::OutputArg, 4> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004517 SmallVector<uint64_t, 4> Offsets;
Dan Gohman84023e02010-07-10 09:00:22 +00004518 GetReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4519 Outs, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004520
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004521 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Dan Gohman84023e02010-07-10 09:00:22 +00004522 FTy->isVarArg(), Outs, FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004523
4524 SDValue DemoteStackSlot;
4525
4526 if (!CanLowerReturn) {
4527 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4528 FTy->getReturnType());
4529 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4530 FTy->getReturnType());
4531 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004532 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004533 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4534
4535 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4536 Entry.Node = DemoteStackSlot;
4537 Entry.Ty = StackSlotPtrType;
4538 Entry.isSExt = false;
4539 Entry.isZExt = false;
4540 Entry.isInReg = false;
4541 Entry.isSRet = true;
4542 Entry.isNest = false;
4543 Entry.isByVal = false;
4544 Entry.Alignment = Align;
4545 Args.push_back(Entry);
4546 RetTy = Type::getVoidTy(FTy->getContext());
4547 }
4548
Dan Gohman46510a72010-04-15 01:51:59 +00004549 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004550 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004551 SDValue ArgNode = getValue(*i);
4552 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4553
4554 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004555 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4556 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4557 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4558 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4559 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4560 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004561 Entry.Alignment = CS.getParamAlignment(attrInd);
4562 Args.push_back(Entry);
4563 }
4564
Chris Lattner512063d2010-04-05 06:19:28 +00004565 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004566 // Insert a label before the invoke call to mark the try range. This can be
4567 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004568 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004569
Jim Grosbachca752c92010-01-28 01:45:32 +00004570 // For SjLj, keep track of which landing pads go with which invokes
4571 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004572 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004573 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004574 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004575 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004576 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004577 }
4578
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004579 // Both PendingLoads and PendingExports must be flushed here;
4580 // this call might not return.
4581 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004582 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004583 }
4584
Dan Gohman98ca4f22009-08-05 01:29:28 +00004585 // Check if target-independent constraints permit a tail call here.
4586 // Target-dependent constraints are checked within TLI.LowerCallTo.
4587 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004588 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004589 isTailCall = false;
4590
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004591 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004592 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004593 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004594 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004595 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004596 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004597 isTailCall,
4598 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004599 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004600 assert((isTailCall || Result.second.getNode()) &&
4601 "Non-null chain expected with non-tail call!");
4602 assert((Result.second.getNode() || !Result.first.getNode()) &&
4603 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004604 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004605 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004606 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004607 // The instruction result is the result of loading from the
4608 // hidden sret parameter.
4609 SmallVector<EVT, 1> PVTs;
4610 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4611
4612 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4613 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4614 EVT PtrVT = PVTs[0];
Dan Gohman84023e02010-07-10 09:00:22 +00004615 unsigned NumValues = Outs.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004616 SmallVector<SDValue, 4> Values(NumValues);
4617 SmallVector<SDValue, 4> Chains(NumValues);
4618
4619 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004620 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4621 DemoteStackSlot,
4622 DAG.getConstant(Offsets[i], PtrVT));
Dan Gohman84023e02010-07-10 09:00:22 +00004623 SDValue L = DAG.getLoad(Outs[i].VT, getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004624 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004625 Values[i] = L;
4626 Chains[i] = L.getValue(1);
4627 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004628
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004629 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4630 MVT::Other, &Chains[0], NumValues);
4631 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004632
4633 // Collect the legal value parts into potentially illegal values
4634 // that correspond to the original function's return values.
4635 SmallVector<EVT, 4> RetTys;
4636 RetTy = FTy->getReturnType();
4637 ComputeValueVTs(TLI, RetTy, RetTys);
4638 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4639 SmallVector<SDValue, 4> ReturnValues;
4640 unsigned CurReg = 0;
4641 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4642 EVT VT = RetTys[I];
4643 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4644 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4645
4646 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004647 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004648 RegisterVT, VT, AssertOp);
4649 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004650 CurReg += NumRegs;
4651 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004652
Bill Wendling4533cac2010-01-28 21:51:40 +00004653 setValue(CS.getInstruction(),
4654 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4655 DAG.getVTList(&RetTys[0], RetTys.size()),
4656 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004657
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004658 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004659
4660 // As a special case, a null chain means that a tail call has been emitted and
4661 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004662 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004663 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004664 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004665 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004666
Chris Lattner512063d2010-04-05 06:19:28 +00004667 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668 // Insert a label at the end of the invoke call to mark the try range. This
4669 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004670 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004671 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004672
4673 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004674 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004675 }
4676}
4677
Chris Lattner8047d9a2009-12-24 00:37:38 +00004678/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4679/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004680static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4681 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004682 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004683 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004684 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004685 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004686 if (C->isNullValue())
4687 continue;
4688 // Unknown instruction.
4689 return false;
4690 }
4691 return true;
4692}
4693
Dan Gohman46510a72010-04-15 01:51:59 +00004694static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4695 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004696 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004697
Chris Lattner8047d9a2009-12-24 00:37:38 +00004698 // Check to see if this load can be trivially constant folded, e.g. if the
4699 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004700 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004701 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004702 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004703 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004704
Dan Gohman46510a72010-04-15 01:51:59 +00004705 if (const Constant *LoadCst =
4706 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4707 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004708 return Builder.getValue(LoadCst);
4709 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004710
Chris Lattner8047d9a2009-12-24 00:37:38 +00004711 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4712 // still constant memory, the input chain can be the entry node.
4713 SDValue Root;
4714 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004715
Chris Lattner8047d9a2009-12-24 00:37:38 +00004716 // Do not serialize (non-volatile) loads of constant memory with anything.
4717 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4718 Root = Builder.DAG.getEntryNode();
4719 ConstantMemory = true;
4720 } else {
4721 // Do not serialize non-volatile loads against each other.
4722 Root = Builder.DAG.getRoot();
4723 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004724
Chris Lattner8047d9a2009-12-24 00:37:38 +00004725 SDValue Ptr = Builder.getValue(PtrVal);
4726 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4727 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004728 false /*volatile*/,
4729 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004730
Chris Lattner8047d9a2009-12-24 00:37:38 +00004731 if (!ConstantMemory)
4732 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4733 return LoadVal;
4734}
4735
4736
4737/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4738/// If so, return true and lower it, otherwise return false and it will be
4739/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004740bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004741 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00004742 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00004743 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004744
Gabor Greif0635f352010-06-25 09:38:13 +00004745 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00004746 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00004747 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004748 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004749 return false;
4750
Gabor Greif0635f352010-06-25 09:38:13 +00004751 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004752
Chris Lattner8047d9a2009-12-24 00:37:38 +00004753 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4754 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004755 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4756 bool ActuallyDoIt = true;
4757 MVT LoadVT;
4758 const Type *LoadTy;
4759 switch (Size->getZExtValue()) {
4760 default:
4761 LoadVT = MVT::Other;
4762 LoadTy = 0;
4763 ActuallyDoIt = false;
4764 break;
4765 case 2:
4766 LoadVT = MVT::i16;
4767 LoadTy = Type::getInt16Ty(Size->getContext());
4768 break;
4769 case 4:
4770 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004771 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004772 break;
4773 case 8:
4774 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004775 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004776 break;
4777 /*
4778 case 16:
4779 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004780 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004781 LoadTy = VectorType::get(LoadTy, 4);
4782 break;
4783 */
4784 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004785
Chris Lattner04b091a2009-12-24 01:07:17 +00004786 // This turns into unaligned loads. We only do this if the target natively
4787 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4788 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004789
Chris Lattner04b091a2009-12-24 01:07:17 +00004790 // Require that we can find a legal MVT, and only do this if the target
4791 // supports unaligned loads of that type. Expanding into byte loads would
4792 // bloat the code.
4793 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4794 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4795 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4796 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4797 ActuallyDoIt = false;
4798 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004799
Chris Lattner04b091a2009-12-24 01:07:17 +00004800 if (ActuallyDoIt) {
4801 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4802 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004803
Chris Lattner04b091a2009-12-24 01:07:17 +00004804 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4805 ISD::SETNE);
4806 EVT CallVT = TLI.getValueType(I.getType(), true);
4807 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4808 return true;
4809 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004810 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004811
4812
Chris Lattner8047d9a2009-12-24 00:37:38 +00004813 return false;
4814}
4815
4816
Dan Gohman46510a72010-04-15 01:51:59 +00004817void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00004818 // Handle inline assembly differently.
4819 if (isa<InlineAsm>(I.getCalledValue())) {
4820 visitInlineAsm(&I);
4821 return;
4822 }
4823
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004824 const char *RenameFn = 0;
4825 if (Function *F = I.getCalledFunction()) {
4826 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00004827 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004828 if (unsigned IID = II->getIntrinsicID(F)) {
4829 RenameFn = visitIntrinsicCall(I, IID);
4830 if (!RenameFn)
4831 return;
4832 }
4833 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004834 if (unsigned IID = F->getIntrinsicID()) {
4835 RenameFn = visitIntrinsicCall(I, IID);
4836 if (!RenameFn)
4837 return;
4838 }
4839 }
4840
4841 // Check for well-known libc/libm calls. If the function is internal, it
4842 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004843 if (!F->hasLocalLinkage() && F->hasName()) {
4844 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004845 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004846 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004847 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4848 I.getType() == I.getArgOperand(0)->getType() &&
4849 I.getType() == I.getArgOperand(1)->getType()) {
4850 SDValue LHS = getValue(I.getArgOperand(0));
4851 SDValue RHS = getValue(I.getArgOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004852 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4853 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854 return;
4855 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004856 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004857 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004858 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4859 I.getType() == I.getArgOperand(0)->getType()) {
4860 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004861 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4862 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004863 return;
4864 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004865 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004866 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004867 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4868 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004869 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004870 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004871 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4872 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004873 return;
4874 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004875 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004876 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004877 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4878 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004879 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004880 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004881 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4882 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004883 return;
4884 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004885 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004886 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004887 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4888 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004889 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004890 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004891 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4892 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004893 return;
4894 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004895 } else if (Name == "memcmp") {
4896 if (visitMemCmpCall(I))
4897 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004898 }
4899 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004900 }
Chris Lattner598751e2010-07-05 05:36:21 +00004901
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004902 SDValue Callee;
4903 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00004904 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004905 else
Bill Wendling056292f2008-09-16 21:48:12 +00004906 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004907
Bill Wendling0d580132009-12-23 01:28:19 +00004908 // Check if we can potentially perform a tail call. More detailed checking is
4909 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004910 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004911}
4912
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004913namespace llvm {
Dan Gohman462f6b52010-05-29 17:53:24 +00004914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004915/// AsmOperandInfo - This contains information for each constraint that we are
4916/// lowering.
Duncan Sands16d8f8b2010-05-11 20:16:09 +00004917class LLVM_LIBRARY_VISIBILITY SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004918 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004919public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004920 /// CallOperand - If this is the result output operand or a clobber
4921 /// this is null, otherwise it is the incoming operand to the CallInst.
4922 /// This gets modified as the asm is processed.
4923 SDValue CallOperand;
4924
4925 /// AssignedRegs - If this is a register or register class operand, this
4926 /// contains the set of register corresponding to the operand.
4927 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004928
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004929 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4930 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4931 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004932
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004933 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4934 /// busy in OutputRegs/InputRegs.
4935 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004936 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004937 std::set<unsigned> &InputRegs,
4938 const TargetRegisterInfo &TRI) const {
4939 if (isOutReg) {
4940 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4941 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4942 }
4943 if (isInReg) {
4944 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4945 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4946 }
4947 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004948
Owen Andersone50ed302009-08-10 22:56:29 +00004949 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004950 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004951 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004952 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004953 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004954 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004955 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004956
Chris Lattner81249c92008-10-17 17:05:25 +00004957 if (isa<BasicBlock>(CallOperandVal))
4958 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004959
Chris Lattner81249c92008-10-17 17:05:25 +00004960 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004961
Chris Lattner81249c92008-10-17 17:05:25 +00004962 // If this is an indirect operand, the operand is a pointer to the
4963 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004964 if (isIndirect) {
4965 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4966 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004967 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004968 OpTy = PtrTy->getElementType();
4969 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004970
Chris Lattner81249c92008-10-17 17:05:25 +00004971 // If OpTy is not a single value, it may be a struct/union that we
4972 // can tile with integers.
4973 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4974 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4975 switch (BitSize) {
4976 default: break;
4977 case 1:
4978 case 8:
4979 case 16:
4980 case 32:
4981 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004982 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004983 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004984 break;
4985 }
4986 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004987
Chris Lattner81249c92008-10-17 17:05:25 +00004988 return TLI.getValueType(OpTy, true);
4989 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004990
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004991private:
4992 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4993 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004994 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004995 const TargetRegisterInfo &TRI) {
4996 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4997 Regs.insert(Reg);
4998 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4999 for (; *Aliases; ++Aliases)
5000 Regs.insert(*Aliases);
5001 }
5002};
Dan Gohman462f6b52010-05-29 17:53:24 +00005003
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005004} // end llvm namespace.
5005
Dan Gohman462f6b52010-05-29 17:53:24 +00005006/// isAllocatableRegister - If the specified register is safe to allocate,
5007/// i.e. it isn't a stack pointer or some other special register, return the
5008/// register class for the register. Otherwise, return null.
5009static const TargetRegisterClass *
5010isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5011 const TargetLowering &TLI,
5012 const TargetRegisterInfo *TRI) {
5013 EVT FoundVT = MVT::Other;
5014 const TargetRegisterClass *FoundRC = 0;
5015 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5016 E = TRI->regclass_end(); RCI != E; ++RCI) {
5017 EVT ThisVT = MVT::Other;
5018
5019 const TargetRegisterClass *RC = *RCI;
5020 // If none of the value types for this register class are valid, we
5021 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5022 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5023 I != E; ++I) {
5024 if (TLI.isTypeLegal(*I)) {
5025 // If we have already found this register in a different register class,
5026 // choose the one with the largest VT specified. For example, on
5027 // PowerPC, we favor f64 register classes over f32.
5028 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
5029 ThisVT = *I;
5030 break;
5031 }
5032 }
5033 }
5034
5035 if (ThisVT == MVT::Other) continue;
5036
5037 // NOTE: This isn't ideal. In particular, this might allocate the
5038 // frame pointer in functions that need it (due to them not being taken
5039 // out of allocation, because a variable sized allocation hasn't been seen
5040 // yet). This is a slight code pessimization, but should still work.
5041 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5042 E = RC->allocation_order_end(MF); I != E; ++I)
5043 if (*I == Reg) {
5044 // We found a matching register class. Keep looking at others in case
5045 // we find one with larger registers that this physreg is also in.
5046 FoundRC = RC;
5047 FoundVT = ThisVT;
5048 break;
5049 }
5050 }
5051 return FoundRC;
5052}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005053
5054/// GetRegistersForValue - Assign registers (virtual or physical) for the
5055/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005056/// register allocator to handle the assignment process. However, if the asm
5057/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005058/// allocation. This produces generally horrible, but correct, code.
5059///
5060/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005061/// Input and OutputRegs are the set of already allocated physical registers.
5062///
Dan Gohman2048b852009-11-23 18:04:58 +00005063void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005064GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005065 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005066 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005067 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005069 // Compute whether this value requires an input register, an output register,
5070 // or both.
5071 bool isOutReg = false;
5072 bool isInReg = false;
5073 switch (OpInfo.Type) {
5074 case InlineAsm::isOutput:
5075 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005076
5077 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005078 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005079 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005080 break;
5081 case InlineAsm::isInput:
5082 isInReg = true;
5083 isOutReg = false;
5084 break;
5085 case InlineAsm::isClobber:
5086 isOutReg = true;
5087 isInReg = true;
5088 break;
5089 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005090
5091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005092 MachineFunction &MF = DAG.getMachineFunction();
5093 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005095 // If this is a constraint for a single physreg, or a constraint for a
5096 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005097 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005098 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5099 OpInfo.ConstraintVT);
5100
5101 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005102 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005103 // If this is a FP input in an integer register (or visa versa) insert a bit
5104 // cast of the input value. More generally, handle any case where the input
5105 // value disagrees with the register class we plan to stick this in.
5106 if (OpInfo.Type == InlineAsm::isInput &&
5107 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005108 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005109 // types are identical size, use a bitcast to convert (e.g. two differing
5110 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005111 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005112 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005113 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005114 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005115 OpInfo.ConstraintVT = RegVT;
5116 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5117 // If the input is a FP value and we want it in FP registers, do a
5118 // bitcast to the corresponding integer type. This turns an f64 value
5119 // into i64, which can be passed with two i32 values on a 32-bit
5120 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005121 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005122 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005123 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005124 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005125 OpInfo.ConstraintVT = RegVT;
5126 }
5127 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005128
Owen Anderson23b9b192009-08-12 00:36:31 +00005129 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005130 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005131
Owen Andersone50ed302009-08-10 22:56:29 +00005132 EVT RegVT;
5133 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005134
5135 // If this is a constraint for a specific physical register, like {r17},
5136 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005137 if (unsigned AssignedReg = PhysReg.first) {
5138 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005139 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005140 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005141
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005142 // Get the actual register value type. This is important, because the user
5143 // may have asked for (e.g.) the AX register in i32 type. We need to
5144 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005145 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005147 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005148 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005149
5150 // If this is an expanded reference, add the rest of the regs to Regs.
5151 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005152 TargetRegisterClass::iterator I = RC->begin();
5153 for (; *I != AssignedReg; ++I)
5154 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156 // Already added the first reg.
5157 --NumRegs; ++I;
5158 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005159 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005160 Regs.push_back(*I);
5161 }
5162 }
Bill Wendling651ad132009-12-22 01:25:10 +00005163
Dan Gohman7451d3e2010-05-29 17:03:36 +00005164 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005165 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5166 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5167 return;
5168 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005170 // Otherwise, if this was a reference to an LLVM register class, create vregs
5171 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005172 if (const TargetRegisterClass *RC = PhysReg.second) {
5173 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005174 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005175 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005176
Evan Chengfb112882009-03-23 08:01:15 +00005177 // Create the appropriate number of virtual registers.
5178 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5179 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005180 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005181
Dan Gohman7451d3e2010-05-29 17:03:36 +00005182 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005183 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005184 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005185
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005186 // This is a reference to a register class that doesn't directly correspond
5187 // to an LLVM register class. Allocate NumRegs consecutive, available,
5188 // registers from the class.
5189 std::vector<unsigned> RegClassRegs
5190 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5191 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005192
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005193 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5194 unsigned NumAllocated = 0;
5195 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5196 unsigned Reg = RegClassRegs[i];
5197 // See if this register is available.
5198 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5199 (isInReg && InputRegs.count(Reg))) { // Already used.
5200 // Make sure we find consecutive registers.
5201 NumAllocated = 0;
5202 continue;
5203 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005205 // Check to see if this register is allocatable (i.e. don't give out the
5206 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005207 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5208 if (!RC) { // Couldn't allocate this register.
5209 // Reset NumAllocated to make sure we return consecutive registers.
5210 NumAllocated = 0;
5211 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005212 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005213
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005214 // Okay, this register is good, we can use it.
5215 ++NumAllocated;
5216
5217 // If we allocated enough consecutive registers, succeed.
5218 if (NumAllocated == NumRegs) {
5219 unsigned RegStart = (i-NumAllocated)+1;
5220 unsigned RegEnd = i+1;
5221 // Mark all of the allocated registers used.
5222 for (unsigned i = RegStart; i != RegEnd; ++i)
5223 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005224
Dan Gohman7451d3e2010-05-29 17:03:36 +00005225 OpInfo.AssignedRegs = RegsForValue(Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005226 OpInfo.ConstraintVT);
5227 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5228 return;
5229 }
5230 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005231
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005232 // Otherwise, we couldn't allocate enough registers for this.
5233}
5234
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005235/// visitInlineAsm - Handle a call to an InlineAsm object.
5236///
Dan Gohman46510a72010-04-15 01:51:59 +00005237void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5238 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005239
5240 /// ConstraintOperands - Information about all of the constraints.
5241 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005242
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005243 std::set<unsigned> OutputRegs, InputRegs;
5244
5245 // Do a prepass over the constraints, canonicalizing them, and building up the
5246 // ConstraintOperands list.
5247 std::vector<InlineAsm::ConstraintInfo>
5248 ConstraintInfos = IA->ParseConstraints();
5249
Evan Chengda43bcf2008-09-24 00:05:32 +00005250 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005251
Chris Lattner6c147292009-04-30 00:48:50 +00005252 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005253
Chris Lattner6c147292009-04-30 00:48:50 +00005254 // We won't need to flush pending loads if this asm doesn't touch
5255 // memory and is nonvolatile.
5256 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005257 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005258 else
5259 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5262 unsigned ResNo = 0; // ResNo - The result number of the next output.
5263 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5264 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5265 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005266
Owen Anderson825b72b2009-08-11 20:47:22 +00005267 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005268
5269 // Compute the value type for each operand.
5270 switch (OpInfo.Type) {
5271 case InlineAsm::isOutput:
5272 // Indirect outputs just consume an argument.
5273 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005274 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005275 break;
5276 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005278 // The return value of the call is this value. As such, there is no
5279 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005280 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005281 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005282 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5283 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5284 } else {
5285 assert(ResNo == 0 && "Asm only has one result!");
5286 OpVT = TLI.getValueType(CS.getType());
5287 }
5288 ++ResNo;
5289 break;
5290 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005291 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005292 break;
5293 case InlineAsm::isClobber:
5294 // Nothing to do.
5295 break;
5296 }
5297
5298 // If this is an input or an indirect output, process the call argument.
5299 // BasicBlocks are labels, currently appearing only in asm's.
5300 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005301 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005302 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5303
Dan Gohman46510a72010-04-15 01:51:59 +00005304 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005306 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005307 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005308 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005309
Owen Anderson1d0be152009-08-13 21:58:54 +00005310 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005311 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005312
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005314 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005315
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005316 // Second pass over the constraints: compute which constraint option to use
5317 // and assign registers to constraints that want a specific physreg.
5318 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5319 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005320
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005321 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005322 // matching input. If their types mismatch, e.g. one is an integer, the
5323 // other is floating point, or their sizes are different, flag it as an
5324 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005325 if (OpInfo.hasMatchingInput()) {
5326 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005327
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005328 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005329 if ((OpInfo.ConstraintVT.isInteger() !=
5330 Input.ConstraintVT.isInteger()) ||
5331 (OpInfo.ConstraintVT.getSizeInBits() !=
5332 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005333 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005334 " with a matching output constraint of"
5335 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005336 }
5337 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005338 }
5339 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005340
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005341 // Compute the constraint code and ConstraintType to use.
Dale Johannesen1784d162010-06-25 21:55:36 +00005342 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005344 // If this is a memory input, and if the operand is not indirect, do what we
5345 // need to to provide an address for the memory input.
5346 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5347 !OpInfo.isIndirect) {
5348 assert(OpInfo.Type == InlineAsm::isInput &&
5349 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005350
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005351 // Memory operands really want the address of the value. If we don't have
5352 // an indirect input, put it in the constpool if we can, otherwise spill
5353 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005354
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005355 // If the operand is a float, integer, or vector constant, spill to a
5356 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005357 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005358 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5359 isa<ConstantVector>(OpVal)) {
5360 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5361 TLI.getPointerTy());
5362 } else {
5363 // Otherwise, create a stack slot and emit a store to it before the
5364 // asm.
5365 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005366 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005367 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5368 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005369 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005370 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005371 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005372 OpInfo.CallOperand, StackSlot, NULL, 0,
5373 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005374 OpInfo.CallOperand = StackSlot;
5375 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005376
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005377 // There is no longer a Value* corresponding to this operand.
5378 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005379
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005380 // It is now an indirect operand.
5381 OpInfo.isIndirect = true;
5382 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005383
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005384 // If this constraint is for a specific register, allocate it before
5385 // anything else.
5386 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005387 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005388 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005389
Bill Wendling651ad132009-12-22 01:25:10 +00005390 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005391
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005392 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005393 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005394 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5395 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005396
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005397 // C_Register operands have already been allocated, Other/Memory don't need
5398 // to be.
5399 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005400 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005401 }
5402
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005403 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5404 std::vector<SDValue> AsmNodeOperands;
5405 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5406 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005407 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5408 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005409
Chris Lattnerdecc2672010-04-07 05:20:54 +00005410 // If we have a !srcloc metadata node associated with it, we want to attach
5411 // this to the ultimately generated inline asm machineinstr. To do this, we
5412 // pass in the third operand as this (potentially null) inline asm MDNode.
5413 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5414 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005415
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005416 // Remember the AlignStack bit as operand 3.
5417 AsmNodeOperands.push_back(DAG.getTargetConstant(IA->isAlignStack() ? 1 : 0,
5418 MVT::i1));
5419
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005420 // Loop over all of the inputs, copying the operand values into the
5421 // appropriate registers and processing the output regs.
5422 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005423
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005424 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5425 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005426
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005427 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5428 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5429
5430 switch (OpInfo.Type) {
5431 case InlineAsm::isOutput: {
5432 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5433 OpInfo.ConstraintType != TargetLowering::C_Register) {
5434 // Memory output, or 'other' output (e.g. 'X' constraint).
5435 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5436
5437 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005438 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5439 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005440 TLI.getPointerTy()));
5441 AsmNodeOperands.push_back(OpInfo.CallOperand);
5442 break;
5443 }
5444
5445 // Otherwise, this is a register or register class output.
5446
5447 // Copy the output from the appropriate register. Find a register that
5448 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005449 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005450 report_fatal_error("Couldn't allocate output reg for constraint '" +
5451 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005452
5453 // If this is an indirect operand, store through the pointer after the
5454 // asm.
5455 if (OpInfo.isIndirect) {
5456 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5457 OpInfo.CallOperandVal));
5458 } else {
5459 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005460 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005461 // Concatenate this output onto the outputs list.
5462 RetValRegs.append(OpInfo.AssignedRegs);
5463 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005464
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005465 // Add information to the INLINEASM node to know that this register is
5466 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005467 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005468 InlineAsm::Kind_RegDefEarlyClobber :
5469 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005470 false,
5471 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005472 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005473 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 break;
5475 }
5476 case InlineAsm::isInput: {
5477 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005478
Chris Lattner6bdcda32008-10-17 16:47:46 +00005479 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005480 // If this is required to match an output register we have already set,
5481 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005482 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005483
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005484 // Scan until we find the definition we already emitted of this operand.
5485 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005486 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005487 for (; OperandNo; --OperandNo) {
5488 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005489 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005490 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005491 assert((InlineAsm::isRegDefKind(OpFlag) ||
5492 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5493 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005494 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005495 }
5496
Evan Cheng697cbbf2009-03-20 18:03:34 +00005497 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005498 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005499 if (InlineAsm::isRegDefKind(OpFlag) ||
5500 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005501 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005502 if (OpInfo.isIndirect) {
5503 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005504 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005505 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5506 " don't know how to handle tied "
5507 "indirect register inputs");
5508 }
5509
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005510 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005511 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005512 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005513 MatchedRegs.RegVTs.push_back(RegVT);
5514 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005515 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005516 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005517 MatchedRegs.Regs.push_back
5518 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005519
5520 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005521 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005522 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005523 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005524 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005525 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005526 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005527 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005528
5529 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5530 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5531 "Unexpected number of operands");
5532 // Add information to the INLINEASM node to know about this input.
5533 // See InlineAsm.h isUseOperandTiedToDef.
5534 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5535 OpInfo.getMatchedOperand());
5536 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5537 TLI.getPointerTy()));
5538 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5539 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005540 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005541
Dale Johannesenb5611a62010-07-13 20:17:05 +00005542 // Treat indirect 'X' constraint as memory.
5543 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
5544 OpInfo.isIndirect)
5545 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005546
Dale Johannesenb5611a62010-07-13 20:17:05 +00005547 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005548 std::vector<SDValue> Ops;
5549 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Dale Johannesen1784d162010-06-25 21:55:36 +00005550 Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005551 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005552 report_fatal_error("Invalid operand for inline asm constraint '" +
5553 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005554
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005555 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005556 unsigned ResOpType =
5557 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005558 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005559 TLI.getPointerTy()));
5560 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5561 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005562 }
5563
5564 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005565 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5566 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5567 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005569 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005570 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005571 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005572 TLI.getPointerTy()));
5573 AsmNodeOperands.push_back(InOperandVal);
5574 break;
5575 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005576
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005577 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5578 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5579 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005580 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005581 "Don't know how to handle indirect register inputs yet!");
5582
5583 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005584 if (OpInfo.AssignedRegs.Regs.empty() ||
Dan Gohman7451d3e2010-05-29 17:03:36 +00005585 !OpInfo.AssignedRegs.areValueTypesLegal(TLI))
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005586 report_fatal_error("Couldn't allocate input reg for constraint '" +
5587 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005588
Dale Johannesen66978ee2009-01-31 02:22:37 +00005589 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005590 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005591
Chris Lattnerdecc2672010-04-07 05:20:54 +00005592 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005593 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005594 break;
5595 }
5596 case InlineAsm::isClobber: {
5597 // Add the clobbered value to the operand list, so that the register
5598 // allocator is aware that the physreg got clobbered.
5599 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005600 OpInfo.AssignedRegs.AddInlineAsmOperands(
5601 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005602 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005603 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005604 break;
5605 }
5606 }
5607 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005608
Chris Lattnerdecc2672010-04-07 05:20:54 +00005609 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005610 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005611 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005612
Dale Johannesen66978ee2009-01-31 02:22:37 +00005613 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005614 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005615 &AsmNodeOperands[0], AsmNodeOperands.size());
5616 Flag = Chain.getValue(1);
5617
5618 // If this asm returns a register value, copy the result from that register
5619 // and set it as the value of the call.
5620 if (!RetValRegs.Regs.empty()) {
Dan Gohman7451d3e2010-05-29 17:03:36 +00005621 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005622 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005623
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005624 // FIXME: Why don't we do this for inline asms with MRVs?
5625 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005626 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005627
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005628 // If any of the results of the inline asm is a vector, it may have the
5629 // wrong width/num elts. This can happen for register classes that can
5630 // contain multiple different value types. The preg or vreg allocated may
5631 // not have the same VT as was expected. Convert it to the right type
5632 // with bit_convert.
5633 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005634 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005635 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005636
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005637 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005638 ResultType.isInteger() && Val.getValueType().isInteger()) {
5639 // If a result value was tied to an input value, the computed result may
5640 // have a wider width than the expected result. Extract the relevant
5641 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005642 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005643 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005644
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005645 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005646 }
Dan Gohman95915732008-10-18 01:03:45 +00005647
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005648 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005649 // Don't need to use this as a chain in this case.
5650 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5651 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005653
Dan Gohman46510a72010-04-15 01:51:59 +00005654 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005655
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005656 // Process indirect outputs, first output all of the flagged copies out of
5657 // physregs.
5658 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5659 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005660 const Value *Ptr = IndirectStoresToEmit[i].second;
Dan Gohman7451d3e2010-05-29 17:03:36 +00005661 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005662 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005663 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5664 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005665
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005666 // Emit the non-flagged stores from the physregs.
5667 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005668 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5669 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5670 StoresToEmit[i].first,
5671 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005672 StoresToEmit[i].second, 0,
5673 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005674 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005675 }
5676
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005679 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005681 DAG.setRoot(Chain);
5682}
5683
Dan Gohman46510a72010-04-15 01:51:59 +00005684void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005685 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5686 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005687 getValue(I.getArgOperand(0)),
5688 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005689}
5690
Dan Gohman46510a72010-04-15 01:51:59 +00005691void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Rafael Espindola9d544d02010-07-12 18:11:17 +00005692 const TargetData &TD = *TLI.getTargetData();
Dale Johannesena04b7572009-02-03 23:04:43 +00005693 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5694 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00005695 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00005696 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005697 setValue(&I, V);
5698 DAG.setRoot(V.getValue(1));
5699}
5700
Dan Gohman46510a72010-04-15 01:51:59 +00005701void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005702 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5703 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005704 getValue(I.getArgOperand(0)),
5705 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005706}
5707
Dan Gohman46510a72010-04-15 01:51:59 +00005708void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005709 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5710 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005711 getValue(I.getArgOperand(0)),
5712 getValue(I.getArgOperand(1)),
5713 DAG.getSrcValue(I.getArgOperand(0)),
5714 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005715}
5716
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005717/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005718/// implementation, which just calls LowerCall.
5719/// FIXME: When all targets are
5720/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005721std::pair<SDValue, SDValue>
5722TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5723 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005724 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005725 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005726 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005727 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005728 ArgListTy &Args, SelectionDAG &DAG,
5729 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005730 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005731 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00005732 SmallVector<SDValue, 32> OutVals;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005733 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005734 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005735 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5736 for (unsigned Value = 0, NumValues = ValueVTs.size();
5737 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005738 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005739 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005740 SDValue Op = SDValue(Args[i].Node.getNode(),
5741 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005742 ISD::ArgFlagsTy Flags;
5743 unsigned OriginalAlignment =
5744 getTargetData()->getABITypeAlignment(ArgTy);
5745
5746 if (Args[i].isZExt)
5747 Flags.setZExt();
5748 if (Args[i].isSExt)
5749 Flags.setSExt();
5750 if (Args[i].isInReg)
5751 Flags.setInReg();
5752 if (Args[i].isSRet)
5753 Flags.setSRet();
5754 if (Args[i].isByVal) {
5755 Flags.setByVal();
5756 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5757 const Type *ElementTy = Ty->getElementType();
5758 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005759 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005760 // For ByVal, alignment should come from FE. BE will guess if this
5761 // info is not there but there are cases it cannot get right.
5762 if (Args[i].Alignment)
5763 FrameAlign = Args[i].Alignment;
5764 Flags.setByValAlign(FrameAlign);
5765 Flags.setByValSize(FrameSize);
5766 }
5767 if (Args[i].isNest)
5768 Flags.setNest();
5769 Flags.setOrigAlign(OriginalAlignment);
5770
Owen Anderson23b9b192009-08-12 00:36:31 +00005771 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5772 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005773 SmallVector<SDValue, 4> Parts(NumParts);
5774 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5775
5776 if (Args[i].isSExt)
5777 ExtendKind = ISD::SIGN_EXTEND;
5778 else if (Args[i].isZExt)
5779 ExtendKind = ISD::ZERO_EXTEND;
5780
Bill Wendling46ada192010-03-02 01:55:18 +00005781 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005782 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005783
Dan Gohman98ca4f22009-08-05 01:29:28 +00005784 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005785 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00005786 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
5787 i < NumFixedArgs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005788 if (NumParts > 1 && j == 0)
5789 MyFlags.Flags.setSplit();
5790 else if (j != 0)
5791 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005792
Dan Gohman98ca4f22009-08-05 01:29:28 +00005793 Outs.push_back(MyFlags);
Dan Gohmanc9403652010-07-07 15:54:55 +00005794 OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005795 }
5796 }
5797 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005798
Dan Gohman98ca4f22009-08-05 01:29:28 +00005799 // Handle the incoming return values from the call.
5800 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005801 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005803 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005804 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005805 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5806 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005807 for (unsigned i = 0; i != NumRegs; ++i) {
5808 ISD::InputArg MyFlags;
5809 MyFlags.VT = RegisterVT;
5810 MyFlags.Used = isReturnValueUsed;
5811 if (RetSExt)
5812 MyFlags.Flags.setSExt();
5813 if (RetZExt)
5814 MyFlags.Flags.setZExt();
5815 if (isInreg)
5816 MyFlags.Flags.setInReg();
5817 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005818 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005819 }
5820
Dan Gohman98ca4f22009-08-05 01:29:28 +00005821 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005822 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +00005823 Outs, OutVals, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005824
5825 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005826 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005827 "LowerCall didn't return a valid chain!");
5828 assert((!isTailCall || InVals.empty()) &&
5829 "LowerCall emitted a return value for a tail call!");
5830 assert((isTailCall || InVals.size() == Ins.size()) &&
5831 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005832
5833 // For a tail call, the return value is merely live-out and there aren't
5834 // any nodes in the DAG representing it. Return a special value to
5835 // indicate that a tail call has been emitted and no more Instructions
5836 // should be processed in the current block.
5837 if (isTailCall) {
5838 DAG.setRoot(Chain);
5839 return std::make_pair(SDValue(), SDValue());
5840 }
5841
Evan Chengaf1871f2010-03-11 19:38:18 +00005842 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5843 assert(InVals[i].getNode() &&
5844 "LowerCall emitted a null value!");
5845 assert(Ins[i].VT == InVals[i].getValueType() &&
5846 "LowerCall emitted a value with the wrong type!");
5847 });
5848
Dan Gohman98ca4f22009-08-05 01:29:28 +00005849 // Collect the legal value parts into potentially illegal values
5850 // that correspond to the original function's return values.
5851 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5852 if (RetSExt)
5853 AssertOp = ISD::AssertSext;
5854 else if (RetZExt)
5855 AssertOp = ISD::AssertZext;
5856 SmallVector<SDValue, 4> ReturnValues;
5857 unsigned CurReg = 0;
5858 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005859 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005860 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5861 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005862
Bill Wendling46ada192010-03-02 01:55:18 +00005863 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005864 NumRegs, RegisterVT, VT,
5865 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005866 CurReg += NumRegs;
5867 }
5868
5869 // For a function returning void, there is no return value. We can't create
5870 // such a node, so we just return a null return value in that case. In
5871 // that case, nothing will actualy look at the value.
5872 if (ReturnValues.empty())
5873 return std::make_pair(SDValue(), Chain);
5874
5875 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5876 DAG.getVTList(&RetTys[0], RetTys.size()),
5877 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005878 return std::make_pair(Res, Chain);
5879}
5880
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005881void TargetLowering::LowerOperationWrapper(SDNode *N,
5882 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005883 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005884 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005885 if (Res.getNode())
5886 Results.push_back(Res);
5887}
5888
Dan Gohmand858e902010-04-17 15:26:15 +00005889SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005890 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005891 return SDValue();
5892}
5893
Dan Gohman46510a72010-04-15 01:51:59 +00005894void
5895SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00005896 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005897 assert((Op.getOpcode() != ISD::CopyFromReg ||
5898 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5899 "Copy from a reg to the same reg!");
5900 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5901
Owen Anderson23b9b192009-08-12 00:36:31 +00005902 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005903 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005904 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005905 PendingExports.push_back(Chain);
5906}
5907
5908#include "llvm/CodeGen/SelectionDAGISel.h"
5909
Dan Gohman46510a72010-04-15 01:51:59 +00005910void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005911 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005912 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005913 SelectionDAG &DAG = SDB->DAG;
Dan Gohman2048b852009-11-23 18:04:58 +00005914 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005915 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005916 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005917
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005918 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005919 SmallVector<ISD::OutputArg, 4> Outs;
5920 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
5921 Outs, TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005922
Dan Gohman7451d3e2010-05-29 17:03:36 +00005923 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005924 // Put in an sret pointer parameter before all the other parameters.
5925 SmallVector<EVT, 1> ValueVTs;
5926 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5927
5928 // NOTE: Assuming that a pointer will never break down to more than one VT
5929 // or one register.
5930 ISD::ArgFlagsTy Flags;
5931 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00005932 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005933 ISD::InputArg RetArg(Flags, RegisterVT, true);
5934 Ins.push_back(RetArg);
5935 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005936
Dan Gohman98ca4f22009-08-05 01:29:28 +00005937 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005938 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005939 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005940 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005941 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005942 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5943 bool isArgValueUsed = !I->use_empty();
5944 for (unsigned Value = 0, NumValues = ValueVTs.size();
5945 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005946 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005947 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005948 ISD::ArgFlagsTy Flags;
5949 unsigned OriginalAlignment =
5950 TD->getABITypeAlignment(ArgTy);
5951
5952 if (F.paramHasAttr(Idx, Attribute::ZExt))
5953 Flags.setZExt();
5954 if (F.paramHasAttr(Idx, Attribute::SExt))
5955 Flags.setSExt();
5956 if (F.paramHasAttr(Idx, Attribute::InReg))
5957 Flags.setInReg();
5958 if (F.paramHasAttr(Idx, Attribute::StructRet))
5959 Flags.setSRet();
5960 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5961 Flags.setByVal();
5962 const PointerType *Ty = cast<PointerType>(I->getType());
5963 const Type *ElementTy = Ty->getElementType();
5964 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5965 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5966 // For ByVal, alignment should be passed from FE. BE will guess if
5967 // this info is not there but there are cases it cannot get right.
5968 if (F.getParamAlignment(Idx))
5969 FrameAlign = F.getParamAlignment(Idx);
5970 Flags.setByValAlign(FrameAlign);
5971 Flags.setByValSize(FrameSize);
5972 }
5973 if (F.paramHasAttr(Idx, Attribute::Nest))
5974 Flags.setNest();
5975 Flags.setOrigAlign(OriginalAlignment);
5976
Owen Anderson23b9b192009-08-12 00:36:31 +00005977 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5978 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005979 for (unsigned i = 0; i != NumRegs; ++i) {
5980 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5981 if (NumRegs > 1 && i == 0)
5982 MyFlags.Flags.setSplit();
5983 // if it isn't first piece, alignment must be 1
5984 else if (i > 0)
5985 MyFlags.Flags.setOrigAlign(1);
5986 Ins.push_back(MyFlags);
5987 }
5988 }
5989 }
5990
5991 // Call the target to set up the argument values.
5992 SmallVector<SDValue, 8> InVals;
5993 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5994 F.isVarArg(), Ins,
5995 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005996
5997 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005998 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005999 "LowerFormalArguments didn't return a valid chain!");
6000 assert(InVals.size() == Ins.size() &&
6001 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006002 DEBUG({
6003 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6004 assert(InVals[i].getNode() &&
6005 "LowerFormalArguments emitted a null value!");
6006 assert(Ins[i].VT == InVals[i].getValueType() &&
6007 "LowerFormalArguments emitted a value with the wrong type!");
6008 }
6009 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006010
Dan Gohman5e866062009-08-06 15:37:27 +00006011 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006012 DAG.setRoot(NewRoot);
6013
6014 // Set up the argument values.
6015 unsigned i = 0;
6016 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006017 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006018 // Create a virtual register for the sret pointer, and put in a copy
6019 // from the sret argument into it.
6020 SmallVector<EVT, 1> ValueVTs;
6021 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6022 EVT VT = ValueVTs[0];
6023 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6024 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006025 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006026 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006027
Dan Gohman2048b852009-11-23 18:04:58 +00006028 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006029 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6030 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006031 FuncInfo->DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006032 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6033 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006034 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006035
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006036 // i indexes lowered arguments. Bump it past the hidden sret argument.
6037 // Idx indexes LLVM arguments. Don't touch it.
6038 ++i;
6039 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006040
Dan Gohman46510a72010-04-15 01:51:59 +00006041 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006042 ++I, ++Idx) {
6043 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006044 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006045 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006046 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006047
6048 // If this argument is unused then remember its value. It is used to generate
6049 // debugging information.
6050 if (I->use_empty() && NumValues)
6051 SDB->setUnusedArgValue(I, InVals[i]);
6052
Dan Gohman98ca4f22009-08-05 01:29:28 +00006053 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006054 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006055 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6056 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006057
6058 if (!I->use_empty()) {
6059 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6060 if (F.paramHasAttr(Idx, Attribute::SExt))
6061 AssertOp = ISD::AssertSext;
6062 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6063 AssertOp = ISD::AssertZext;
6064
Bill Wendling46ada192010-03-02 01:55:18 +00006065 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006066 NumParts, PartVT, VT,
6067 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006068 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006069
Dan Gohman98ca4f22009-08-05 01:29:28 +00006070 i += NumParts;
6071 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006072
Dan Gohman98ca4f22009-08-05 01:29:28 +00006073 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00006074 SDValue Res;
6075 if (!ArgValues.empty())
6076 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6077 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006078 SDB->setValue(I, Res);
6079
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006080 // If this argument is live outside of the entry block, insert a copy from
6081 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006082 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006083 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006084 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006085
Dan Gohman98ca4f22009-08-05 01:29:28 +00006086 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006087
6088 // Finally, if the target has anything special to do, allow it to do so.
6089 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006090 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006091}
6092
6093/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6094/// ensure constants are generated when needed. Remember the virtual registers
6095/// that need to be added to the Machine PHI nodes as input. We cannot just
6096/// directly add them, because expansion might result in multiple MBB's for one
6097/// BB. As such, the start of the BB might correspond to a different MBB than
6098/// the end.
6099///
6100void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006101SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006102 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006103
6104 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6105
6106 // Check successor nodes' PHI nodes that expect a constant to be available
6107 // from this block.
6108 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006109 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006110 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006111 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006112
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006113 // If this terminator has multiple identical successors (common for
6114 // switches), only handle each succ once.
6115 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006116
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006117 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006118
6119 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6120 // nodes and Machine PHI nodes, but the incoming operands have not been
6121 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006122 for (BasicBlock::const_iterator I = SuccBB->begin();
6123 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006124 // Ignore dead phi's.
6125 if (PN->use_empty()) continue;
6126
6127 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006128 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006129
Dan Gohman46510a72010-04-15 01:51:59 +00006130 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006131 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006132 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00006133 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006134 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006135 }
6136 Reg = RegOut;
6137 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00006138 DenseMap<const Value *, unsigned>::iterator I =
6139 FuncInfo.ValueMap.find(PHIOp);
6140 if (I != FuncInfo.ValueMap.end())
6141 Reg = I->second;
6142 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006143 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006144 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00006146 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006147 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006148 }
6149 }
6150
6151 // Remember that this register needs to added to the machine PHI node as
6152 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006153 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006154 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6155 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006156 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006157 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006158 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006159 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006160 Reg += NumRegisters;
6161 }
6162 }
6163 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006164 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006165}