blob: af0a28da8fe04a9609cf5d2eea3138cba98ac8e1 [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 Gohman6277eb22009-11-23 17:16:22 +000017#include "FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000018#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000019#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000021#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000022#include "llvm/Constants.h"
23#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Function.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/InlineAsm.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/IntrinsicInst.h"
Chris Lattner6129c372010-04-08 00:09:16 +000031#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000032#include "llvm/Module.h"
Dan Gohman5eb6d652010-04-21 01:22:34 +000033#include "llvm/CodeGen/Analysis.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000034#include "llvm/CodeGen/FastISel.h"
35#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 {
533 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
534
535 // Assemble the legal parts into the final values.
536 SmallVector<SDValue, 4> Values(ValueVTs.size());
537 SmallVector<SDValue, 8> Parts;
538 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
539 // Copy the legal parts from the registers.
540 EVT ValueVT = ValueVTs[Value];
541 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
542 EVT RegisterVT = RegVTs[Value];
543
544 Parts.resize(NumRegs);
545 for (unsigned i = 0; i != NumRegs; ++i) {
546 SDValue P;
547 if (Flag == 0) {
548 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
549 } else {
550 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
551 *Flag = P.getValue(2);
552 }
553
554 Chain = P.getValue(1);
555
556 // If the source register was virtual and if we know something about it,
557 // add an assert node.
558 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
559 RegisterVT.isInteger() && !RegisterVT.isVector()) {
560 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
561 if (FuncInfo.LiveOutRegInfo.size() > SlotNo) {
562 const FunctionLoweringInfo::LiveOutInfo &LOI =
563 FuncInfo.LiveOutRegInfo[SlotNo];
564
565 unsigned RegSize = RegisterVT.getSizeInBits();
566 unsigned NumSignBits = LOI.NumSignBits;
567 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
568
569 // FIXME: We capture more information than the dag can represent. For
570 // now, just use the tightest assertzext/assertsext possible.
571 bool isSExt = true;
572 EVT FromVT(MVT::Other);
573 if (NumSignBits == RegSize)
574 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
575 else if (NumZeroBits >= RegSize-1)
576 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
577 else if (NumSignBits > RegSize-8)
578 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
579 else if (NumZeroBits >= RegSize-8)
580 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
581 else if (NumSignBits > RegSize-16)
582 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
583 else if (NumZeroBits >= RegSize-16)
584 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
585 else if (NumSignBits > RegSize-32)
586 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
587 else if (NumZeroBits >= RegSize-32)
588 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
589
590 if (FromVT != MVT::Other)
591 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
592 RegisterVT, P, DAG.getValueType(FromVT));
593 }
594 }
595
596 Parts[i] = P;
597 }
598
599 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
600 NumRegs, RegisterVT, ValueVT);
601 Part += NumRegs;
602 Parts.clear();
603 }
604
605 return DAG.getNode(ISD::MERGE_VALUES, dl,
606 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
607 &Values[0], ValueVTs.size());
608}
609
610/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
611/// specified value into the registers specified by this object. This uses
612/// Chain/Flag as the input and updates them for the output Chain/Flag.
613/// If the Flag pointer is NULL, no flag is used.
614void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
615 SDValue &Chain, SDValue *Flag) const {
616 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
617
618 // Get the list of the values's legal parts.
619 unsigned NumRegs = Regs.size();
620 SmallVector<SDValue, 8> Parts(NumRegs);
621 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
622 EVT ValueVT = ValueVTs[Value];
623 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
624 EVT RegisterVT = RegVTs[Value];
625
626 getCopyToParts(DAG, dl,
627 Val.getValue(Val.getResNo() + Value),
628 &Parts[Part], NumParts, RegisterVT);
629 Part += NumParts;
630 }
631
632 // Copy the parts into the registers.
633 SmallVector<SDValue, 8> Chains(NumRegs);
634 for (unsigned i = 0; i != NumRegs; ++i) {
635 SDValue Part;
636 if (Flag == 0) {
637 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
638 } else {
639 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
640 *Flag = Part.getValue(1);
641 }
642
643 Chains[i] = Part.getValue(0);
644 }
645
646 if (NumRegs == 1 || Flag)
647 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
648 // flagged to it. That is the CopyToReg nodes and the user are considered
649 // a single scheduling unit. If we create a TokenFactor and return it as
650 // chain, then the TokenFactor is both a predecessor (operand) of the
651 // user as well as a successor (the TF operands are flagged to the user).
652 // c1, f1 = CopyToReg
653 // c2, f2 = CopyToReg
654 // c3 = TokenFactor c1, c2
655 // ...
656 // = op c3, ..., f2
657 Chain = Chains[NumRegs-1];
658 else
659 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
660}
661
662/// AddInlineAsmOperands - Add this value to the specified inlineasm node
663/// operand list. This adds the code marker and includes the number of
664/// values added into it.
665void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
666 unsigned MatchingIdx,
667 SelectionDAG &DAG,
668 std::vector<SDValue> &Ops) const {
669 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
670
671 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
672 if (HasMatching)
673 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
674 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
675 Ops.push_back(Res);
676
677 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
678 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
679 EVT RegisterVT = RegVTs[Value];
680 for (unsigned i = 0; i != NumRegs; ++i) {
681 assert(Reg < Regs.size() && "Mismatch in # registers expected");
682 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
683 }
684 }
685}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000686
Dan Gohman2048b852009-11-23 18:04:58 +0000687void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000688 AA = &aa;
689 GFI = gfi;
690 TD = DAG.getTarget().getTargetData();
691}
692
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000693/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000694/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000695/// for a new block. This doesn't clear out information about
696/// additional blocks that are needed to complete switch lowering
697/// or PHI node updating; that information is cleared out as it is
698/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000699void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000700 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000701 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000702 PendingLoads.clear();
703 PendingExports.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000704 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000705 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000706}
707
708/// getRoot - Return the current virtual root of the Selection DAG,
709/// flushing any PendingLoad items. This must be done before emitting
710/// a store or any other node that may need to be ordered after any
711/// prior load instructions.
712///
Dan Gohman2048b852009-11-23 18:04:58 +0000713SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000714 if (PendingLoads.empty())
715 return DAG.getRoot();
716
717 if (PendingLoads.size() == 1) {
718 SDValue Root = PendingLoads[0];
719 DAG.setRoot(Root);
720 PendingLoads.clear();
721 return Root;
722 }
723
724 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000726 &PendingLoads[0], PendingLoads.size());
727 PendingLoads.clear();
728 DAG.setRoot(Root);
729 return Root;
730}
731
732/// getControlRoot - Similar to getRoot, but instead of flushing all the
733/// PendingLoad items, flush all the PendingExports items. It is necessary
734/// to do this before emitting a terminator instruction.
735///
Dan Gohman2048b852009-11-23 18:04:58 +0000736SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000737 SDValue Root = DAG.getRoot();
738
739 if (PendingExports.empty())
740 return Root;
741
742 // Turn all of the CopyToReg chains into one factored node.
743 if (Root.getOpcode() != ISD::EntryToken) {
744 unsigned i = 0, e = PendingExports.size();
745 for (; i != e; ++i) {
746 assert(PendingExports[i].getNode()->getNumOperands() > 1);
747 if (PendingExports[i].getNode()->getOperand(0) == Root)
748 break; // Don't add the root if we already indirectly depend on it.
749 }
750
751 if (i == e)
752 PendingExports.push_back(Root);
753 }
754
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000756 &PendingExports[0],
757 PendingExports.size());
758 PendingExports.clear();
759 DAG.setRoot(Root);
760 return Root;
761}
762
Bill Wendling4533cac2010-01-28 21:51:40 +0000763void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
764 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
765 DAG.AssignOrdering(Node, SDNodeOrder);
766
767 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
768 AssignOrderingToNode(Node->getOperand(I).getNode());
769}
770
Dan Gohman46510a72010-04-15 01:51:59 +0000771void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000772 // Set up outgoing PHI node register values before emitting the terminator.
773 if (isa<TerminatorInst>(&I))
774 HandlePHINodesInSuccessorBlocks(I.getParent());
775
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000776 CurDebugLoc = I.getDebugLoc();
777
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000778 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000779
Dan Gohman92884f72010-04-20 15:03:56 +0000780 if (!isa<TerminatorInst>(&I) && !HasTailCall)
781 CopyToExportRegsIfNeeded(&I);
782
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000783 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000784}
785
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000786void SelectionDAGBuilder::visitPHI(const PHINode &) {
787 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
788}
789
Dan Gohman46510a72010-04-15 01:51:59 +0000790void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000791 // Note: this doesn't use InstVisitor, because it has to work with
792 // ConstantExpr's in addition to instructions.
793 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000794 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000795 // Build the switch statement using the Instruction.def file.
796#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000797 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000798#include "llvm/Instruction.def"
799 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000800
801 // Assign the ordering to the freshly created DAG nodes.
802 if (NodeMap.count(&I)) {
803 ++SDNodeOrder;
804 AssignOrderingToNode(getValue(&I).getNode());
805 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000806}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000807
Dan Gohman28a17352010-07-01 01:59:43 +0000808// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +0000809SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +0000810 // If we already have an SDValue for this value, use it. It's important
811 // to do this first, so that we don't create a CopyFromReg if we already
812 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000813 SDValue &N = NodeMap[V];
814 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000815
Dan Gohman28a17352010-07-01 01:59:43 +0000816 // If there's a virtual register allocated and initialized for this
817 // value, use it.
818 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
819 if (It != FuncInfo.ValueMap.end()) {
820 unsigned InReg = It->second;
821 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
822 SDValue Chain = DAG.getEntryNode();
823 return N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL);
824 }
825
826 // Otherwise create a new SDValue and remember it.
827 SDValue Val = getValueImpl(V);
828 NodeMap[V] = Val;
829 return Val;
830}
831
832/// getNonRegisterValue - Return an SDValue for the given Value, but
833/// don't look in FuncInfo.ValueMap for a virtual register.
834SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
835 // If we already have an SDValue for this value, use it.
836 SDValue &N = NodeMap[V];
837 if (N.getNode()) return N;
838
839 // Otherwise create a new SDValue and remember it.
840 SDValue Val = getValueImpl(V);
841 NodeMap[V] = Val;
842 return Val;
843}
844
845/// getValueImpl - Helper function for getValue and getMaterializedValue.
846/// Create an SDValue for the given value.
847SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Dan Gohman383b5f62010-04-17 15:32:28 +0000848 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000849 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000850
Dan Gohman383b5f62010-04-17 15:32:28 +0000851 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000852 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000853
Dan Gohman383b5f62010-04-17 15:32:28 +0000854 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000855 return DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000856
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000857 if (isa<ConstantPointerNull>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000858 return DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000859
Dan Gohman383b5f62010-04-17 15:32:28 +0000860 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000861 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000862
Nate Begeman9008ca62009-04-27 18:41:29 +0000863 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +0000864 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000865
Dan Gohman383b5f62010-04-17 15:32:28 +0000866 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000867 visit(CE->getOpcode(), *CE);
868 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000869 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000870 return N1;
871 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000873 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
874 SmallVector<SDValue, 4> Constants;
875 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
876 OI != OE; ++OI) {
877 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000878 // If the operand is an empty aggregate, there are no values.
879 if (!Val) continue;
880 // Add each leaf value from the operand to the Constants list
881 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000882 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
883 Constants.push_back(SDValue(Val, i));
884 }
Bill Wendling87710f02009-12-21 23:47:40 +0000885
Bill Wendling4533cac2010-01-28 21:51:40 +0000886 return DAG.getMergeValues(&Constants[0], Constants.size(),
887 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000888 }
889
Duncan Sands1df98592010-02-16 11:11:14 +0000890 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000891 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
892 "Unknown struct or array constant!");
893
Owen Andersone50ed302009-08-10 22:56:29 +0000894 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000895 ComputeValueVTs(TLI, C->getType(), ValueVTs);
896 unsigned NumElts = ValueVTs.size();
897 if (NumElts == 0)
898 return SDValue(); // empty struct
899 SmallVector<SDValue, 4> Constants(NumElts);
900 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000901 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000902 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000903 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000904 else if (EltVT.isFloatingPoint())
905 Constants[i] = DAG.getConstantFP(0, EltVT);
906 else
907 Constants[i] = DAG.getConstant(0, EltVT);
908 }
Bill Wendling87710f02009-12-21 23:47:40 +0000909
Bill Wendling4533cac2010-01-28 21:51:40 +0000910 return DAG.getMergeValues(&Constants[0], NumElts,
911 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000912 }
913
Dan Gohman383b5f62010-04-17 15:32:28 +0000914 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000915 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000916
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000917 const VectorType *VecTy = cast<VectorType>(V->getType());
918 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000919
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000920 // Now that we know the number and type of the elements, get that number of
921 // elements into the Ops array based on what kind of constant it is.
922 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000923 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000924 for (unsigned i = 0; i != NumElements; ++i)
925 Ops.push_back(getValue(CP->getOperand(i)));
926 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000927 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000928 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000929
930 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000931 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000932 Op = DAG.getConstantFP(0, EltVT);
933 else
934 Op = DAG.getConstant(0, EltVT);
935 Ops.assign(NumElements, Op);
936 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000937
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000938 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000939 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
940 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000941 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000943 // If this is a static alloca, generate it as the frameindex instead of
944 // computation.
945 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
946 DenseMap<const AllocaInst*, int>::iterator SI =
947 FuncInfo.StaticAllocaMap.find(AI);
948 if (SI != FuncInfo.StaticAllocaMap.end())
949 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
950 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000951
Dan Gohman28a17352010-07-01 01:59:43 +0000952 // If this is an instruction which fast-isel has deferred, select it now.
953 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
954 assert(Inst->isSafeToSpeculativelyExecute() &&
955 "Instruction with side effects deferred!");
956 visit(*Inst);
957 DenseMap<const Value *, SDValue>::iterator NIt = NodeMap.find(Inst);
958 if (NIt != NodeMap.end() && NIt->second.getNode())
959 return NIt->second;
960 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000961
Dan Gohman28a17352010-07-01 01:59:43 +0000962 llvm_unreachable("Can't get register for value!");
963 return SDValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000964}
965
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000966/// Get the EVTs and ArgFlags collections that represent the legalized return
967/// type of the given function. This does not require a DAG or a return value,
968/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000969static void getReturnInfo(const Type* ReturnType,
970 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000971 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000972 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000973 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000974 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000975 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000976 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000977 if (NumValues == 0) return;
978 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000979
980 for (unsigned j = 0, f = NumValues; j != f; ++j) {
981 EVT VT = ValueVTs[j];
982 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000983
984 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000985 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000986 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000987 ExtendKind = ISD::ZERO_EXTEND;
988
989 // FIXME: C calling convention requires the return type to be promoted to
990 // at least 32-bit. But this is not necessary for non-C calling
991 // conventions. The frontend should mark functions whose return values
992 // require promoting with signext or zeroext attributes.
993 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000994 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000995 if (VT.bitsLT(MinVT))
996 VT = MinVT;
997 }
998
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000999 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
1000 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00001001 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
1002 PartVT.getTypeForEVT(ReturnType->getContext()));
1003
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001004 // 'inreg' on function refers to return value
1005 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001006 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001007 Flags.setInReg();
1008
1009 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001010 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001011 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001012 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001013 Flags.setZExt();
1014
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001015 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001016 OutVTs.push_back(PartVT);
1017 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00001018 if (Offsets)
1019 {
1020 Offsets->push_back(Offset);
1021 Offset += PartSize;
1022 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001023 }
1024 }
1025}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001026
Dan Gohman46510a72010-04-15 01:51:59 +00001027void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001028 SDValue Chain = getControlRoot();
1029 SmallVector<ISD::OutputArg, 8> Outs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001030
Dan Gohman7451d3e2010-05-29 17:03:36 +00001031 if (!FuncInfo.CanLowerReturn) {
1032 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001033 const Function *F = I.getParent()->getParent();
1034
1035 // Emit a store of the return value through the virtual register.
1036 // Leave Outs empty so that LowerReturn won't try to load return
1037 // registers the usual way.
1038 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001039 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001040 PtrValueVTs);
1041
1042 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1043 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001044
Owen Andersone50ed302009-08-10 22:56:29 +00001045 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001046 SmallVector<uint64_t, 4> Offsets;
1047 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001048 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001049
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001050 SmallVector<SDValue, 4> Chains(NumValues);
1051 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +00001052 for (unsigned i = 0; i != NumValues; ++i) {
1053 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
1054 DAG.getConstant(Offsets[i], PtrVT));
1055 Chains[i] =
1056 DAG.getStore(Chain, getCurDebugLoc(),
1057 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00001058 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001059 }
1060
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001061 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
1062 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001063 } else if (I.getNumOperands() != 0) {
1064 SmallVector<EVT, 4> ValueVTs;
1065 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
1066 unsigned NumValues = ValueVTs.size();
1067 if (NumValues) {
1068 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001069 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1070 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001071
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001072 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001073
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001074 const Function *F = I.getParent()->getParent();
1075 if (F->paramHasAttr(0, Attribute::SExt))
1076 ExtendKind = ISD::SIGN_EXTEND;
1077 else if (F->paramHasAttr(0, Attribute::ZExt))
1078 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001079
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001080 // FIXME: C calling convention requires the return type to be promoted
1081 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001082 // conventions. The frontend should mark functions whose return values
1083 // require promoting with signext or zeroext attributes.
1084 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
1085 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
1086 if (VT.bitsLT(MinVT))
1087 VT = MinVT;
1088 }
1089
1090 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
1091 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
1092 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +00001093 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001094 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
1095 &Parts[0], NumParts, PartVT, ExtendKind);
1096
1097 // 'inreg' on function refers to return value
1098 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1099 if (F->paramHasAttr(0, Attribute::InReg))
1100 Flags.setInReg();
1101
1102 // Propagate extension type if any
1103 if (F->paramHasAttr(0, Attribute::SExt))
1104 Flags.setSExt();
1105 else if (F->paramHasAttr(0, Attribute::ZExt))
1106 Flags.setZExt();
1107
1108 for (unsigned i = 0; i < NumParts; ++i)
1109 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +00001110 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001111 }
1112 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001113
1114 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001115 CallingConv::ID CallConv =
1116 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001117 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
1118 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001119
1120 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001121 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001122 "LowerReturn didn't return a valid chain!");
1123
1124 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001125 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001126}
1127
Dan Gohmanad62f532009-04-23 23:13:24 +00001128/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1129/// created for it, emit nodes to copy the value into the virtual
1130/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001131void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +00001132 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1133 if (VMI != FuncInfo.ValueMap.end()) {
1134 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1135 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001136 }
1137}
1138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001139/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1140/// the current basic block, add it to ValueMap now so that we'll get a
1141/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001142void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001143 // No need to export constants.
1144 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001146 // Already exported?
1147 if (FuncInfo.isExportedInst(V)) return;
1148
1149 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1150 CopyValueToVirtualRegister(V, Reg);
1151}
1152
Dan Gohman46510a72010-04-15 01:51:59 +00001153bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001154 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155 // The operands of the setcc have to be in this block. We don't know
1156 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001157 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 // Can export from current BB.
1159 if (VI->getParent() == FromBB)
1160 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001161
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001162 // Is already exported, noop.
1163 return FuncInfo.isExportedInst(V);
1164 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001166 // If this is an argument, we can export it if the BB is the entry block or
1167 // if it is already exported.
1168 if (isa<Argument>(V)) {
1169 if (FromBB == &FromBB->getParent()->getEntryBlock())
1170 return true;
1171
1172 // Otherwise, can only export this if it is already exported.
1173 return FuncInfo.isExportedInst(V);
1174 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001175
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001176 // Otherwise, constants can always be exported.
1177 return true;
1178}
1179
1180static bool InBlock(const Value *V, const BasicBlock *BB) {
1181 if (const Instruction *I = dyn_cast<Instruction>(V))
1182 return I->getParent() == BB;
1183 return true;
1184}
1185
Dan Gohmanc2277342008-10-17 21:16:08 +00001186/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1187/// This function emits a branch and is used at the leaves of an OR or an
1188/// AND operator tree.
1189///
1190void
Dan Gohman46510a72010-04-15 01:51:59 +00001191SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001192 MachineBasicBlock *TBB,
1193 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001194 MachineBasicBlock *CurBB,
1195 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001196 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001197
Dan Gohmanc2277342008-10-17 21:16:08 +00001198 // If the leaf of the tree is a comparison, merge the condition into
1199 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001200 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001201 // The operands of the cmp have to be in this block. We don't know
1202 // how to export them from some other block. If this is the first block
1203 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001204 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001205 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1206 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001208 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001209 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001210 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001211 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212 } else {
1213 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001214 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001215 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001216
1217 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001218 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1219 SwitchCases.push_back(CB);
1220 return;
1221 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001222 }
1223
1224 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001225 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001226 NULL, TBB, FBB, CurBB);
1227 SwitchCases.push_back(CB);
1228}
1229
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001230/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001231void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001232 MachineBasicBlock *TBB,
1233 MachineBasicBlock *FBB,
1234 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001235 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001236 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001237 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001238 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001239 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001240 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1241 BOp->getParent() != CurBB->getBasicBlock() ||
1242 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1243 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001244 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001245 return;
1246 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001247
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001248 // Create TmpBB after CurBB.
1249 MachineFunction::iterator BBI = CurBB;
1250 MachineFunction &MF = DAG.getMachineFunction();
1251 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1252 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001253
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001254 if (Opc == Instruction::Or) {
1255 // Codegen X | Y as:
1256 // jmp_if_X TBB
1257 // jmp TmpBB
1258 // TmpBB:
1259 // jmp_if_Y TBB
1260 // jmp FBB
1261 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001264 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001266 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001267 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001268 } else {
1269 assert(Opc == Instruction::And && "Unknown merge op!");
1270 // Codegen X & Y as:
1271 // jmp_if_X TmpBB
1272 // jmp FBB
1273 // TmpBB:
1274 // jmp_if_Y TBB
1275 // jmp FBB
1276 //
1277 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001278
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001279 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001280 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001282 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001283 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001284 }
1285}
1286
1287/// If the set of cases should be emitted as a series of branches, return true.
1288/// If we should emit this as a bunch of and/or'd together conditions, return
1289/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001290bool
Dan Gohman2048b852009-11-23 18:04:58 +00001291SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001292 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001293
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001294 // If this is two comparisons of the same values or'd or and'd together, they
1295 // will get folded into a single comparison, so don't emit two blocks.
1296 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1297 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1298 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1299 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1300 return false;
1301 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001302
Chris Lattner133ce872010-01-02 00:00:03 +00001303 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1304 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1305 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1306 Cases[0].CC == Cases[1].CC &&
1307 isa<Constant>(Cases[0].CmpRHS) &&
1308 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1309 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1310 return false;
1311 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1312 return false;
1313 }
1314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 return true;
1316}
1317
Dan Gohman46510a72010-04-15 01:51:59 +00001318void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001319 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1320
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001321 // Update machine-CFG edges.
1322 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1323
1324 // Figure out which block is immediately after the current one.
1325 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001326 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001327 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001328 NextBlock = BBI;
1329
1330 if (I.isUnconditional()) {
1331 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001332 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001335 if (Succ0MBB != NextBlock)
1336 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001338 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001339
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001340 return;
1341 }
1342
1343 // If this condition is one of the special cases we handle, do special stuff
1344 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001345 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001346 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1347
1348 // If this is a series of conditions that are or'd or and'd together, emit
1349 // this as a sequence of branches instead of setcc's with and/or operations.
1350 // For example, instead of something like:
1351 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001352 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001353 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001354 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001355 // or C, F
1356 // jnz foo
1357 // Emit:
1358 // cmp A, B
1359 // je foo
1360 // cmp D, E
1361 // jle foo
1362 //
Dan Gohman46510a72010-04-15 01:51:59 +00001363 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001364 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 (BOp->getOpcode() == Instruction::And ||
1366 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001367 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1368 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001369 // If the compares in later blocks need to use values not currently
1370 // exported from this block, export them now. This block should always
1371 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001372 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001373
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001374 // Allow some cases to be rejected.
1375 if (ShouldEmitAsBranches(SwitchCases)) {
1376 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1377 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1378 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1379 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001380
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001381 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001382 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001383 SwitchCases.erase(SwitchCases.begin());
1384 return;
1385 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001386
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001387 // Okay, we decided not to do this, remove any inserted MBB's and clear
1388 // SwitchCases.
1389 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001390 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001391
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001392 SwitchCases.clear();
1393 }
1394 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001395
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001397 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001398 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001399
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001400 // Use visitSwitchCase to actually insert the fast branch sequence for this
1401 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001402 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001403}
1404
1405/// visitSwitchCase - Emits the necessary code to represent a single node in
1406/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001407void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1408 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409 SDValue Cond;
1410 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001411 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001412
1413 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001414 if (CB.CmpMHS == NULL) {
1415 // Fold "(X == true)" to X and "(X == false)" to !X to
1416 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001417 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001418 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001419 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001420 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001421 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001422 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001423 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001424 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001425 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001426 } else {
1427 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1428
Anton Korobeynikov23218582008-12-23 22:25:27 +00001429 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1430 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001431
1432 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001433 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001434
1435 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001436 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001437 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001438 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001439 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001440 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442 DAG.getConstant(High-Low, VT), ISD::SETULE);
1443 }
1444 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001445
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001446 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001447 SwitchBB->addSuccessor(CB.TrueBB);
1448 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001449
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001450 // Set NextBlock to be the MBB immediately after the current one, if any.
1451 // This is used to avoid emitting unnecessary branches to the next block.
1452 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001453 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001454 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001455 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457 // If the lhs block is the next block, invert the condition so that we can
1458 // fall through to the lhs instead of the rhs block.
1459 if (CB.TrueBB == NextBlock) {
1460 std::swap(CB.TrueBB, CB.FalseBB);
1461 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001462 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001463 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001464
Dale Johannesenf5d97892009-02-04 01:48:28 +00001465 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001466 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001467 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001468
Dan Gohmandeca0522010-06-24 17:08:31 +00001469 // Insert the false branch.
1470 if (CB.FalseBB != NextBlock)
1471 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1472 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001473
1474 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001475}
1476
1477/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001478void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001479 // Emit the code for the jump table
1480 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001481 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001482 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1483 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001484 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001485 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1486 MVT::Other, Index.getValue(1),
1487 Table, Index);
1488 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001489}
1490
1491/// visitJumpTableHeader - This function emits necessary code to produce index
1492/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001493void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001494 JumpTableHeader &JTH,
1495 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001496 // Subtract the lowest switch case value from the value being switched on and
1497 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001498 // difference between smallest and largest cases.
1499 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001500 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001501 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001502 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001503
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001504 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001505 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001506 // can be used as an index into the jump table in a subsequent basic block.
1507 // This value may be smaller or larger than the target's pointer type, and
1508 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001509 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001510
Dan Gohman89496d02010-07-02 00:10:16 +00001511 unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001512 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1513 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001514 JT.Reg = JumpTableReg;
1515
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001516 // Emit the range check for the jump table, and branch to the default block
1517 // for the switch statement if the value being switched on exceeds the largest
1518 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001519 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001520 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001521 DAG.getConstant(JTH.Last-JTH.First,VT),
1522 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001523
1524 // Set NextBlock to be the MBB immediately after the current one, if any.
1525 // This is used to avoid emitting unnecessary branches to the next block.
1526 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001527 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001528
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001529 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001530 NextBlock = BBI;
1531
Dale Johannesen66978ee2009-01-31 02:22:37 +00001532 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001534 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001535
Bill Wendling4533cac2010-01-28 21:51:40 +00001536 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001537 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1538 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001539
Bill Wendling87710f02009-12-21 23:47:40 +00001540 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001541}
1542
1543/// visitBitTestHeader - This function emits necessary code to produce value
1544/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001545void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1546 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001547 // Subtract the minimum value
1548 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001549 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001550 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001551 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552
1553 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001554 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001555 TLI.getSetCCResultType(Sub.getValueType()),
1556 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001557 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001558
Bill Wendling87710f02009-12-21 23:47:40 +00001559 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1560 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001561
Dan Gohman89496d02010-07-02 00:10:16 +00001562 B.Reg = FuncInfo.CreateReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001563 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1564 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001565
1566 // Set NextBlock to be the MBB immediately after the current one, if any.
1567 // This is used to avoid emitting unnecessary branches to the next block.
1568 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001569 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001570 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001571 NextBlock = BBI;
1572
1573 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1574
Dan Gohman99be8ae2010-04-19 22:41:47 +00001575 SwitchBB->addSuccessor(B.Default);
1576 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001577
Dale Johannesen66978ee2009-01-31 02:22:37 +00001578 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001580 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001581
Bill Wendling4533cac2010-01-28 21:51:40 +00001582 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001583 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1584 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001585
Bill Wendling87710f02009-12-21 23:47:40 +00001586 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001587}
1588
1589/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001590void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1591 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001592 BitTestCase &B,
1593 MachineBasicBlock *SwitchBB) {
Dale Johannesena04b7572009-02-03 23:04:43 +00001594 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001595 TLI.getPointerTy());
Dan Gohman8e0163a2010-06-24 02:06:24 +00001596 SDValue Cmp;
1597 if (CountPopulation_64(B.Mask) == 1) {
1598 // Testing for a single bit; just compare the shift count with what it
1599 // would need to be to shift a 1 bit in that position.
1600 Cmp = DAG.getSetCC(getCurDebugLoc(),
1601 TLI.getSetCCResultType(ShiftOp.getValueType()),
1602 ShiftOp,
1603 DAG.getConstant(CountTrailingZeros_64(B.Mask),
1604 TLI.getPointerTy()),
1605 ISD::SETEQ);
1606 } else {
1607 // Make desired shift
1608 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
1609 TLI.getPointerTy(),
1610 DAG.getConstant(1, TLI.getPointerTy()),
1611 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001612
Dan Gohman8e0163a2010-06-24 02:06:24 +00001613 // Emit bit tests and jumps
1614 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
1615 TLI.getPointerTy(), SwitchVal,
1616 DAG.getConstant(B.Mask, TLI.getPointerTy()));
1617 Cmp = DAG.getSetCC(getCurDebugLoc(),
1618 TLI.getSetCCResultType(AndOp.getValueType()),
1619 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
1620 ISD::SETNE);
1621 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622
Dan Gohman99be8ae2010-04-19 22:41:47 +00001623 SwitchBB->addSuccessor(B.TargetBB);
1624 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001625
Dale Johannesen66978ee2009-01-31 02:22:37 +00001626 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001627 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001628 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001629
1630 // Set NextBlock to be the MBB immediately after the current one, if any.
1631 // This is used to avoid emitting unnecessary branches to the next block.
1632 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001633 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001634 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001635 NextBlock = BBI;
1636
Bill Wendling4533cac2010-01-28 21:51:40 +00001637 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001638 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1639 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001640
Bill Wendling87710f02009-12-21 23:47:40 +00001641 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001642}
1643
Dan Gohman46510a72010-04-15 01:51:59 +00001644void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001645 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1646
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001647 // Retrieve successors.
1648 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1649 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1650
Gabor Greifb67e6b32009-01-15 11:10:44 +00001651 const Value *Callee(I.getCalledValue());
1652 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653 visitInlineAsm(&I);
1654 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001655 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656
1657 // If the value of the invoke is used outside of its defining block, make it
1658 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001659 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001660
1661 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001662 InvokeMBB->addSuccessor(Return);
1663 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001664
1665 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001666 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1667 MVT::Other, getControlRoot(),
1668 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001669}
1670
Dan Gohman46510a72010-04-15 01:51:59 +00001671void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001672}
1673
1674/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1675/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001676bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1677 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001678 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001679 MachineBasicBlock *Default,
1680 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001682
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001683 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001684 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001685 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001686 return false;
1687
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001688 // Get the MachineFunction which holds the current MBB. This is used when
1689 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001690 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001691
1692 // Figure out which block is immediately after the current one.
1693 MachineBasicBlock *NextBlock = 0;
1694 MachineFunction::iterator BBI = CR.CaseBB;
1695
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001696 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001697 NextBlock = BBI;
1698
1699 // TODO: If any two of the cases has the same destination, and if one value
1700 // is the same as the other, but has one bit unset that the other has set,
1701 // use bit manipulation to do two compares at once. For example:
1702 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001703
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001704 // Rearrange the case blocks so that the last one falls through if possible.
1705 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1706 // The last case block won't fall through into 'NextBlock' if we emit the
1707 // branches in this order. See if rearranging a case value would help.
1708 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1709 if (I->BB == NextBlock) {
1710 std::swap(*I, BackCase);
1711 break;
1712 }
1713 }
1714 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001715
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001716 // Create a CaseBlock record representing a conditional branch to
1717 // the Case's target mbb if the value being switched on SV is equal
1718 // to C.
1719 MachineBasicBlock *CurBlock = CR.CaseBB;
1720 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1721 MachineBasicBlock *FallThrough;
1722 if (I != E-1) {
1723 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1724 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001725
1726 // Put SV in a virtual register to make it available from the new blocks.
1727 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001728 } else {
1729 // If the last case doesn't match, go to the default block.
1730 FallThrough = Default;
1731 }
1732
Dan Gohman46510a72010-04-15 01:51:59 +00001733 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001734 ISD::CondCode CC;
1735 if (I->High == I->Low) {
1736 // This is just small small case range :) containing exactly 1 case
1737 CC = ISD::SETEQ;
1738 LHS = SV; RHS = I->High; MHS = NULL;
1739 } else {
1740 CC = ISD::SETLE;
1741 LHS = I->Low; MHS = SV; RHS = I->High;
1742 }
1743 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001744
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001745 // If emitting the first comparison, just call visitSwitchCase to emit the
1746 // code into the current block. Otherwise, push the CaseBlock onto the
1747 // vector to be later processed by SDISel, and insert the node's MBB
1748 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001749 if (CurBlock == SwitchBB)
1750 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001751 else
1752 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001753
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001754 CurBlock = FallThrough;
1755 }
1756
1757 return true;
1758}
1759
1760static inline bool areJTsAllowed(const TargetLowering &TLI) {
1761 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1763 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001764}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001765
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001766static APInt ComputeRange(const APInt &First, const APInt &Last) {
1767 APInt LastExt(Last), FirstExt(First);
1768 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1769 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1770 return (LastExt - FirstExt + 1ULL);
1771}
1772
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001773/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001774bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1775 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001776 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001777 MachineBasicBlock* Default,
1778 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001779 Case& FrontCase = *CR.Range.first;
1780 Case& BackCase = *(CR.Range.second-1);
1781
Chris Lattnere880efe2009-11-07 07:50:34 +00001782 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1783 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001784
Chris Lattnere880efe2009-11-07 07:50:34 +00001785 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001786 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1787 I!=E; ++I)
1788 TSize += I->size();
1789
Dan Gohmane0567812010-04-08 23:03:40 +00001790 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001792
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001793 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001794 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001795 if (Density < 0.4)
1796 return false;
1797
David Greene4b69d992010-01-05 01:24:57 +00001798 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001799 << "First entry: " << First << ". Last entry: " << Last << '\n'
1800 << "Range: " << Range
1801 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001802
1803 // Get the MachineFunction which holds the current MBB. This is used when
1804 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001805 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001806
1807 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001809 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001810
1811 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1812
1813 // Create a new basic block to hold the code for loading the address
1814 // of the jump table, and jumping to it. Update successor information;
1815 // we will either branch to the default case for the switch, or the jump
1816 // table.
1817 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1818 CurMF->insert(BBI, JumpTableBB);
1819 CR.CaseBB->addSuccessor(Default);
1820 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001821
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001822 // Build a vector of destination BBs, corresponding to each target
1823 // of the jump table. If the value of the jump table slot corresponds to
1824 // a case statement, push the case's BB onto the vector, otherwise, push
1825 // the default BB.
1826 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001827 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001828 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001829 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1830 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001831
1832 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001833 DestBBs.push_back(I->BB);
1834 if (TEI==High)
1835 ++I;
1836 } else {
1837 DestBBs.push_back(Default);
1838 }
1839 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001840
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001841 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001842 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1843 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001844 E = DestBBs.end(); I != E; ++I) {
1845 if (!SuccsHandled[(*I)->getNumber()]) {
1846 SuccsHandled[(*I)->getNumber()] = true;
1847 JumpTableBB->addSuccessor(*I);
1848 }
1849 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001850
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001851 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001852 unsigned JTEncoding = TLI.getJumpTableEncoding();
1853 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001854 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001855
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001856 // Set the jump table information so that we can codegen it as a second
1857 // MachineBasicBlock
1858 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001859 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1860 if (CR.CaseBB == SwitchBB)
1861 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001862
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 JTCases.push_back(JumpTableBlock(JTH, JT));
1864
1865 return true;
1866}
1867
1868/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1869/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001870bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1871 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001872 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001873 MachineBasicBlock *Default,
1874 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001875 // Get the MachineFunction which holds the current MBB. This is used when
1876 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001877 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001878
1879 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001880 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001881 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882
1883 Case& FrontCase = *CR.Range.first;
1884 Case& BackCase = *(CR.Range.second-1);
1885 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1886
1887 // Size is the number of Cases represented by this range.
1888 unsigned Size = CR.Range.second - CR.Range.first;
1889
Chris Lattnere880efe2009-11-07 07:50:34 +00001890 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1891 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001892 double FMetric = 0;
1893 CaseItr Pivot = CR.Range.first + Size/2;
1894
1895 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1896 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001897 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001898 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1899 I!=E; ++I)
1900 TSize += I->size();
1901
Chris Lattnere880efe2009-11-07 07:50:34 +00001902 APInt LSize = FrontCase.size();
1903 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001904 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001905 << "First: " << First << ", Last: " << Last <<'\n'
1906 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001907 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1908 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001909 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1910 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001911 APInt Range = ComputeRange(LEnd, RBegin);
1912 assert((Range - 2ULL).isNonNegative() &&
1913 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001914 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001915 (LEnd - First + 1ULL).roundToDouble();
1916 double RDensity = (double)RSize.roundToDouble() /
1917 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001918 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001920 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001921 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1922 << "LDensity: " << LDensity
1923 << ", RDensity: " << RDensity << '\n'
1924 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001925 if (FMetric < Metric) {
1926 Pivot = J;
1927 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001928 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929 }
1930
1931 LSize += J->size();
1932 RSize -= J->size();
1933 }
1934 if (areJTsAllowed(TLI)) {
1935 // If our case is dense we *really* should handle it earlier!
1936 assert((FMetric > 0) && "Should handle dense range earlier!");
1937 } else {
1938 Pivot = CR.Range.first + Size/2;
1939 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001940
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941 CaseRange LHSR(CR.Range.first, Pivot);
1942 CaseRange RHSR(Pivot, CR.Range.second);
1943 Constant *C = Pivot->Low;
1944 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001947 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001948 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001949 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001950 // Pivot's Value, then we can branch directly to the LHS's Target,
1951 // rather than creating a leaf node for it.
1952 if ((LHSR.second - LHSR.first) == 1 &&
1953 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001954 cast<ConstantInt>(C)->getValue() ==
1955 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001956 TrueBB = LHSR.first->BB;
1957 } else {
1958 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1959 CurMF->insert(BBI, TrueBB);
1960 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001961
1962 // Put SV in a virtual register to make it available from the new blocks.
1963 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001964 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001965
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001966 // Similar to the optimization above, if the Value being switched on is
1967 // known to be less than the Constant CR.LT, and the current Case Value
1968 // is CR.LT - 1, then we can branch directly to the target block for
1969 // the current Case Value, rather than emitting a RHS leaf node for it.
1970 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001971 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1972 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001973 FalseBB = RHSR.first->BB;
1974 } else {
1975 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1976 CurMF->insert(BBI, FalseBB);
1977 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001978
1979 // Put SV in a virtual register to make it available from the new blocks.
1980 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001981 }
1982
1983 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001984 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001985 // Otherwise, branch to LHS.
1986 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1987
Dan Gohman99be8ae2010-04-19 22:41:47 +00001988 if (CR.CaseBB == SwitchBB)
1989 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990 else
1991 SwitchCases.push_back(CB);
1992
1993 return true;
1994}
1995
1996/// handleBitTestsSwitchCase - if current case range has few destination and
1997/// range span less, than machine word bitwidth, encode case range into series
1998/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001999bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2000 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002001 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002002 MachineBasicBlock* Default,
2003 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00002004 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002005 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002006
2007 Case& FrontCase = *CR.Range.first;
2008 Case& BackCase = *(CR.Range.second-1);
2009
2010 // Get the MachineFunction which holds the current MBB. This is used when
2011 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002012 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002013
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002014 // If target does not have legal shift left, do not emit bit tests at all.
2015 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
2016 return false;
2017
Anton Korobeynikov23218582008-12-23 22:25:27 +00002018 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002019 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2020 I!=E; ++I) {
2021 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002022 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002023 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002024
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 // Count unique destinations
2026 SmallSet<MachineBasicBlock*, 4> Dests;
2027 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2028 Dests.insert(I->BB);
2029 if (Dests.size() > 3)
2030 // Don't bother the code below, if there are too much unique destinations
2031 return false;
2032 }
David Greene4b69d992010-01-05 01:24:57 +00002033 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002034 << Dests.size() << '\n'
2035 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002036
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002037 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002038 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2039 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002040 APInt cmpRange = maxValue - minValue;
2041
David Greene4b69d992010-01-05 01:24:57 +00002042 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002043 << "Low bound: " << minValue << '\n'
2044 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002045
Dan Gohmane0567812010-04-08 23:03:40 +00002046 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002047 (!(Dests.size() == 1 && numCmps >= 3) &&
2048 !(Dests.size() == 2 && numCmps >= 5) &&
2049 !(Dests.size() >= 3 && numCmps >= 6)))
2050 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002051
David Greene4b69d992010-01-05 01:24:57 +00002052 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002053 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002055 // Optimize the case where all the case values fit in a
2056 // word without having to subtract minValue. In this case,
2057 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00002058 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002059 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002060 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002061 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002062 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002063
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002064 CaseBitsVector CasesBits;
2065 unsigned i, count = 0;
2066
2067 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2068 MachineBasicBlock* Dest = I->BB;
2069 for (i = 0; i < count; ++i)
2070 if (Dest == CasesBits[i].BB)
2071 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002072
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002073 if (i == count) {
2074 assert((count < 3) && "Too much destinations to test!");
2075 CasesBits.push_back(CaseBits(0, Dest, 0));
2076 count++;
2077 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002078
2079 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2080 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2081
2082 uint64_t lo = (lowValue - lowBound).getZExtValue();
2083 uint64_t hi = (highValue - lowBound).getZExtValue();
2084
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002085 for (uint64_t j = lo; j <= hi; j++) {
2086 CasesBits[i].Mask |= 1ULL << j;
2087 CasesBits[i].Bits++;
2088 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002089
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002090 }
2091 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002092
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093 BitTestInfo BTC;
2094
2095 // Figure out which block is immediately after the current one.
2096 MachineFunction::iterator BBI = CR.CaseBB;
2097 ++BBI;
2098
2099 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2100
David Greene4b69d992010-01-05 01:24:57 +00002101 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002102 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002103 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002104 << ", Bits: " << CasesBits[i].Bits
2105 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002106
2107 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2108 CurMF->insert(BBI, CaseBB);
2109 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2110 CaseBB,
2111 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002112
2113 // Put SV in a virtual register to make it available from the new blocks.
2114 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002115 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002116
2117 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002118 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002119 CR.CaseBB, Default, BTC);
2120
Dan Gohman99be8ae2010-04-19 22:41:47 +00002121 if (CR.CaseBB == SwitchBB)
2122 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002124 BitTestCases.push_back(BTB);
2125
2126 return true;
2127}
2128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002130size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2131 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002132 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133
2134 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002135 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002136 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2137 Cases.push_back(Case(SI.getSuccessorValue(i),
2138 SI.getSuccessorValue(i),
2139 SMBB));
2140 }
2141 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2142
2143 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002144 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002145 // Must recompute end() each iteration because it may be
2146 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002147 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2148 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2149 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002150 MachineBasicBlock* nextBB = J->BB;
2151 MachineBasicBlock* currentBB = I->BB;
2152
2153 // If the two neighboring cases go to the same destination, merge them
2154 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002155 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002156 I->High = J->High;
2157 J = Cases.erase(J);
2158 } else {
2159 I = J++;
2160 }
2161 }
2162
2163 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2164 if (I->Low != I->High)
2165 // A range counts double, since it requires two compares.
2166 ++numCmps;
2167 }
2168
2169 return numCmps;
2170}
2171
Dan Gohman46510a72010-04-15 01:51:59 +00002172void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002173 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
2174
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002175 // Figure out which block is immediately after the current one.
2176 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002177 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2178
2179 // If there is only the default destination, branch to it if it is not the
2180 // next basic block. Otherwise, just fall through.
2181 if (SI.getNumOperands() == 2) {
2182 // Update machine-CFG edges.
2183
2184 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002185 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002186 if (Default != NextBlock)
2187 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
2188 MVT::Other, getControlRoot(),
2189 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002190
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002191 return;
2192 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002193
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002194 // If there are any non-default case statements, create a vector of Cases
2195 // representing each one, and sort the vector so that we can efficiently
2196 // create a binary search tree from them.
2197 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002198 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002199 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002200 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002201 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002202
2203 // Get the Value to be switched on and default basic blocks, which will be
2204 // inserted into CaseBlock records, representing basic blocks in the binary
2205 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002206 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207
2208 // Push the initial CaseRec onto the worklist
2209 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002210 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2211 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002212
2213 while (!WorkList.empty()) {
2214 // Grab a record representing a case range to process off the worklist
2215 CaseRec CR = WorkList.back();
2216 WorkList.pop_back();
2217
Dan Gohman99be8ae2010-04-19 22:41:47 +00002218 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002219 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002220
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002221 // If the range has few cases (two or less) emit a series of specific
2222 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002223 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002225
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002226 // If the switch has more than 5 blocks, and at least 40% dense, and the
2227 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002229 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002230 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002231
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002232 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2233 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002234 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235 }
2236}
2237
Dan Gohman46510a72010-04-15 01:51:59 +00002238void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002239 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2240
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002241 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002242 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002243 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002244 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002245 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002246 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002247 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2248 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002249 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002250
Bill Wendling4533cac2010-01-28 21:51:40 +00002251 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2252 MVT::Other, getControlRoot(),
2253 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002254}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002255
Dan Gohman46510a72010-04-15 01:51:59 +00002256void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002257 // -0.0 - X --> fneg
2258 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002259 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002260 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2261 const VectorType *DestTy = cast<VectorType>(I.getType());
2262 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002263 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002264 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002265 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002266 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002267 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002268 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2269 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002270 return;
2271 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002272 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002274
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002275 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002276 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002277 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002278 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2279 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002280 return;
2281 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002282
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002283 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002284}
2285
Dan Gohman46510a72010-04-15 01:51:59 +00002286void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002287 SDValue Op1 = getValue(I.getOperand(0));
2288 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002289 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2290 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002291}
2292
Dan Gohman46510a72010-04-15 01:51:59 +00002293void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002294 SDValue Op1 = getValue(I.getOperand(0));
2295 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002296 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002297 Op2.getValueType() != TLI.getShiftAmountTy()) {
2298 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002299 EVT PTy = TLI.getPointerTy();
2300 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002301 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002302 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2303 TLI.getShiftAmountTy(), Op2);
2304 // If the operand is larger than the shift count type but the shift
2305 // count type has enough bits to represent any shift value, truncate
2306 // it now. This is a common case and it exposes the truncate to
2307 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002308 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002309 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2310 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2311 TLI.getShiftAmountTy(), Op2);
2312 // Otherwise we'll need to temporarily settle for some other
2313 // convenient type; type legalization will make adjustments as
2314 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002315 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002316 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002317 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002318 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002319 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002320 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002321 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002322
Bill Wendling4533cac2010-01-28 21:51:40 +00002323 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2324 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002325}
2326
Dan Gohman46510a72010-04-15 01:51:59 +00002327void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002328 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002329 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002330 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002331 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002332 predicate = ICmpInst::Predicate(IC->getPredicate());
2333 SDValue Op1 = getValue(I.getOperand(0));
2334 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002335 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002336
Owen Andersone50ed302009-08-10 22:56:29 +00002337 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002338 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002339}
2340
Dan Gohman46510a72010-04-15 01:51:59 +00002341void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002342 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002343 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002344 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002345 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002346 predicate = FCmpInst::Predicate(FC->getPredicate());
2347 SDValue Op1 = getValue(I.getOperand(0));
2348 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002349 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002350 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002351 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002352}
2353
Dan Gohman46510a72010-04-15 01:51:59 +00002354void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002355 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002356 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2357 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002358 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002359
Bill Wendling49fcff82009-12-21 22:30:11 +00002360 SmallVector<SDValue, 4> Values(NumValues);
2361 SDValue Cond = getValue(I.getOperand(0));
2362 SDValue TrueVal = getValue(I.getOperand(1));
2363 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002364
Bill Wendling4533cac2010-01-28 21:51:40 +00002365 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002366 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002367 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2368 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002369 SDValue(TrueVal.getNode(),
2370 TrueVal.getResNo() + i),
2371 SDValue(FalseVal.getNode(),
2372 FalseVal.getResNo() + i));
2373
Bill Wendling4533cac2010-01-28 21:51:40 +00002374 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2375 DAG.getVTList(&ValueVTs[0], NumValues),
2376 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002377}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002378
Dan Gohman46510a72010-04-15 01:51:59 +00002379void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002380 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2381 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002382 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002383 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002384}
2385
Dan Gohman46510a72010-04-15 01:51:59 +00002386void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002387 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2388 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2389 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002390 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002391 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002392}
2393
Dan Gohman46510a72010-04-15 01:51:59 +00002394void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002395 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2396 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2397 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002398 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002399 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002400}
2401
Dan Gohman46510a72010-04-15 01:51:59 +00002402void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002403 // FPTrunc is never a no-op cast, no need to check
2404 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002405 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002406 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2407 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002408}
2409
Dan Gohman46510a72010-04-15 01:51:59 +00002410void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002411 // FPTrunc is never a no-op cast, no need to check
2412 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002413 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002414 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002415}
2416
Dan Gohman46510a72010-04-15 01:51:59 +00002417void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002418 // FPToUI is never a no-op cast, no need to check
2419 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002420 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002421 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002422}
2423
Dan Gohman46510a72010-04-15 01:51:59 +00002424void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002425 // FPToSI is never a no-op cast, no need to check
2426 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002427 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002428 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002429}
2430
Dan Gohman46510a72010-04-15 01:51:59 +00002431void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002432 // UIToFP is never a no-op cast, no need to check
2433 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002434 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002435 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002436}
2437
Dan Gohman46510a72010-04-15 01:51:59 +00002438void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002439 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002440 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002441 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002442 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002443}
2444
Dan Gohman46510a72010-04-15 01:51:59 +00002445void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002446 // What to do depends on the size of the integer and the size of the pointer.
2447 // We can either truncate, zero extend, or no-op, accordingly.
2448 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002449 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002450 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002451}
2452
Dan Gohman46510a72010-04-15 01:51:59 +00002453void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002454 // What to do depends on the size of the integer and the size of the pointer.
2455 // We can either truncate, zero extend, or no-op, accordingly.
2456 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002457 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002458 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002459}
2460
Dan Gohman46510a72010-04-15 01:51:59 +00002461void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002462 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002463 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002464
Bill Wendling49fcff82009-12-21 22:30:11 +00002465 // BitCast assures us that source and destination are the same size so this is
2466 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002467 if (DestVT != N.getValueType())
2468 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2469 DestVT, N)); // convert types.
2470 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002471 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002472}
2473
Dan Gohman46510a72010-04-15 01:51:59 +00002474void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002475 SDValue InVec = getValue(I.getOperand(0));
2476 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002477 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002478 TLI.getPointerTy(),
2479 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002480 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2481 TLI.getValueType(I.getType()),
2482 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002483}
2484
Dan Gohman46510a72010-04-15 01:51:59 +00002485void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002486 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002487 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002488 TLI.getPointerTy(),
2489 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002490 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2491 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492}
2493
Mon P Wangaeb06d22008-11-10 04:46:22 +00002494// Utility for visitShuffleVector - Returns true if the mask is mask starting
2495// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002496static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2497 unsigned MaskNumElts = Mask.size();
2498 for (unsigned i = 0; i != MaskNumElts; ++i)
2499 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002500 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002501 return true;
2502}
2503
Dan Gohman46510a72010-04-15 01:51:59 +00002504void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002505 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002506 SDValue Src1 = getValue(I.getOperand(0));
2507 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002508
Nate Begeman9008ca62009-04-27 18:41:29 +00002509 // Convert the ConstantVector mask operand into an array of ints, with -1
2510 // representing undef values.
2511 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002512 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002513 unsigned MaskNumElts = MaskElts.size();
2514 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002515 if (isa<UndefValue>(MaskElts[i]))
2516 Mask.push_back(-1);
2517 else
2518 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2519 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002520
Owen Andersone50ed302009-08-10 22:56:29 +00002521 EVT VT = TLI.getValueType(I.getType());
2522 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002523 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002524
Mon P Wangc7849c22008-11-16 05:06:27 +00002525 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002526 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2527 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002528 return;
2529 }
2530
2531 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002532 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2533 // Mask is longer than the source vectors and is a multiple of the source
2534 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002535 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002536 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2537 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002538 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2539 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002540 return;
2541 }
2542
Mon P Wangc7849c22008-11-16 05:06:27 +00002543 // Pad both vectors with undefs to make them the same length as the mask.
2544 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002545 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2546 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002547 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002548
Nate Begeman9008ca62009-04-27 18:41:29 +00002549 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2550 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002551 MOps1[0] = Src1;
2552 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002553
2554 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2555 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002556 &MOps1[0], NumConcat);
2557 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002558 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002559 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002560
Mon P Wangaeb06d22008-11-10 04:46:22 +00002561 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002562 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002563 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002564 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002565 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002566 MappedOps.push_back(Idx);
2567 else
2568 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002569 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002570
Bill Wendling4533cac2010-01-28 21:51:40 +00002571 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2572 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002573 return;
2574 }
2575
Mon P Wangc7849c22008-11-16 05:06:27 +00002576 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002577 // Analyze the access pattern of the vector to see if we can extract
2578 // two subvectors and do the shuffle. The analysis is done by calculating
2579 // the range of elements the mask access on both vectors.
2580 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2581 int MaxRange[2] = {-1, -1};
2582
Nate Begeman5a5ca152009-04-29 05:20:52 +00002583 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002584 int Idx = Mask[i];
2585 int Input = 0;
2586 if (Idx < 0)
2587 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002588
Nate Begeman5a5ca152009-04-29 05:20:52 +00002589 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002590 Input = 1;
2591 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002592 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002593 if (Idx > MaxRange[Input])
2594 MaxRange[Input] = Idx;
2595 if (Idx < MinRange[Input])
2596 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002597 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002598
Mon P Wangc7849c22008-11-16 05:06:27 +00002599 // Check if the access is smaller than the vector size and can we find
2600 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002601 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2602 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002603 int StartIdx[2]; // StartIdx to extract from
2604 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002605 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002606 RangeUse[Input] = 0; // Unused
2607 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002608 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002609 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002610 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002611 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002612 RangeUse[Input] = 1; // Extract from beginning of the vector
2613 StartIdx[Input] = 0;
2614 } else {
2615 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002616 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002617 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002618 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002619 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002620 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002621 }
2622
Bill Wendling636e2582009-08-21 18:16:06 +00002623 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002624 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002625 return;
2626 }
2627 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2628 // Extract appropriate subvector and generate a vector shuffle
2629 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002630 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002631 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002632 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002633 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002634 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002635 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002636 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002637
Mon P Wangc7849c22008-11-16 05:06:27 +00002638 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002639 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002640 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002641 int Idx = Mask[i];
2642 if (Idx < 0)
2643 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002644 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002645 MappedOps.push_back(Idx - StartIdx[0]);
2646 else
2647 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002648 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002649
Bill Wendling4533cac2010-01-28 21:51:40 +00002650 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2651 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002652 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002653 }
2654 }
2655
Mon P Wangc7849c22008-11-16 05:06:27 +00002656 // We can't use either concat vectors or extract subvectors so fall back to
2657 // replacing the shuffle with extract and build vector.
2658 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002659 EVT EltVT = VT.getVectorElementType();
2660 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002661 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002662 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002663 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002664 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002665 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002666 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002667 SDValue Res;
2668
Nate Begeman5a5ca152009-04-29 05:20:52 +00002669 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002670 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2671 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002672 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002673 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2674 EltVT, Src2,
2675 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2676
2677 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002678 }
2679 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002680
Bill Wendling4533cac2010-01-28 21:51:40 +00002681 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2682 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002683}
2684
Dan Gohman46510a72010-04-15 01:51:59 +00002685void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002686 const Value *Op0 = I.getOperand(0);
2687 const Value *Op1 = I.getOperand(1);
2688 const Type *AggTy = I.getType();
2689 const Type *ValTy = Op1->getType();
2690 bool IntoUndef = isa<UndefValue>(Op0);
2691 bool FromUndef = isa<UndefValue>(Op1);
2692
2693 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2694 I.idx_begin(), I.idx_end());
2695
Owen Andersone50ed302009-08-10 22:56:29 +00002696 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002697 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002698 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002699 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2700
2701 unsigned NumAggValues = AggValueVTs.size();
2702 unsigned NumValValues = ValValueVTs.size();
2703 SmallVector<SDValue, 4> Values(NumAggValues);
2704
2705 SDValue Agg = getValue(Op0);
2706 SDValue Val = getValue(Op1);
2707 unsigned i = 0;
2708 // Copy the beginning value(s) from the original aggregate.
2709 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002710 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711 SDValue(Agg.getNode(), Agg.getResNo() + i);
2712 // Copy values from the inserted value(s).
2713 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002714 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002715 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2716 // Copy remaining value(s) from the original aggregate.
2717 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002718 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002719 SDValue(Agg.getNode(), Agg.getResNo() + i);
2720
Bill Wendling4533cac2010-01-28 21:51:40 +00002721 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2722 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2723 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002724}
2725
Dan Gohman46510a72010-04-15 01:51:59 +00002726void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002727 const Value *Op0 = I.getOperand(0);
2728 const Type *AggTy = Op0->getType();
2729 const Type *ValTy = I.getType();
2730 bool OutOfUndef = isa<UndefValue>(Op0);
2731
2732 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2733 I.idx_begin(), I.idx_end());
2734
Owen Andersone50ed302009-08-10 22:56:29 +00002735 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002736 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2737
2738 unsigned NumValValues = ValValueVTs.size();
2739 SmallVector<SDValue, 4> Values(NumValValues);
2740
2741 SDValue Agg = getValue(Op0);
2742 // Copy out the selected value(s).
2743 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2744 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002745 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002746 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002747 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002748
Bill Wendling4533cac2010-01-28 21:51:40 +00002749 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2750 DAG.getVTList(&ValValueVTs[0], NumValValues),
2751 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002752}
2753
Dan Gohman46510a72010-04-15 01:51:59 +00002754void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002755 SDValue N = getValue(I.getOperand(0));
2756 const Type *Ty = I.getOperand(0)->getType();
2757
Dan Gohman46510a72010-04-15 01:51:59 +00002758 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002759 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002760 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002761 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2762 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2763 if (Field) {
2764 // N = N + Offset
2765 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002766 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002767 DAG.getIntPtrConstant(Offset));
2768 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002769
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002770 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002771 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2772 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2773
2774 // Offset canonically 0 for unions, but type changes
2775 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002776 } else {
2777 Ty = cast<SequentialType>(Ty)->getElementType();
2778
2779 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002780 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00002781 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002782 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002783 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002784 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002785 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002786 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002787 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002788 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2789 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002790 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002791 else
Evan Chengb1032a82009-02-09 20:54:38 +00002792 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002793
Dale Johannesen66978ee2009-01-31 02:22:37 +00002794 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002795 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796 continue;
2797 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002798
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002799 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002800 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2801 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002802 SDValue IdxN = getValue(Idx);
2803
2804 // If the index is smaller or larger than intptr_t, truncate or extend
2805 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002806 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807
2808 // If this is a multiply by a power of two, turn it into a shl
2809 // immediately. This is a very common case.
2810 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002811 if (ElementSize.isPowerOf2()) {
2812 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002813 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002814 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002815 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002816 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002817 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002818 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002819 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002820 }
2821 }
2822
Scott Michelfdc40a02009-02-17 22:15:04 +00002823 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002824 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 }
2826 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002828 setValue(&I, N);
2829}
2830
Dan Gohman46510a72010-04-15 01:51:59 +00002831void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 // If this is a fixed sized alloca in the entry block of the function,
2833 // allocate it statically on the stack.
2834 if (FuncInfo.StaticAllocaMap.count(&I))
2835 return; // getValue will auto-populate this.
2836
2837 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002838 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002839 unsigned Align =
2840 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2841 I.getAlignment());
2842
2843 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002844
Owen Andersone50ed302009-08-10 22:56:29 +00002845 EVT IntPtr = TLI.getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00002846 if (AllocSize.getValueType() != IntPtr)
2847 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
2848
2849 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr,
2850 AllocSize,
2851 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002852
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002853 // Handle alignment. If the requested alignment is less than or equal to
2854 // the stack alignment, ignore it. If the size is greater than or equal to
2855 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002856 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002857 if (Align <= StackAlign)
2858 Align = 0;
2859
2860 // Round the size of the allocation up to the stack alignment size
2861 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002862 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002863 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002864 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002865
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002866 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002867 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002868 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2870
2871 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002872 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002873 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002874 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002875 setValue(&I, DSA);
2876 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002877
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002878 // Inform the Frame Information that we have just allocated a variable-sized
2879 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002880 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881}
2882
Dan Gohman46510a72010-04-15 01:51:59 +00002883void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002884 const Value *SV = I.getOperand(0);
2885 SDValue Ptr = getValue(SV);
2886
2887 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002888
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002889 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002890 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002891 unsigned Alignment = I.getAlignment();
2892
Owen Andersone50ed302009-08-10 22:56:29 +00002893 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002894 SmallVector<uint64_t, 4> Offsets;
2895 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2896 unsigned NumValues = ValueVTs.size();
2897 if (NumValues == 0)
2898 return;
2899
2900 SDValue Root;
2901 bool ConstantMemory = false;
2902 if (I.isVolatile())
2903 // Serialize volatile loads with other side effects.
2904 Root = getRoot();
2905 else if (AA->pointsToConstantMemory(SV)) {
2906 // Do not serialize (non-volatile) loads of constant memory with anything.
2907 Root = DAG.getEntryNode();
2908 ConstantMemory = true;
2909 } else {
2910 // Do not serialize non-volatile loads against each other.
2911 Root = DAG.getRoot();
2912 }
2913
2914 SmallVector<SDValue, 4> Values(NumValues);
2915 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002916 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002917 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002918 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2919 PtrVT, Ptr,
2920 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002921 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002922 A, SV, Offsets[i], isVolatile,
2923 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002924
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002925 Values[i] = L;
2926 Chains[i] = L.getValue(1);
2927 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002928
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002929 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002930 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002931 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002932 if (isVolatile)
2933 DAG.setRoot(Chain);
2934 else
2935 PendingLoads.push_back(Chain);
2936 }
2937
Bill Wendling4533cac2010-01-28 21:51:40 +00002938 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2939 DAG.getVTList(&ValueVTs[0], NumValues),
2940 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002941}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002942
Dan Gohman46510a72010-04-15 01:51:59 +00002943void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2944 const Value *SrcV = I.getOperand(0);
2945 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002946
Owen Andersone50ed302009-08-10 22:56:29 +00002947 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002948 SmallVector<uint64_t, 4> Offsets;
2949 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2950 unsigned NumValues = ValueVTs.size();
2951 if (NumValues == 0)
2952 return;
2953
2954 // Get the lowered operands. Note that we do this after
2955 // checking if NumResults is zero, because with zero results
2956 // the operands won't have values in the map.
2957 SDValue Src = getValue(SrcV);
2958 SDValue Ptr = getValue(PtrV);
2959
2960 SDValue Root = getRoot();
2961 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002962 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002963 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002964 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002965 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002966
2967 for (unsigned i = 0; i != NumValues; ++i) {
2968 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2969 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002970 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002971 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002972 Add, PtrV, Offsets[i], isVolatile,
2973 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002974 }
2975
Bill Wendling4533cac2010-01-28 21:51:40 +00002976 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2977 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002978}
2979
2980/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2981/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002982void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002983 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002984 bool HasChain = !I.doesNotAccessMemory();
2985 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2986
2987 // Build the operand list.
2988 SmallVector<SDValue, 8> Ops;
2989 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2990 if (OnlyLoad) {
2991 // We don't need to serialize loads against other loads.
2992 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002993 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002994 Ops.push_back(getRoot());
2995 }
2996 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002997
2998 // Info is set by getTgtMemInstrinsic
2999 TargetLowering::IntrinsicInfo Info;
3000 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3001
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003002 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003003 if (!IsTgtIntrinsic)
3004 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003005
3006 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003007 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3008 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003009 assert(TLI.isTypeLegal(Op.getValueType()) &&
3010 "Intrinsic uses a non-legal type?");
3011 Ops.push_back(Op);
3012 }
3013
Owen Andersone50ed302009-08-10 22:56:29 +00003014 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003015 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3016#ifndef NDEBUG
3017 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3018 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3019 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003020 }
Bob Wilson8d919552009-07-31 22:41:21 +00003021#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003022
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003023 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003024 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003025
Bob Wilson8d919552009-07-31 22:41:21 +00003026 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003027
3028 // Create the node.
3029 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003030 if (IsTgtIntrinsic) {
3031 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003032 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003033 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003034 Info.memVT, Info.ptrVal, Info.offset,
3035 Info.align, Info.vol,
3036 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003037 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003038 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003039 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003040 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003041 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003042 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003043 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003044 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003045 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003046 }
3047
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003048 if (HasChain) {
3049 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3050 if (OnlyLoad)
3051 PendingLoads.push_back(Chain);
3052 else
3053 DAG.setRoot(Chain);
3054 }
Bill Wendling856ff412009-12-22 00:12:37 +00003055
Benjamin Kramerf0127052010-01-05 13:12:22 +00003056 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003057 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003058 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003059 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003060 }
Bill Wendling856ff412009-12-22 00:12:37 +00003061
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003062 setValue(&I, Result);
3063 }
3064}
3065
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003066/// GetSignificand - Get the significand and build it into a floating-point
3067/// number with exponent of 1:
3068///
3069/// Op = (Op & 0x007fffff) | 0x3f800000;
3070///
3071/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003072static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00003073GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003074 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3075 DAG.getConstant(0x007fffff, MVT::i32));
3076 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3077 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003078 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003079}
3080
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003081/// GetExponent - Get the exponent:
3082///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003083/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003084///
3085/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003086static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003087GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00003088 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3090 DAG.getConstant(0x7f800000, MVT::i32));
3091 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003092 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003093 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3094 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003095 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003096}
3097
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003098/// getF32Constant - Get 32-bit floating point constant.
3099static SDValue
3100getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003101 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003102}
3103
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003104/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003105/// visitIntrinsicCall: I is a call instruction
3106/// Op is the associated NodeType for I
3107const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003108SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
3109 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003110 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003111 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003112 DAG.getAtomic(Op, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00003113 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003114 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00003115 getValue(I.getArgOperand(0)),
3116 getValue(I.getArgOperand(1)),
3117 I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003118 setValue(&I, L);
3119 DAG.setRoot(L.getValue(1));
3120 return 0;
3121}
3122
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003123// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003124const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003125SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Gabor Greif0635f352010-06-25 09:38:13 +00003126 SDValue Op1 = getValue(I.getArgOperand(0));
3127 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling74c37652008-12-09 22:08:41 +00003128
Owen Anderson825b72b2009-08-11 20:47:22 +00003129 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00003130 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003131 return 0;
3132}
Bill Wendling74c37652008-12-09 22:08:41 +00003133
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003134/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3135/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003136void
Dan Gohman46510a72010-04-15 01:51:59 +00003137SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003138 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003139 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003140
Gabor Greif0635f352010-06-25 09:38:13 +00003141 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003142 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003143 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003144
3145 // Put the exponent in the right bit position for later addition to the
3146 // final result:
3147 //
3148 // #define LOG2OFe 1.4426950f
3149 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003151 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003153
3154 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3156 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003157
3158 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003159 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003160 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003161
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003162 if (LimitFloatPrecision <= 6) {
3163 // For floating-point precision of 6:
3164 //
3165 // TwoToFractionalPartOfX =
3166 // 0.997535578f +
3167 // (0.735607626f + 0.252464424f * x) * x;
3168 //
3169 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003171 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003172 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003173 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003174 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3175 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003176 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003177 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003178
3179 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003180 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003181 TwoToFracPartOfX, IntegerPartOfX);
3182
Owen Anderson825b72b2009-08-11 20:47:22 +00003183 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003184 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3185 // For floating-point precision of 12:
3186 //
3187 // TwoToFractionalPartOfX =
3188 // 0.999892986f +
3189 // (0.696457318f +
3190 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3191 //
3192 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003193 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003194 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003196 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003197 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3198 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003199 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003200 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3201 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003202 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003204
3205 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003206 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003207 TwoToFracPartOfX, IntegerPartOfX);
3208
Owen Anderson825b72b2009-08-11 20:47:22 +00003209 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003210 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3211 // For floating-point precision of 18:
3212 //
3213 // TwoToFractionalPartOfX =
3214 // 0.999999982f +
3215 // (0.693148872f +
3216 // (0.240227044f +
3217 // (0.554906021e-1f +
3218 // (0.961591928e-2f +
3219 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3220 //
3221 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003223 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003224 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003225 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3227 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003228 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3230 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003231 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3233 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003234 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003235 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3236 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003237 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003238 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3239 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003240 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003241 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003242 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003243
3244 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003246 TwoToFracPartOfX, IntegerPartOfX);
3247
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003249 }
3250 } else {
3251 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003252 result = DAG.getNode(ISD::FEXP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003253 getValue(I.getArgOperand(0)).getValueType(),
3254 getValue(I.getArgOperand(0)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003255 }
3256
Dale Johannesen59e577f2008-09-05 18:38:42 +00003257 setValue(&I, result);
3258}
3259
Bill Wendling39150252008-09-09 20:39:27 +00003260/// visitLog - Lower a log intrinsic. Handles the special sequences for
3261/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003262void
Dan Gohman46510a72010-04-15 01:51:59 +00003263SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003264 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003265 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003266
Gabor Greif0635f352010-06-25 09:38:13 +00003267 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003268 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003269 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003270 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003271
3272 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003273 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003274 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003275 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003276
3277 // Get the significand and build it into a floating-point number with
3278 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003279 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003280
3281 if (LimitFloatPrecision <= 6) {
3282 // For floating-point precision of 6:
3283 //
3284 // LogofMantissa =
3285 // -1.1609546f +
3286 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003287 //
Bill Wendling39150252008-09-09 20:39:27 +00003288 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003290 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003292 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3294 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003295 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003296
Scott Michelfdc40a02009-02-17 22:15:04 +00003297 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003299 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3300 // For floating-point precision of 12:
3301 //
3302 // LogOfMantissa =
3303 // -1.7417939f +
3304 // (2.8212026f +
3305 // (-1.4699568f +
3306 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3307 //
3308 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003309 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003310 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003311 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003312 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3314 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003315 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3317 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003318 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3320 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003321 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003322
Scott Michelfdc40a02009-02-17 22:15:04 +00003323 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003324 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003325 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3326 // For floating-point precision of 18:
3327 //
3328 // LogOfMantissa =
3329 // -2.1072184f +
3330 // (4.2372794f +
3331 // (-3.7029485f +
3332 // (2.2781945f +
3333 // (-0.87823314f +
3334 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3335 //
3336 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003338 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003340 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3342 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003343 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3345 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003346 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3348 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3351 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003352 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3354 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003355 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003356
Scott Michelfdc40a02009-02-17 22:15:04 +00003357 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003359 }
3360 } else {
3361 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003362 result = DAG.getNode(ISD::FLOG, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003363 getValue(I.getArgOperand(0)).getValueType(),
3364 getValue(I.getArgOperand(0)));
Bill Wendling39150252008-09-09 20:39:27 +00003365 }
3366
Dale Johannesen59e577f2008-09-05 18:38:42 +00003367 setValue(&I, result);
3368}
3369
Bill Wendling3eb59402008-09-09 00:28:24 +00003370/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3371/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003372void
Dan Gohman46510a72010-04-15 01:51:59 +00003373SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003374 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003375 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003376
Gabor Greif0635f352010-06-25 09:38:13 +00003377 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003378 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003379 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003380 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003381
Bill Wendling39150252008-09-09 20:39:27 +00003382 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003383 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003384
Bill Wendling3eb59402008-09-09 00:28:24 +00003385 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003386 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003387 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003388
Bill Wendling3eb59402008-09-09 00:28:24 +00003389 // Different possible minimax approximations of significand in
3390 // floating-point for various degrees of accuracy over [1,2].
3391 if (LimitFloatPrecision <= 6) {
3392 // For floating-point precision of 6:
3393 //
3394 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3395 //
3396 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003398 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003400 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3402 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003403 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003404
Scott Michelfdc40a02009-02-17 22:15:04 +00003405 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003407 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3408 // For floating-point precision of 12:
3409 //
3410 // Log2ofMantissa =
3411 // -2.51285454f +
3412 // (4.07009056f +
3413 // (-2.12067489f +
3414 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003415 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003416 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003417 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003418 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003420 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3422 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003423 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3425 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003426 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3428 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003429 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003430
Scott Michelfdc40a02009-02-17 22:15:04 +00003431 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003433 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3434 // For floating-point precision of 18:
3435 //
3436 // Log2ofMantissa =
3437 // -3.0400495f +
3438 // (6.1129976f +
3439 // (-5.3420409f +
3440 // (3.2865683f +
3441 // (-1.2669343f +
3442 // (0.27515199f -
3443 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3444 //
3445 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003446 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003447 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003449 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3451 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3454 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003455 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3457 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003458 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3460 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003461 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3463 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003464 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003465
Scott Michelfdc40a02009-02-17 22:15:04 +00003466 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003468 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003469 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003470 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003471 result = DAG.getNode(ISD::FLOG2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003472 getValue(I.getArgOperand(0)).getValueType(),
3473 getValue(I.getArgOperand(0)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003474 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003475
Dale Johannesen59e577f2008-09-05 18:38:42 +00003476 setValue(&I, result);
3477}
3478
Bill Wendling3eb59402008-09-09 00:28:24 +00003479/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3480/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003481void
Dan Gohman46510a72010-04-15 01:51:59 +00003482SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003483 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003484 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003485
Gabor Greif0635f352010-06-25 09:38:13 +00003486 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003487 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003488 SDValue Op = getValue(I.getArgOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003489 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003490
Bill Wendling39150252008-09-09 20:39:27 +00003491 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003492 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003494 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003495
3496 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003497 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003498 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003499
3500 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003501 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003502 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003503 // Log10ofMantissa =
3504 // -0.50419619f +
3505 // (0.60948995f - 0.10380950f * x) * x;
3506 //
3507 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003509 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003510 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003511 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3513 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003514 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003515
Scott Michelfdc40a02009-02-17 22:15:04 +00003516 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003517 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003518 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3519 // For floating-point precision of 12:
3520 //
3521 // Log10ofMantissa =
3522 // -0.64831180f +
3523 // (0.91751397f +
3524 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3525 //
3526 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003528 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003530 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3532 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003533 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3535 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003536 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003537
Scott Michelfdc40a02009-02-17 22:15:04 +00003538 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003540 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003541 // For floating-point precision of 18:
3542 //
3543 // Log10ofMantissa =
3544 // -0.84299375f +
3545 // (1.5327582f +
3546 // (-1.0688956f +
3547 // (0.49102474f +
3548 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3549 //
3550 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003552 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003554 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3556 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003557 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3559 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003560 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3562 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3565 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003567
Scott Michelfdc40a02009-02-17 22:15:04 +00003568 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003570 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003571 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003572 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003573 result = DAG.getNode(ISD::FLOG10, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003574 getValue(I.getArgOperand(0)).getValueType(),
3575 getValue(I.getArgOperand(0)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003576 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003577
Dale Johannesen59e577f2008-09-05 18:38:42 +00003578 setValue(&I, result);
3579}
3580
Bill Wendlinge10c8142008-09-09 22:39:21 +00003581/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3582/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003583void
Dan Gohman46510a72010-04-15 01:51:59 +00003584SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003585 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003586 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003587
Gabor Greif0635f352010-06-25 09:38:13 +00003588 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003589 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003590 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003591
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003593
3594 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3596 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003597
3598 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003600 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003601
3602 if (LimitFloatPrecision <= 6) {
3603 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003604 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003605 // TwoToFractionalPartOfX =
3606 // 0.997535578f +
3607 // (0.735607626f + 0.252464424f * x) * x;
3608 //
3609 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003611 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003613 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3615 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003616 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003617 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
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, t6, 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 if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3624 // For floating-point precision of 12:
3625 //
3626 // TwoToFractionalPartOfX =
3627 // 0.999892986f +
3628 // (0.696457318f +
3629 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3630 //
3631 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003632 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003633 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003635 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003636 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3637 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003638 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003639 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3640 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003641 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003642 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003643 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003644 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003645
Scott Michelfdc40a02009-02-17 22:15:04 +00003646 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003648 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3649 // For floating-point precision of 18:
3650 //
3651 // TwoToFractionalPartOfX =
3652 // 0.999999982f +
3653 // (0.693148872f +
3654 // (0.240227044f +
3655 // (0.554906021e-1f +
3656 // (0.961591928e-2f +
3657 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3658 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003659 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003660 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003662 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003663 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3664 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003665 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003666 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3667 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003668 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003669 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3670 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003671 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003672 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3673 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003674 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003675 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3676 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003677 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003678 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003679 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003681
Scott Michelfdc40a02009-02-17 22:15:04 +00003682 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003684 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003685 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003686 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003687 result = DAG.getNode(ISD::FEXP2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003688 getValue(I.getArgOperand(0)).getValueType(),
3689 getValue(I.getArgOperand(0)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003690 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003691
Dale Johannesen601d3c02008-09-05 01:48:15 +00003692 setValue(&I, result);
3693}
3694
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003695/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3696/// limited-precision mode with x == 10.0f.
3697void
Dan Gohman46510a72010-04-15 01:51:59 +00003698SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003699 SDValue result;
Gabor Greif0635f352010-06-25 09:38:13 +00003700 const Value *Val = I.getArgOperand(0);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003701 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003702 bool IsExp10 = false;
3703
Owen Anderson825b72b2009-08-11 20:47:22 +00003704 if (getValue(Val).getValueType() == MVT::f32 &&
Gabor Greif0635f352010-06-25 09:38:13 +00003705 getValue(I.getArgOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003706 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3707 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3708 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3709 APFloat Ten(10.0f);
3710 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3711 }
3712 }
3713 }
3714
3715 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003716 SDValue Op = getValue(I.getArgOperand(1));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003717
3718 // Put the exponent in the right bit position for later addition to the
3719 // final result:
3720 //
3721 // #define LOG2OF10 3.3219281f
3722 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003723 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003724 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003725 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003726
3727 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003728 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3729 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003730
3731 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003733 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003734
3735 if (LimitFloatPrecision <= 6) {
3736 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003737 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003738 // twoToFractionalPartOfX =
3739 // 0.997535578f +
3740 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003741 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003742 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003743 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003744 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003745 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003746 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003747 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3748 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003749 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003750 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
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, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003753
Dale Johannesenfa42dea2009-01-30 01:34:22 +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 if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3757 // For floating-point precision of 12:
3758 //
3759 // TwoToFractionalPartOfX =
3760 // 0.999892986f +
3761 // (0.696457318f +
3762 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3763 //
3764 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003765 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003766 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003768 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003769 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3770 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003771 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003772 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3773 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003774 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003775 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003776 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003777 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003778
Scott Michelfdc40a02009-02-17 22:15:04 +00003779 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003780 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003781 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3782 // For floating-point precision of 18:
3783 //
3784 // TwoToFractionalPartOfX =
3785 // 0.999999982f +
3786 // (0.693148872f +
3787 // (0.240227044f +
3788 // (0.554906021e-1f +
3789 // (0.961591928e-2f +
3790 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3791 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003792 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003793 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003795 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3797 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003798 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3800 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003801 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3803 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003804 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3806 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003807 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003808 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3809 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003810 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003812 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003814
Scott Michelfdc40a02009-02-17 22:15:04 +00003815 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003817 }
3818 } else {
3819 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003820 result = DAG.getNode(ISD::FPOW, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003821 getValue(I.getArgOperand(0)).getValueType(),
3822 getValue(I.getArgOperand(0)),
3823 getValue(I.getArgOperand(1)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003824 }
3825
3826 setValue(&I, result);
3827}
3828
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003829
3830/// ExpandPowI - Expand a llvm.powi intrinsic.
3831static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3832 SelectionDAG &DAG) {
3833 // If RHS is a constant, we can expand this out to a multiplication tree,
3834 // otherwise we end up lowering to a call to __powidf2 (for example). When
3835 // optimizing for size, we only want to do this if the expansion would produce
3836 // a small number of multiplies, otherwise we do the full expansion.
3837 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3838 // Get the exponent as a positive value.
3839 unsigned Val = RHSC->getSExtValue();
3840 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003841
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003842 // powi(x, 0) -> 1.0
3843 if (Val == 0)
3844 return DAG.getConstantFP(1.0, LHS.getValueType());
3845
Dan Gohmanae541aa2010-04-15 04:33:49 +00003846 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003847 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3848 // If optimizing for size, don't insert too many multiplies. This
3849 // inserts up to 5 multiplies.
3850 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3851 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003852 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003853 // powi(x,15) generates one more multiply than it should), but this has
3854 // the benefit of being both really simple and much better than a libcall.
3855 SDValue Res; // Logically starts equal to 1.0
3856 SDValue CurSquare = LHS;
3857 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003858 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003859 if (Res.getNode())
3860 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3861 else
3862 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003863 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003864
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003865 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3866 CurSquare, CurSquare);
3867 Val >>= 1;
3868 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003869
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003870 // If the original was negative, invert the result, producing 1/(x*x*x).
3871 if (RHSC->getSExtValue() < 0)
3872 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3873 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3874 return Res;
3875 }
3876 }
3877
3878 // Otherwise, expand to a libcall.
3879 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3880}
3881
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003882/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
3883/// argument, create the corresponding DBG_VALUE machine instruction for it now.
3884/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003885bool
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003886SelectionDAGBuilder::EmitFuncArgumentDbgValue(const DbgValueInst &DI,
3887 const Value *V, MDNode *Variable,
Dan Gohman5d11ea32010-05-01 00:33:16 +00003888 uint64_t Offset,
3889 const SDValue &N) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003890 if (!isa<Argument>(V))
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003891 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003892
Devang Patel719f6a92010-04-29 20:40:36 +00003893 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela83ce982010-04-29 18:50:36 +00003894 // Ignore inlined function arguments here.
3895 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00003896 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00003897 return false;
3898
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003899 MachineBasicBlock *MBB = FuncInfo.MBBMap[DI.getParent()];
3900 if (MBB != &MF.front())
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003901 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003902
3903 unsigned Reg = 0;
3904 if (N.getOpcode() == ISD::CopyFromReg) {
3905 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Evan Cheng1deef272010-04-29 00:59:34 +00003906 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003907 MachineRegisterInfo &RegInfo = MF.getRegInfo();
3908 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
3909 if (PR)
3910 Reg = PR;
3911 }
3912 }
3913
Evan Chenga36acad2010-04-29 06:33:38 +00003914 if (!Reg) {
3915 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
3916 if (VMI == FuncInfo.ValueMap.end())
3917 return false;
3918 Reg = VMI->second;
3919 }
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003920
3921 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
3922 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
3923 TII->get(TargetOpcode::DBG_VALUE))
Evan Chenga36acad2010-04-29 06:33:38 +00003924 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003925 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003926 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003927}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003928
Douglas Gregor7d9663c2010-05-11 06:17:44 +00003929// VisualStudio defines setjmp as _setjmp
3930#if defined(_MSC_VER) && defined(setjmp)
3931#define setjmp_undefined_for_visual_studio
3932#undef setjmp
3933#endif
3934
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003935/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3936/// we want to emit this as a call to a named external function, return the name
3937/// otherwise lower it and return null.
3938const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003939SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003940 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003941 SDValue Res;
3942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003943 switch (Intrinsic) {
3944 default:
3945 // By default, turn this into a target intrinsic node.
3946 visitTargetIntrinsic(I, Intrinsic);
3947 return 0;
3948 case Intrinsic::vastart: visitVAStart(I); return 0;
3949 case Intrinsic::vaend: visitVAEnd(I); return 0;
3950 case Intrinsic::vacopy: visitVACopy(I); return 0;
3951 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003952 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00003953 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003954 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003955 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003956 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00003957 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003958 return 0;
3959 case Intrinsic::setjmp:
3960 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003961 case Intrinsic::longjmp:
3962 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003963 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003964 // Assert for address < 256 since we support only user defined address
3965 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00003966 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003967 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00003968 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003969 < 256 &&
3970 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00003971 SDValue Op1 = getValue(I.getArgOperand(0));
3972 SDValue Op2 = getValue(I.getArgOperand(1));
3973 SDValue Op3 = getValue(I.getArgOperand(2));
3974 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
3975 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003976 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Gabor Greif0635f352010-06-25 09:38:13 +00003977 I.getArgOperand(0), 0, I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003978 return 0;
3979 }
Chris Lattner824b9582008-11-21 16:42:48 +00003980 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003981 // Assert for address < 256 since we support only user defined address
3982 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00003983 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003984 < 256 &&
3985 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00003986 SDValue Op1 = getValue(I.getArgOperand(0));
3987 SDValue Op2 = getValue(I.getArgOperand(1));
3988 SDValue Op3 = getValue(I.getArgOperand(2));
3989 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
3990 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003991 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Gabor Greif0635f352010-06-25 09:38:13 +00003992 I.getArgOperand(0), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003993 return 0;
3994 }
Chris Lattner824b9582008-11-21 16:42:48 +00003995 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003996 // Assert for address < 256 since we support only user defined address
3997 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00003998 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003999 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004000 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004001 < 256 &&
4002 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004003 SDValue Op1 = getValue(I.getArgOperand(0));
4004 SDValue Op2 = getValue(I.getArgOperand(1));
4005 SDValue Op3 = getValue(I.getArgOperand(2));
4006 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4007 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004008
4009 // If the source and destination are known to not be aliases, we can
4010 // lower memmove as memcpy.
4011 uint64_t Size = -1ULL;
4012 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004013 Size = C->getZExtValue();
Gabor Greif0635f352010-06-25 09:38:13 +00004014 if (AA->alias(I.getArgOperand(0), Size, I.getArgOperand(1), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004015 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004016 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Gabor Greif0635f352010-06-25 09:38:13 +00004017 false, I.getArgOperand(0), 0, I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004018 return 0;
4019 }
4020
Mon P Wang20adc9d2010-04-04 03:10:48 +00004021 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Gabor Greif0635f352010-06-25 09:38:13 +00004022 I.getArgOperand(0), 0, I.getArgOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004023 return 0;
4024 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004025 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004026 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004027 if (!DIVariable(DI.getVariable()).Verify())
Devang Patel7e1e31f2009-07-02 22:43:26 +00004028 return 0;
4029
Devang Patelac1ceb32009-10-09 22:42:28 +00004030 MDNode *Variable = DI.getVariable();
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004031 // Parameters are handled specially.
Devang Patelf38c6c82010-04-28 23:24:13 +00004032 bool isParameter =
4033 DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable;
Dan Gohman46510a72010-04-15 01:51:59 +00004034 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004035 if (!Address)
4036 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00004037 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00004038 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004039 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004040 if (AI) {
4041 // Don't handle byval arguments or VLAs, for example.
4042 // Non-byval arguments are handled here (they refer to the stack temporary
4043 // alloca at this point).
4044 DenseMap<const AllocaInst*, int>::iterator SI =
4045 FuncInfo.StaticAllocaMap.find(AI);
4046 if (SI == FuncInfo.StaticAllocaMap.end())
4047 return 0; // VLAs.
4048 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004049
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004050 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4051 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4052 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
4053 }
4054
4055 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4056 // but do not always have a corresponding SDNode built. The SDNodeOrder
4057 // absolute, but not relative, values are different depending on whether
4058 // debug info exists.
4059 ++SDNodeOrder;
4060 SDValue &N = NodeMap[Address];
4061 SDDbgValue *SDV;
4062 if (N.getNode()) {
4063 if (isParameter && !AI) {
4064 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4065 if (FINode)
4066 // Byval parameter. We have a frame index at this point.
4067 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4068 0, dl, SDNodeOrder);
4069 else
4070 // Can't do anything with other non-AI cases yet. This might be a
4071 // parameter of a callee function that got inlined, for example.
4072 return 0;
4073 } else if (AI)
4074 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4075 0, dl, SDNodeOrder);
4076 else
4077 // Can't do anything with other non-AI cases yet.
4078 return 0;
4079 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4080 } else {
4081 // This isn't useful, but it shows what we're missing.
4082 SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()),
4083 0, dl, SDNodeOrder);
4084 DAG.AddDbgValue(SDV, 0, isParameter);
4085 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004086 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004087 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004088 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004089 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004090 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004091 return 0;
4092
4093 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004094 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004095 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004096 if (!V)
4097 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004098
4099 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4100 // but do not always have a corresponding SDNode built. The SDNodeOrder
4101 // absolute, but not relative, values are different depending on whether
4102 // debug info exists.
4103 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004104 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00004105 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004106 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4107 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004108 } else {
Devang Pateld47f3c82010-05-05 22:29:00 +00004109 bool createUndef = false;
4110 // FIXME : Why not use getValue() directly ?
Devang Patel9126c0d2010-06-01 19:59:01 +00004111 SDValue N = NodeMap[V];
4112 if (!N.getNode() && isa<Argument>(V))
4113 // Check unused arguments map.
4114 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004115 if (N.getNode()) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004116 if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) {
4117 SDV = DAG.getDbgValue(Variable, N.getNode(),
4118 N.getResNo(), Offset, dl, SDNodeOrder);
4119 DAG.AddDbgValue(SDV, N.getNode(), false);
4120 }
Devang Pateld47f3c82010-05-05 22:29:00 +00004121 } else if (isa<PHINode>(V) && !V->use_empty()) {
4122 SDValue N = getValue(V);
4123 if (N.getNode()) {
4124 if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) {
4125 SDV = DAG.getDbgValue(Variable, N.getNode(),
4126 N.getResNo(), Offset, dl, SDNodeOrder);
4127 DAG.AddDbgValue(SDV, N.getNode(), false);
4128 }
4129 } else
4130 createUndef = true;
4131 } else
4132 createUndef = true;
4133 if (createUndef) {
Devang Patel00190342010-03-15 19:15:44 +00004134 // We may expand this to cover more cases. One case where we have no
4135 // data available is an unreferenced parameter; we need this fallback.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004136 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
4137 Offset, dl, SDNodeOrder);
4138 DAG.AddDbgValue(SDV, 0, false);
4139 }
Devang Patel00190342010-03-15 19:15:44 +00004140 }
4141
4142 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004143 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004144 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004145 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004146 // Don't handle byval struct arguments or VLAs, for example.
4147 if (!AI)
4148 return 0;
4149 DenseMap<const AllocaInst*, int>::iterator SI =
4150 FuncInfo.StaticAllocaMap.find(AI);
4151 if (SI == FuncInfo.StaticAllocaMap.end())
4152 return 0; // VLAs.
4153 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00004154
Chris Lattner512063d2010-04-05 06:19:28 +00004155 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4156 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4157 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004158 return 0;
4159 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004160 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004161 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00004162 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
4163 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004165 SDValue Ops[1];
4166 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004167 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004168 setValue(&I, Op);
4169 DAG.setRoot(Op.getValue(1));
4170 return 0;
4171 }
4172
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004173 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00004174 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00004175 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00004176 if (CallMBB->isLandingPad())
4177 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004178 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004179#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004180 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004181#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004182 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4183 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00004184 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004185 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004186
Chris Lattner3a5815f2009-09-17 23:54:54 +00004187 // Insert the EHSELECTION instruction.
4188 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4189 SDValue Ops[2];
Gabor Greif0635f352010-06-25 09:38:13 +00004190 Ops[0] = getValue(I.getArgOperand(0));
Chris Lattner3a5815f2009-09-17 23:54:54 +00004191 Ops[1] = getRoot();
4192 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004193 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004194 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004195 return 0;
4196 }
4197
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004198 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004199 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004200 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004201 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4202 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004203 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004204 return 0;
4205 }
4206
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004207 case Intrinsic::eh_return_i32:
4208 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004209 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
4210 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
4211 MVT::Other,
4212 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004213 getValue(I.getArgOperand(0)),
4214 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004215 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004216 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004217 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004218 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004219 case Intrinsic::eh_dwarf_cfa: {
Gabor Greif0635f352010-06-25 09:38:13 +00004220 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004221 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004222 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004223 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004224 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004225 TLI.getPointerTy()),
4226 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004227 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004228 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004229 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00004230 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4231 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004232 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004233 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004234 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004235 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004236 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004237 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004238 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004239
Chris Lattner512063d2010-04-05 06:19:28 +00004240 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004241 return 0;
4242 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004243 case Intrinsic::eh_sjlj_setjmp: {
4244 setValue(&I, DAG.getNode(ISD::EH_SJLJ_SETJMP, dl, MVT::i32, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004245 getValue(I.getArgOperand(0))));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004246 return 0;
4247 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004248 case Intrinsic::eh_sjlj_longjmp: {
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004249 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, dl, MVT::Other,
4250 getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004251 getValue(I.getArgOperand(0))));
Jim Grosbach5eb19512010-05-22 01:06:18 +00004252 return 0;
4253 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004254
Mon P Wang77cdf302008-11-10 20:54:11 +00004255 case Intrinsic::convertff:
4256 case Intrinsic::convertfsi:
4257 case Intrinsic::convertfui:
4258 case Intrinsic::convertsif:
4259 case Intrinsic::convertuif:
4260 case Intrinsic::convertss:
4261 case Intrinsic::convertsu:
4262 case Intrinsic::convertus:
4263 case Intrinsic::convertuu: {
4264 ISD::CvtCode Code = ISD::CVT_INVALID;
4265 switch (Intrinsic) {
4266 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4267 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4268 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4269 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4270 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4271 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4272 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4273 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4274 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4275 }
Owen Andersone50ed302009-08-10 22:56:29 +00004276 EVT DestVT = TLI.getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004277 const Value *Op1 = I.getArgOperand(0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004278 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4279 DAG.getValueType(DestVT),
4280 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004281 getValue(I.getArgOperand(1)),
4282 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004283 Code);
4284 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004285 return 0;
4286 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004287 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004288 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004289 getValue(I.getArgOperand(0)).getValueType(),
4290 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004291 return 0;
4292 case Intrinsic::powi:
Gabor Greif0635f352010-06-25 09:38:13 +00004293 setValue(&I, ExpandPowI(dl, getValue(I.getArgOperand(0)),
4294 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004295 return 0;
4296 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004297 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004298 getValue(I.getArgOperand(0)).getValueType(),
4299 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004300 return 0;
4301 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004302 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004303 getValue(I.getArgOperand(0)).getValueType(),
4304 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004305 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004306 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004307 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004308 return 0;
4309 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004310 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004311 return 0;
4312 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004313 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004314 return 0;
4315 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004316 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004317 return 0;
4318 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004319 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004320 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004321 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004322 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004323 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004324 case Intrinsic::convert_to_fp16:
4325 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004326 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004327 return 0;
4328 case Intrinsic::convert_from_fp16:
4329 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004330 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004331 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004332 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004333 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004334 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004335 return 0;
4336 }
4337 case Intrinsic::readcyclecounter: {
4338 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004339 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4340 DAG.getVTList(MVT::i64, MVT::Other),
4341 &Op, 1);
4342 setValue(&I, Res);
4343 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004344 return 0;
4345 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004346 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004347 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004348 getValue(I.getArgOperand(0)).getValueType(),
4349 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004350 return 0;
4351 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004352 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004353 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004354 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004355 return 0;
4356 }
4357 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004358 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004359 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004360 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004361 return 0;
4362 }
4363 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00004364 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004365 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004366 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004367 return 0;
4368 }
4369 case Intrinsic::stacksave: {
4370 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004371 Res = DAG.getNode(ISD::STACKSAVE, dl,
4372 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4373 setValue(&I, Res);
4374 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004375 return 0;
4376 }
4377 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00004378 Res = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004379 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004380 return 0;
4381 }
Bill Wendling57344502008-11-18 11:01:33 +00004382 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004383 // Emit code into the DAG to store the stack guard onto the stack.
4384 MachineFunction &MF = DAG.getMachineFunction();
4385 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004386 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004387
Gabor Greif0635f352010-06-25 09:38:13 +00004388 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
4389 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004390
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004391 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004392 MFI->setStackProtectorIndex(FI);
4393
4394 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4395
4396 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004397 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4398 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004399 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004400 setValue(&I, Res);
4401 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004402 return 0;
4403 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004404 case Intrinsic::objectsize: {
4405 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00004406 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004407
4408 assert(CI && "Non-constant type in __builtin_object_size?");
4409
Gabor Greif0635f352010-06-25 09:38:13 +00004410 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004411 EVT Ty = Arg.getValueType();
4412
Dan Gohmane368b462010-06-18 14:22:04 +00004413 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004414 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004415 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004416 Res = DAG.getConstant(0, Ty);
4417
4418 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004419 return 0;
4420 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004421 case Intrinsic::var_annotation:
4422 // Discard annotate attributes
4423 return 0;
4424
4425 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00004426 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004427
4428 SDValue Ops[6];
4429 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004430 Ops[1] = getValue(I.getArgOperand(0));
4431 Ops[2] = getValue(I.getArgOperand(1));
4432 Ops[3] = getValue(I.getArgOperand(2));
4433 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004434 Ops[5] = DAG.getSrcValue(F);
4435
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004436 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4437 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4438 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004439
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004440 setValue(&I, Res);
4441 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004442 return 0;
4443 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004444 case Intrinsic::gcroot:
4445 if (GFI) {
Gabor Greif0635f352010-06-25 09:38:13 +00004446 const Value *Alloca = I.getArgOperand(0);
4447 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004448
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004449 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4450 GFI->addStackRoot(FI->getIndex(), TypeMap);
4451 }
4452 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004453 case Intrinsic::gcread:
4454 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004455 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004456 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004457 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004458 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004459 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004460 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004461 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004462 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004463 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004464 return implVisitAluOverflow(I, ISD::UADDO);
4465 case Intrinsic::sadd_with_overflow:
4466 return implVisitAluOverflow(I, ISD::SADDO);
4467 case Intrinsic::usub_with_overflow:
4468 return implVisitAluOverflow(I, ISD::USUBO);
4469 case Intrinsic::ssub_with_overflow:
4470 return implVisitAluOverflow(I, ISD::SSUBO);
4471 case Intrinsic::umul_with_overflow:
4472 return implVisitAluOverflow(I, ISD::UMULO);
4473 case Intrinsic::smul_with_overflow:
4474 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004475
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004476 case Intrinsic::prefetch: {
4477 SDValue Ops[4];
4478 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004479 Ops[1] = getValue(I.getArgOperand(0));
4480 Ops[2] = getValue(I.getArgOperand(1));
4481 Ops[3] = getValue(I.getArgOperand(2));
Bill Wendling4533cac2010-01-28 21:51:40 +00004482 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004483 return 0;
4484 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004485
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004486 case Intrinsic::memory_barrier: {
4487 SDValue Ops[6];
4488 Ops[0] = getRoot();
4489 for (int x = 1; x < 6; ++x)
Gabor Greif0635f352010-06-25 09:38:13 +00004490 Ops[x] = getValue(I.getArgOperand(x - 1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004491
Bill Wendling4533cac2010-01-28 21:51:40 +00004492 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004493 return 0;
4494 }
4495 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004496 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004497 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004498 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00004499 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004500 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00004501 getValue(I.getArgOperand(0)),
4502 getValue(I.getArgOperand(1)),
4503 getValue(I.getArgOperand(2)),
4504 I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004505 setValue(&I, L);
4506 DAG.setRoot(L.getValue(1));
4507 return 0;
4508 }
4509 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004510 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004511 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004512 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004513 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004514 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004515 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004516 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004517 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004518 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004519 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004520 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004521 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004522 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004523 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004524 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004525 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004526 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004527 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004528 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004529 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004530 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004531
4532 case Intrinsic::invariant_start:
4533 case Intrinsic::lifetime_start:
4534 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004535 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004536 return 0;
4537 case Intrinsic::invariant_end:
4538 case Intrinsic::lifetime_end:
4539 // Discard region information.
4540 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004541 }
4542}
4543
Dan Gohman46510a72010-04-15 01:51:59 +00004544void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004545 bool isTailCall,
4546 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004547 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4548 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004549 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004550 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004551 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004552
4553 TargetLowering::ArgListTy Args;
4554 TargetLowering::ArgListEntry Entry;
4555 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004556
4557 // Check whether the function can return without sret-demotion.
4558 SmallVector<EVT, 4> OutVTs;
4559 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4560 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004561 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004562 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004563
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004564 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004565 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4566
4567 SDValue DemoteStackSlot;
4568
4569 if (!CanLowerReturn) {
4570 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4571 FTy->getReturnType());
4572 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4573 FTy->getReturnType());
4574 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004575 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004576 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4577
4578 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4579 Entry.Node = DemoteStackSlot;
4580 Entry.Ty = StackSlotPtrType;
4581 Entry.isSExt = false;
4582 Entry.isZExt = false;
4583 Entry.isInReg = false;
4584 Entry.isSRet = true;
4585 Entry.isNest = false;
4586 Entry.isByVal = false;
4587 Entry.Alignment = Align;
4588 Args.push_back(Entry);
4589 RetTy = Type::getVoidTy(FTy->getContext());
4590 }
4591
Dan Gohman46510a72010-04-15 01:51:59 +00004592 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004593 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004594 SDValue ArgNode = getValue(*i);
4595 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4596
4597 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004598 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4599 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4600 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4601 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4602 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4603 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004604 Entry.Alignment = CS.getParamAlignment(attrInd);
4605 Args.push_back(Entry);
4606 }
4607
Chris Lattner512063d2010-04-05 06:19:28 +00004608 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004609 // Insert a label before the invoke call to mark the try range. This can be
4610 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004611 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004612
Jim Grosbachca752c92010-01-28 01:45:32 +00004613 // For SjLj, keep track of which landing pads go with which invokes
4614 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004615 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004616 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004617 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004618 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004619 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004620 }
4621
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004622 // Both PendingLoads and PendingExports must be flushed here;
4623 // this call might not return.
4624 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004625 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004626 }
4627
Dan Gohman98ca4f22009-08-05 01:29:28 +00004628 // Check if target-independent constraints permit a tail call here.
4629 // Target-dependent constraints are checked within TLI.LowerCallTo.
4630 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004631 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004632 isTailCall = false;
4633
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004634 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004635 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004636 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004637 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004638 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004639 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004640 isTailCall,
4641 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004642 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004643 assert((isTailCall || Result.second.getNode()) &&
4644 "Non-null chain expected with non-tail call!");
4645 assert((Result.second.getNode() || !Result.first.getNode()) &&
4646 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004647 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004648 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004649 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004650 // The instruction result is the result of loading from the
4651 // hidden sret parameter.
4652 SmallVector<EVT, 1> PVTs;
4653 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4654
4655 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4656 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4657 EVT PtrVT = PVTs[0];
4658 unsigned NumValues = OutVTs.size();
4659 SmallVector<SDValue, 4> Values(NumValues);
4660 SmallVector<SDValue, 4> Chains(NumValues);
4661
4662 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004663 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4664 DemoteStackSlot,
4665 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004666 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004667 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004668 Values[i] = L;
4669 Chains[i] = L.getValue(1);
4670 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004671
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004672 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4673 MVT::Other, &Chains[0], NumValues);
4674 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004675
4676 // Collect the legal value parts into potentially illegal values
4677 // that correspond to the original function's return values.
4678 SmallVector<EVT, 4> RetTys;
4679 RetTy = FTy->getReturnType();
4680 ComputeValueVTs(TLI, RetTy, RetTys);
4681 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4682 SmallVector<SDValue, 4> ReturnValues;
4683 unsigned CurReg = 0;
4684 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4685 EVT VT = RetTys[I];
4686 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4687 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4688
4689 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004690 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004691 RegisterVT, VT, AssertOp);
4692 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004693 CurReg += NumRegs;
4694 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004695
Bill Wendling4533cac2010-01-28 21:51:40 +00004696 setValue(CS.getInstruction(),
4697 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4698 DAG.getVTList(&RetTys[0], RetTys.size()),
4699 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004700
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004701 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004702
4703 // As a special case, a null chain means that a tail call has been emitted and
4704 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004705 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004706 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004707 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004708 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004709
Chris Lattner512063d2010-04-05 06:19:28 +00004710 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004711 // Insert a label at the end of the invoke call to mark the try range. This
4712 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004713 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004714 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004715
4716 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004717 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004718 }
4719}
4720
Chris Lattner8047d9a2009-12-24 00:37:38 +00004721/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4722/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004723static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4724 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004725 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004726 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004727 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004728 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004729 if (C->isNullValue())
4730 continue;
4731 // Unknown instruction.
4732 return false;
4733 }
4734 return true;
4735}
4736
Dan Gohman46510a72010-04-15 01:51:59 +00004737static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4738 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004739 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004740
Chris Lattner8047d9a2009-12-24 00:37:38 +00004741 // Check to see if this load can be trivially constant folded, e.g. if the
4742 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004743 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004744 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004745 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004746 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004747
Dan Gohman46510a72010-04-15 01:51:59 +00004748 if (const Constant *LoadCst =
4749 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4750 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004751 return Builder.getValue(LoadCst);
4752 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004753
Chris Lattner8047d9a2009-12-24 00:37:38 +00004754 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4755 // still constant memory, the input chain can be the entry node.
4756 SDValue Root;
4757 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004758
Chris Lattner8047d9a2009-12-24 00:37:38 +00004759 // Do not serialize (non-volatile) loads of constant memory with anything.
4760 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4761 Root = Builder.DAG.getEntryNode();
4762 ConstantMemory = true;
4763 } else {
4764 // Do not serialize non-volatile loads against each other.
4765 Root = Builder.DAG.getRoot();
4766 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004767
Chris Lattner8047d9a2009-12-24 00:37:38 +00004768 SDValue Ptr = Builder.getValue(PtrVal);
4769 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4770 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004771 false /*volatile*/,
4772 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004773
Chris Lattner8047d9a2009-12-24 00:37:38 +00004774 if (!ConstantMemory)
4775 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4776 return LoadVal;
4777}
4778
4779
4780/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4781/// If so, return true and lower it, otherwise return false and it will be
4782/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004783bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004784 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00004785 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00004786 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004787
Gabor Greif0635f352010-06-25 09:38:13 +00004788 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00004789 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00004790 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004791 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004792 return false;
4793
Gabor Greif0635f352010-06-25 09:38:13 +00004794 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004795
Chris Lattner8047d9a2009-12-24 00:37:38 +00004796 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4797 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004798 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4799 bool ActuallyDoIt = true;
4800 MVT LoadVT;
4801 const Type *LoadTy;
4802 switch (Size->getZExtValue()) {
4803 default:
4804 LoadVT = MVT::Other;
4805 LoadTy = 0;
4806 ActuallyDoIt = false;
4807 break;
4808 case 2:
4809 LoadVT = MVT::i16;
4810 LoadTy = Type::getInt16Ty(Size->getContext());
4811 break;
4812 case 4:
4813 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004814 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004815 break;
4816 case 8:
4817 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004818 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004819 break;
4820 /*
4821 case 16:
4822 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004823 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004824 LoadTy = VectorType::get(LoadTy, 4);
4825 break;
4826 */
4827 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004828
Chris Lattner04b091a2009-12-24 01:07:17 +00004829 // This turns into unaligned loads. We only do this if the target natively
4830 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4831 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004832
Chris Lattner04b091a2009-12-24 01:07:17 +00004833 // Require that we can find a legal MVT, and only do this if the target
4834 // supports unaligned loads of that type. Expanding into byte loads would
4835 // bloat the code.
4836 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4837 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4838 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4839 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4840 ActuallyDoIt = false;
4841 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004842
Chris Lattner04b091a2009-12-24 01:07:17 +00004843 if (ActuallyDoIt) {
4844 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4845 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004846
Chris Lattner04b091a2009-12-24 01:07:17 +00004847 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4848 ISD::SETNE);
4849 EVT CallVT = TLI.getValueType(I.getType(), true);
4850 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4851 return true;
4852 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004853 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004854
4855
Chris Lattner8047d9a2009-12-24 00:37:38 +00004856 return false;
4857}
4858
4859
Dan Gohman46510a72010-04-15 01:51:59 +00004860void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004861 const char *RenameFn = 0;
4862 if (Function *F = I.getCalledFunction()) {
4863 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004864 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004865 if (II) {
4866 if (unsigned IID = II->getIntrinsicID(F)) {
4867 RenameFn = visitIntrinsicCall(I, IID);
4868 if (!RenameFn)
4869 return;
4870 }
4871 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004872 if (unsigned IID = F->getIntrinsicID()) {
4873 RenameFn = visitIntrinsicCall(I, IID);
4874 if (!RenameFn)
4875 return;
4876 }
4877 }
4878
4879 // Check for well-known libc/libm calls. If the function is internal, it
4880 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004881 if (!F->hasLocalLinkage() && F->hasName()) {
4882 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004883 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004884 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004885 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4886 I.getType() == I.getArgOperand(0)->getType() &&
4887 I.getType() == I.getArgOperand(1)->getType()) {
4888 SDValue LHS = getValue(I.getArgOperand(0));
4889 SDValue RHS = getValue(I.getArgOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004890 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4891 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004892 return;
4893 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004894 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004895 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004896 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4897 I.getType() == I.getArgOperand(0)->getType()) {
4898 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004899 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4900 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004901 return;
4902 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004903 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004904 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004905 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4906 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004907 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004908 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004909 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4910 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004911 return;
4912 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004913 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004914 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004915 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4916 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004917 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004918 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004919 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4920 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004921 return;
4922 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004923 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
Gabor Greif37387d52010-06-30 12:55:46 +00004924 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00004925 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
4926 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004927 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00004928 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00004929 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4930 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004931 return;
4932 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004933 } else if (Name == "memcmp") {
4934 if (visitMemCmpCall(I))
4935 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004936 }
4937 }
Gabor Greif0635f352010-06-25 09:38:13 +00004938 } else if (isa<InlineAsm>(I.getCalledValue())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004939 visitInlineAsm(&I);
4940 return;
4941 }
4942
4943 SDValue Callee;
4944 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00004945 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004946 else
Bill Wendling056292f2008-09-16 21:48:12 +00004947 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004948
Bill Wendling0d580132009-12-23 01:28:19 +00004949 // Check if we can potentially perform a tail call. More detailed checking is
4950 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004951 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004952}
4953
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004954namespace llvm {
Dan Gohman462f6b52010-05-29 17:53:24 +00004955
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004956/// AsmOperandInfo - This contains information for each constraint that we are
4957/// lowering.
Duncan Sands16d8f8b2010-05-11 20:16:09 +00004958class LLVM_LIBRARY_VISIBILITY SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004959 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004960public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004961 /// CallOperand - If this is the result output operand or a clobber
4962 /// this is null, otherwise it is the incoming operand to the CallInst.
4963 /// This gets modified as the asm is processed.
4964 SDValue CallOperand;
4965
4966 /// AssignedRegs - If this is a register or register class operand, this
4967 /// contains the set of register corresponding to the operand.
4968 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004969
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004970 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4971 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4972 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004973
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004974 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4975 /// busy in OutputRegs/InputRegs.
4976 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004977 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004978 std::set<unsigned> &InputRegs,
4979 const TargetRegisterInfo &TRI) const {
4980 if (isOutReg) {
4981 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4982 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4983 }
4984 if (isInReg) {
4985 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4986 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4987 }
4988 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004989
Owen Andersone50ed302009-08-10 22:56:29 +00004990 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004991 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004992 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004993 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004994 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004995 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004996 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004997
Chris Lattner81249c92008-10-17 17:05:25 +00004998 if (isa<BasicBlock>(CallOperandVal))
4999 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005000
Chris Lattner81249c92008-10-17 17:05:25 +00005001 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005002
Chris Lattner81249c92008-10-17 17:05:25 +00005003 // If this is an indirect operand, the operand is a pointer to the
5004 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005005 if (isIndirect) {
5006 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5007 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005008 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005009 OpTy = PtrTy->getElementType();
5010 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005011
Chris Lattner81249c92008-10-17 17:05:25 +00005012 // If OpTy is not a single value, it may be a struct/union that we
5013 // can tile with integers.
5014 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5015 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5016 switch (BitSize) {
5017 default: break;
5018 case 1:
5019 case 8:
5020 case 16:
5021 case 32:
5022 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005023 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005024 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005025 break;
5026 }
5027 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005028
Chris Lattner81249c92008-10-17 17:05:25 +00005029 return TLI.getValueType(OpTy, true);
5030 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005031
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005032private:
5033 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5034 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005035 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005036 const TargetRegisterInfo &TRI) {
5037 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5038 Regs.insert(Reg);
5039 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5040 for (; *Aliases; ++Aliases)
5041 Regs.insert(*Aliases);
5042 }
5043};
Dan Gohman462f6b52010-05-29 17:53:24 +00005044
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005045} // end llvm namespace.
5046
Dan Gohman462f6b52010-05-29 17:53:24 +00005047/// isAllocatableRegister - If the specified register is safe to allocate,
5048/// i.e. it isn't a stack pointer or some other special register, return the
5049/// register class for the register. Otherwise, return null.
5050static const TargetRegisterClass *
5051isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5052 const TargetLowering &TLI,
5053 const TargetRegisterInfo *TRI) {
5054 EVT FoundVT = MVT::Other;
5055 const TargetRegisterClass *FoundRC = 0;
5056 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5057 E = TRI->regclass_end(); RCI != E; ++RCI) {
5058 EVT ThisVT = MVT::Other;
5059
5060 const TargetRegisterClass *RC = *RCI;
5061 // If none of the value types for this register class are valid, we
5062 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5063 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5064 I != E; ++I) {
5065 if (TLI.isTypeLegal(*I)) {
5066 // If we have already found this register in a different register class,
5067 // choose the one with the largest VT specified. For example, on
5068 // PowerPC, we favor f64 register classes over f32.
5069 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
5070 ThisVT = *I;
5071 break;
5072 }
5073 }
5074 }
5075
5076 if (ThisVT == MVT::Other) continue;
5077
5078 // NOTE: This isn't ideal. In particular, this might allocate the
5079 // frame pointer in functions that need it (due to them not being taken
5080 // out of allocation, because a variable sized allocation hasn't been seen
5081 // yet). This is a slight code pessimization, but should still work.
5082 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5083 E = RC->allocation_order_end(MF); I != E; ++I)
5084 if (*I == Reg) {
5085 // We found a matching register class. Keep looking at others in case
5086 // we find one with larger registers that this physreg is also in.
5087 FoundRC = RC;
5088 FoundVT = ThisVT;
5089 break;
5090 }
5091 }
5092 return FoundRC;
5093}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005094
5095/// GetRegistersForValue - Assign registers (virtual or physical) for the
5096/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005097/// register allocator to handle the assignment process. However, if the asm
5098/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005099/// allocation. This produces generally horrible, but correct, code.
5100///
5101/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005102/// Input and OutputRegs are the set of already allocated physical registers.
5103///
Dan Gohman2048b852009-11-23 18:04:58 +00005104void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005105GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005106 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005107 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005108 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005110 // Compute whether this value requires an input register, an output register,
5111 // or both.
5112 bool isOutReg = false;
5113 bool isInReg = false;
5114 switch (OpInfo.Type) {
5115 case InlineAsm::isOutput:
5116 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005117
5118 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005119 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005120 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005121 break;
5122 case InlineAsm::isInput:
5123 isInReg = true;
5124 isOutReg = false;
5125 break;
5126 case InlineAsm::isClobber:
5127 isOutReg = true;
5128 isInReg = true;
5129 break;
5130 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005131
5132
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005133 MachineFunction &MF = DAG.getMachineFunction();
5134 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005135
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005136 // If this is a constraint for a single physreg, or a constraint for a
5137 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005138 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005139 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5140 OpInfo.ConstraintVT);
5141
5142 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005144 // If this is a FP input in an integer register (or visa versa) insert a bit
5145 // cast of the input value. More generally, handle any case where the input
5146 // value disagrees with the register class we plan to stick this in.
5147 if (OpInfo.Type == InlineAsm::isInput &&
5148 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005149 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005150 // types are identical size, use a bitcast to convert (e.g. two differing
5151 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005152 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005153 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005154 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005155 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005156 OpInfo.ConstraintVT = RegVT;
5157 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5158 // If the input is a FP value and we want it in FP registers, do a
5159 // bitcast to the corresponding integer type. This turns an f64 value
5160 // into i64, which can be passed with two i32 values on a 32-bit
5161 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005162 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005163 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005164 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005165 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005166 OpInfo.ConstraintVT = RegVT;
5167 }
5168 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005169
Owen Anderson23b9b192009-08-12 00:36:31 +00005170 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005171 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005172
Owen Andersone50ed302009-08-10 22:56:29 +00005173 EVT RegVT;
5174 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005175
5176 // If this is a constraint for a specific physical register, like {r17},
5177 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005178 if (unsigned AssignedReg = PhysReg.first) {
5179 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005180 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005181 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005182
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005183 // Get the actual register value type. This is important, because the user
5184 // may have asked for (e.g.) the AX register in i32 type. We need to
5185 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005186 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005187
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005188 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005189 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005190
5191 // If this is an expanded reference, add the rest of the regs to Regs.
5192 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005193 TargetRegisterClass::iterator I = RC->begin();
5194 for (; *I != AssignedReg; ++I)
5195 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005196
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005197 // Already added the first reg.
5198 --NumRegs; ++I;
5199 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005200 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005201 Regs.push_back(*I);
5202 }
5203 }
Bill Wendling651ad132009-12-22 01:25:10 +00005204
Dan Gohman7451d3e2010-05-29 17:03:36 +00005205 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005206 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5207 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5208 return;
5209 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005211 // Otherwise, if this was a reference to an LLVM register class, create vregs
5212 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005213 if (const TargetRegisterClass *RC = PhysReg.second) {
5214 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005215 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005216 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005217
Evan Chengfb112882009-03-23 08:01:15 +00005218 // Create the appropriate number of virtual registers.
5219 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5220 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005221 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005222
Dan Gohman7451d3e2010-05-29 17:03:36 +00005223 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005224 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005225 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005226
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005227 // This is a reference to a register class that doesn't directly correspond
5228 // to an LLVM register class. Allocate NumRegs consecutive, available,
5229 // registers from the class.
5230 std::vector<unsigned> RegClassRegs
5231 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5232 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005233
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005234 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5235 unsigned NumAllocated = 0;
5236 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5237 unsigned Reg = RegClassRegs[i];
5238 // See if this register is available.
5239 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5240 (isInReg && InputRegs.count(Reg))) { // Already used.
5241 // Make sure we find consecutive registers.
5242 NumAllocated = 0;
5243 continue;
5244 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005245
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005246 // Check to see if this register is allocatable (i.e. don't give out the
5247 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005248 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5249 if (!RC) { // Couldn't allocate this register.
5250 // Reset NumAllocated to make sure we return consecutive registers.
5251 NumAllocated = 0;
5252 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005253 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005254
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005255 // Okay, this register is good, we can use it.
5256 ++NumAllocated;
5257
5258 // If we allocated enough consecutive registers, succeed.
5259 if (NumAllocated == NumRegs) {
5260 unsigned RegStart = (i-NumAllocated)+1;
5261 unsigned RegEnd = i+1;
5262 // Mark all of the allocated registers used.
5263 for (unsigned i = RegStart; i != RegEnd; ++i)
5264 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005265
Dan Gohman7451d3e2010-05-29 17:03:36 +00005266 OpInfo.AssignedRegs = RegsForValue(Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005267 OpInfo.ConstraintVT);
5268 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5269 return;
5270 }
5271 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005272
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005273 // Otherwise, we couldn't allocate enough registers for this.
5274}
5275
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005276/// visitInlineAsm - Handle a call to an InlineAsm object.
5277///
Dan Gohman46510a72010-04-15 01:51:59 +00005278void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5279 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005280
5281 /// ConstraintOperands - Information about all of the constraints.
5282 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005283
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005284 std::set<unsigned> OutputRegs, InputRegs;
5285
5286 // Do a prepass over the constraints, canonicalizing them, and building up the
5287 // ConstraintOperands list.
5288 std::vector<InlineAsm::ConstraintInfo>
5289 ConstraintInfos = IA->ParseConstraints();
5290
Evan Chengda43bcf2008-09-24 00:05:32 +00005291 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005292
Chris Lattner6c147292009-04-30 00:48:50 +00005293 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005294
Chris Lattner6c147292009-04-30 00:48:50 +00005295 // We won't need to flush pending loads if this asm doesn't touch
5296 // memory and is nonvolatile.
5297 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005298 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005299 else
5300 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005301
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005302 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5303 unsigned ResNo = 0; // ResNo - The result number of the next output.
5304 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5305 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5306 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005307
Owen Anderson825b72b2009-08-11 20:47:22 +00005308 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005309
5310 // Compute the value type for each operand.
5311 switch (OpInfo.Type) {
5312 case InlineAsm::isOutput:
5313 // Indirect outputs just consume an argument.
5314 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005315 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005316 break;
5317 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005319 // The return value of the call is this value. As such, there is no
5320 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005321 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005322 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005323 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5324 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5325 } else {
5326 assert(ResNo == 0 && "Asm only has one result!");
5327 OpVT = TLI.getValueType(CS.getType());
5328 }
5329 ++ResNo;
5330 break;
5331 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005332 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005333 break;
5334 case InlineAsm::isClobber:
5335 // Nothing to do.
5336 break;
5337 }
5338
5339 // If this is an input or an indirect output, process the call argument.
5340 // BasicBlocks are labels, currently appearing only in asm's.
5341 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005342 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005343 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5344
Dan Gohman46510a72010-04-15 01:51:59 +00005345 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005346 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005347 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005348 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005349 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005350
Owen Anderson1d0be152009-08-13 21:58:54 +00005351 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005353
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005354 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005355 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005356
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005357 // Second pass over the constraints: compute which constraint option to use
5358 // and assign registers to constraints that want a specific physreg.
5359 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5360 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005361
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005362 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005363 // matching input. If their types mismatch, e.g. one is an integer, the
5364 // other is floating point, or their sizes are different, flag it as an
5365 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005366 if (OpInfo.hasMatchingInput()) {
5367 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005368
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005369 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005370 if ((OpInfo.ConstraintVT.isInteger() !=
5371 Input.ConstraintVT.isInteger()) ||
5372 (OpInfo.ConstraintVT.getSizeInBits() !=
5373 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005374 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005375 " with a matching output constraint of"
5376 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005377 }
5378 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005379 }
5380 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005381
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005382 // Compute the constraint code and ConstraintType to use.
Dale Johannesen1784d162010-06-25 21:55:36 +00005383 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005384
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005385 // If this is a memory input, and if the operand is not indirect, do what we
5386 // need to to provide an address for the memory input.
5387 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5388 !OpInfo.isIndirect) {
5389 assert(OpInfo.Type == InlineAsm::isInput &&
5390 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005391
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005392 // Memory operands really want the address of the value. If we don't have
5393 // an indirect input, put it in the constpool if we can, otherwise spill
5394 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005395
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005396 // If the operand is a float, integer, or vector constant, spill to a
5397 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005398 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005399 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5400 isa<ConstantVector>(OpVal)) {
5401 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5402 TLI.getPointerTy());
5403 } else {
5404 // Otherwise, create a stack slot and emit a store to it before the
5405 // asm.
5406 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005407 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005408 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5409 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005410 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005411 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005412 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005413 OpInfo.CallOperand, StackSlot, NULL, 0,
5414 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005415 OpInfo.CallOperand = StackSlot;
5416 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005417
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005418 // There is no longer a Value* corresponding to this operand.
5419 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005421 // It is now an indirect operand.
5422 OpInfo.isIndirect = true;
5423 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005425 // If this constraint is for a specific register, allocate it before
5426 // anything else.
5427 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005428 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005429 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005430
Bill Wendling651ad132009-12-22 01:25:10 +00005431 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005432
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005433 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005434 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005435 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5436 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005437
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005438 // C_Register operands have already been allocated, Other/Memory don't need
5439 // to be.
5440 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005441 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005442 }
5443
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005444 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5445 std::vector<SDValue> AsmNodeOperands;
5446 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5447 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005448 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5449 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005450
Chris Lattnerdecc2672010-04-07 05:20:54 +00005451 // If we have a !srcloc metadata node associated with it, we want to attach
5452 // this to the ultimately generated inline asm machineinstr. To do this, we
5453 // pass in the third operand as this (potentially null) inline asm MDNode.
5454 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5455 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005456
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005457 // Remember the AlignStack bit as operand 3.
5458 AsmNodeOperands.push_back(DAG.getTargetConstant(IA->isAlignStack() ? 1 : 0,
5459 MVT::i1));
5460
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005461 // Loop over all of the inputs, copying the operand values into the
5462 // appropriate registers and processing the output regs.
5463 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005464
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005465 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5466 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005467
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5469 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5470
5471 switch (OpInfo.Type) {
5472 case InlineAsm::isOutput: {
5473 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5474 OpInfo.ConstraintType != TargetLowering::C_Register) {
5475 // Memory output, or 'other' output (e.g. 'X' constraint).
5476 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5477
5478 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005479 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5480 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005481 TLI.getPointerTy()));
5482 AsmNodeOperands.push_back(OpInfo.CallOperand);
5483 break;
5484 }
5485
5486 // Otherwise, this is a register or register class output.
5487
5488 // Copy the output from the appropriate register. Find a register that
5489 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005490 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005491 report_fatal_error("Couldn't allocate output reg for constraint '" +
5492 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005493
5494 // If this is an indirect operand, store through the pointer after the
5495 // asm.
5496 if (OpInfo.isIndirect) {
5497 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5498 OpInfo.CallOperandVal));
5499 } else {
5500 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005501 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005502 // Concatenate this output onto the outputs list.
5503 RetValRegs.append(OpInfo.AssignedRegs);
5504 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005506 // Add information to the INLINEASM node to know that this register is
5507 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005508 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005509 InlineAsm::Kind_RegDefEarlyClobber :
5510 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005511 false,
5512 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005513 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005514 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005515 break;
5516 }
5517 case InlineAsm::isInput: {
5518 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005519
Chris Lattner6bdcda32008-10-17 16:47:46 +00005520 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005521 // If this is required to match an output register we have already set,
5522 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005523 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005524
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005525 // Scan until we find the definition we already emitted of this operand.
5526 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005527 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005528 for (; OperandNo; --OperandNo) {
5529 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005530 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005531 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005532 assert((InlineAsm::isRegDefKind(OpFlag) ||
5533 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5534 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005535 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005536 }
5537
Evan Cheng697cbbf2009-03-20 18:03:34 +00005538 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005539 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005540 if (InlineAsm::isRegDefKind(OpFlag) ||
5541 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005542 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005543 if (OpInfo.isIndirect) {
5544 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005545 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005546 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5547 " don't know how to handle tied "
5548 "indirect register inputs");
5549 }
5550
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005551 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005552 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005553 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005554 MatchedRegs.RegVTs.push_back(RegVT);
5555 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005556 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005557 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005558 MatchedRegs.Regs.push_back
5559 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005560
5561 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005562 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005563 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005564 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005565 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005566 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005567 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005568 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005569
5570 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5571 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5572 "Unexpected number of operands");
5573 // Add information to the INLINEASM node to know about this input.
5574 // See InlineAsm.h isUseOperandTiedToDef.
5575 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5576 OpInfo.getMatchedOperand());
5577 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5578 TLI.getPointerTy()));
5579 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5580 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005581 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005582
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005583 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005584 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005585 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005586
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005587 std::vector<SDValue> Ops;
5588 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Dale Johannesen1784d162010-06-25 21:55:36 +00005589 Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005590 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005591 report_fatal_error("Invalid operand for inline asm constraint '" +
5592 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005593
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005594 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005595 unsigned ResOpType =
5596 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005597 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005598 TLI.getPointerTy()));
5599 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5600 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005601 }
5602
5603 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005604 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5605 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5606 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005607
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005608 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005609 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005610 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005611 TLI.getPointerTy()));
5612 AsmNodeOperands.push_back(InOperandVal);
5613 break;
5614 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005615
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005616 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5617 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5618 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005619 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005620 "Don't know how to handle indirect register inputs yet!");
5621
5622 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005623 if (OpInfo.AssignedRegs.Regs.empty() ||
Dan Gohman7451d3e2010-05-29 17:03:36 +00005624 !OpInfo.AssignedRegs.areValueTypesLegal(TLI))
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005625 report_fatal_error("Couldn't allocate input reg for constraint '" +
5626 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005627
Dale Johannesen66978ee2009-01-31 02:22:37 +00005628 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005629 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005630
Chris Lattnerdecc2672010-04-07 05:20:54 +00005631 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005632 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005633 break;
5634 }
5635 case InlineAsm::isClobber: {
5636 // Add the clobbered value to the operand list, so that the register
5637 // allocator is aware that the physreg got clobbered.
5638 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005639 OpInfo.AssignedRegs.AddInlineAsmOperands(
5640 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005641 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005642 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005643 break;
5644 }
5645 }
5646 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005647
Chris Lattnerdecc2672010-04-07 05:20:54 +00005648 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005649 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005650 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005651
Dale Johannesen66978ee2009-01-31 02:22:37 +00005652 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 &AsmNodeOperands[0], AsmNodeOperands.size());
5655 Flag = Chain.getValue(1);
5656
5657 // If this asm returns a register value, copy the result from that register
5658 // and set it as the value of the call.
5659 if (!RetValRegs.Regs.empty()) {
Dan Gohman7451d3e2010-05-29 17:03:36 +00005660 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005661 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005662
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005663 // FIXME: Why don't we do this for inline asms with MRVs?
5664 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005665 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005666
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005667 // If any of the results of the inline asm is a vector, it may have the
5668 // wrong width/num elts. This can happen for register classes that can
5669 // contain multiple different value types. The preg or vreg allocated may
5670 // not have the same VT as was expected. Convert it to the right type
5671 // with bit_convert.
5672 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005673 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005674 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005675
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005676 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005677 ResultType.isInteger() && Val.getValueType().isInteger()) {
5678 // If a result value was tied to an input value, the computed result may
5679 // have a wider width than the expected result. Extract the relevant
5680 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005681 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005682 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005683
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005684 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005685 }
Dan Gohman95915732008-10-18 01:03:45 +00005686
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005687 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005688 // Don't need to use this as a chain in this case.
5689 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5690 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005691 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005692
Dan Gohman46510a72010-04-15 01:51:59 +00005693 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005694
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005695 // Process indirect outputs, first output all of the flagged copies out of
5696 // physregs.
5697 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5698 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005699 const Value *Ptr = IndirectStoresToEmit[i].second;
Dan Gohman7451d3e2010-05-29 17:03:36 +00005700 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005701 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005702 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5703 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005705 // Emit the non-flagged stores from the physregs.
5706 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005707 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5708 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5709 StoresToEmit[i].first,
5710 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005711 StoresToEmit[i].second, 0,
5712 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005713 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005714 }
5715
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005716 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005717 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005718 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005719
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005720 DAG.setRoot(Chain);
5721}
5722
Dan Gohman46510a72010-04-15 01:51:59 +00005723void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005724 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5725 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005726 getValue(I.getArgOperand(0)),
5727 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005728}
5729
Dan Gohman46510a72010-04-15 01:51:59 +00005730void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005731 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5732 getRoot(), getValue(I.getOperand(0)),
5733 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005734 setValue(&I, V);
5735 DAG.setRoot(V.getValue(1));
5736}
5737
Dan Gohman46510a72010-04-15 01:51:59 +00005738void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005739 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5740 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005741 getValue(I.getArgOperand(0)),
5742 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005743}
5744
Dan Gohman46510a72010-04-15 01:51:59 +00005745void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005746 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5747 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00005748 getValue(I.getArgOperand(0)),
5749 getValue(I.getArgOperand(1)),
5750 DAG.getSrcValue(I.getArgOperand(0)),
5751 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005752}
5753
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005754/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005755/// implementation, which just calls LowerCall.
5756/// FIXME: When all targets are
5757/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005758std::pair<SDValue, SDValue>
5759TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5760 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005761 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005762 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005763 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005764 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005765 ArgListTy &Args, SelectionDAG &DAG,
5766 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005767 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005768 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005769 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005770 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005771 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5772 for (unsigned Value = 0, NumValues = ValueVTs.size();
5773 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005774 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005775 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005776 SDValue Op = SDValue(Args[i].Node.getNode(),
5777 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005778 ISD::ArgFlagsTy Flags;
5779 unsigned OriginalAlignment =
5780 getTargetData()->getABITypeAlignment(ArgTy);
5781
5782 if (Args[i].isZExt)
5783 Flags.setZExt();
5784 if (Args[i].isSExt)
5785 Flags.setSExt();
5786 if (Args[i].isInReg)
5787 Flags.setInReg();
5788 if (Args[i].isSRet)
5789 Flags.setSRet();
5790 if (Args[i].isByVal) {
5791 Flags.setByVal();
5792 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5793 const Type *ElementTy = Ty->getElementType();
5794 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005795 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005796 // For ByVal, alignment should come from FE. BE will guess if this
5797 // info is not there but there are cases it cannot get right.
5798 if (Args[i].Alignment)
5799 FrameAlign = Args[i].Alignment;
5800 Flags.setByValAlign(FrameAlign);
5801 Flags.setByValSize(FrameSize);
5802 }
5803 if (Args[i].isNest)
5804 Flags.setNest();
5805 Flags.setOrigAlign(OriginalAlignment);
5806
Owen Anderson23b9b192009-08-12 00:36:31 +00005807 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5808 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005809 SmallVector<SDValue, 4> Parts(NumParts);
5810 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5811
5812 if (Args[i].isSExt)
5813 ExtendKind = ISD::SIGN_EXTEND;
5814 else if (Args[i].isZExt)
5815 ExtendKind = ISD::ZERO_EXTEND;
5816
Bill Wendling46ada192010-03-02 01:55:18 +00005817 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005818 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005819
Dan Gohman98ca4f22009-08-05 01:29:28 +00005820 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005821 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005822 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5823 if (NumParts > 1 && j == 0)
5824 MyFlags.Flags.setSplit();
5825 else if (j != 0)
5826 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005827
Dan Gohman98ca4f22009-08-05 01:29:28 +00005828 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005829 }
5830 }
5831 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005832
Dan Gohman98ca4f22009-08-05 01:29:28 +00005833 // Handle the incoming return values from the call.
5834 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005835 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005836 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005837 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005838 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005839 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5840 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005841 for (unsigned i = 0; i != NumRegs; ++i) {
5842 ISD::InputArg MyFlags;
5843 MyFlags.VT = RegisterVT;
5844 MyFlags.Used = isReturnValueUsed;
5845 if (RetSExt)
5846 MyFlags.Flags.setSExt();
5847 if (RetZExt)
5848 MyFlags.Flags.setZExt();
5849 if (isInreg)
5850 MyFlags.Flags.setInReg();
5851 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005852 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005853 }
5854
Dan Gohman98ca4f22009-08-05 01:29:28 +00005855 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005856 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005857 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005858
5859 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005860 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005861 "LowerCall didn't return a valid chain!");
5862 assert((!isTailCall || InVals.empty()) &&
5863 "LowerCall emitted a return value for a tail call!");
5864 assert((isTailCall || InVals.size() == Ins.size()) &&
5865 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005866
5867 // For a tail call, the return value is merely live-out and there aren't
5868 // any nodes in the DAG representing it. Return a special value to
5869 // indicate that a tail call has been emitted and no more Instructions
5870 // should be processed in the current block.
5871 if (isTailCall) {
5872 DAG.setRoot(Chain);
5873 return std::make_pair(SDValue(), SDValue());
5874 }
5875
Evan Chengaf1871f2010-03-11 19:38:18 +00005876 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5877 assert(InVals[i].getNode() &&
5878 "LowerCall emitted a null value!");
5879 assert(Ins[i].VT == InVals[i].getValueType() &&
5880 "LowerCall emitted a value with the wrong type!");
5881 });
5882
Dan Gohman98ca4f22009-08-05 01:29:28 +00005883 // Collect the legal value parts into potentially illegal values
5884 // that correspond to the original function's return values.
5885 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5886 if (RetSExt)
5887 AssertOp = ISD::AssertSext;
5888 else if (RetZExt)
5889 AssertOp = ISD::AssertZext;
5890 SmallVector<SDValue, 4> ReturnValues;
5891 unsigned CurReg = 0;
5892 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005893 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005894 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5895 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005896
Bill Wendling46ada192010-03-02 01:55:18 +00005897 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005898 NumRegs, RegisterVT, VT,
5899 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005900 CurReg += NumRegs;
5901 }
5902
5903 // For a function returning void, there is no return value. We can't create
5904 // such a node, so we just return a null return value in that case. In
5905 // that case, nothing will actualy look at the value.
5906 if (ReturnValues.empty())
5907 return std::make_pair(SDValue(), Chain);
5908
5909 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5910 DAG.getVTList(&RetTys[0], RetTys.size()),
5911 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005912 return std::make_pair(Res, Chain);
5913}
5914
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005915void TargetLowering::LowerOperationWrapper(SDNode *N,
5916 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005917 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005918 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005919 if (Res.getNode())
5920 Results.push_back(Res);
5921}
5922
Dan Gohmand858e902010-04-17 15:26:15 +00005923SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005924 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005925 return SDValue();
5926}
5927
Dan Gohman46510a72010-04-15 01:51:59 +00005928void
5929SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00005930 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005931 assert((Op.getOpcode() != ISD::CopyFromReg ||
5932 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5933 "Copy from a reg to the same reg!");
5934 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5935
Owen Anderson23b9b192009-08-12 00:36:31 +00005936 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005937 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005938 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005939 PendingExports.push_back(Chain);
5940}
5941
5942#include "llvm/CodeGen/SelectionDAGISel.h"
5943
Dan Gohman46510a72010-04-15 01:51:59 +00005944void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005945 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005946 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005947 SelectionDAG &DAG = SDB->DAG;
Dan Gohman2048b852009-11-23 18:04:58 +00005948 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005949 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005950 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005951
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005952 // Check whether the function can return without sret-demotion.
5953 SmallVector<EVT, 4> OutVTs;
5954 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005955 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005956 OutVTs, OutsFlags, TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005957
Dan Gohman7451d3e2010-05-29 17:03:36 +00005958 FuncInfo->CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(),
5959 F.isVarArg(),
5960 OutVTs, OutsFlags, DAG);
5961 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005962 // Put in an sret pointer parameter before all the other parameters.
5963 SmallVector<EVT, 1> ValueVTs;
5964 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5965
5966 // NOTE: Assuming that a pointer will never break down to more than one VT
5967 // or one register.
5968 ISD::ArgFlagsTy Flags;
5969 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00005970 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005971 ISD::InputArg RetArg(Flags, RegisterVT, true);
5972 Ins.push_back(RetArg);
5973 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005974
Dan Gohman98ca4f22009-08-05 01:29:28 +00005975 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005976 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005977 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005978 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005979 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005980 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5981 bool isArgValueUsed = !I->use_empty();
5982 for (unsigned Value = 0, NumValues = ValueVTs.size();
5983 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005984 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005985 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005986 ISD::ArgFlagsTy Flags;
5987 unsigned OriginalAlignment =
5988 TD->getABITypeAlignment(ArgTy);
5989
5990 if (F.paramHasAttr(Idx, Attribute::ZExt))
5991 Flags.setZExt();
5992 if (F.paramHasAttr(Idx, Attribute::SExt))
5993 Flags.setSExt();
5994 if (F.paramHasAttr(Idx, Attribute::InReg))
5995 Flags.setInReg();
5996 if (F.paramHasAttr(Idx, Attribute::StructRet))
5997 Flags.setSRet();
5998 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5999 Flags.setByVal();
6000 const PointerType *Ty = cast<PointerType>(I->getType());
6001 const Type *ElementTy = Ty->getElementType();
6002 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6003 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6004 // For ByVal, alignment should be passed from FE. BE will guess if
6005 // this info is not there but there are cases it cannot get right.
6006 if (F.getParamAlignment(Idx))
6007 FrameAlign = F.getParamAlignment(Idx);
6008 Flags.setByValAlign(FrameAlign);
6009 Flags.setByValSize(FrameSize);
6010 }
6011 if (F.paramHasAttr(Idx, Attribute::Nest))
6012 Flags.setNest();
6013 Flags.setOrigAlign(OriginalAlignment);
6014
Owen Anderson23b9b192009-08-12 00:36:31 +00006015 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6016 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006017 for (unsigned i = 0; i != NumRegs; ++i) {
6018 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6019 if (NumRegs > 1 && i == 0)
6020 MyFlags.Flags.setSplit();
6021 // if it isn't first piece, alignment must be 1
6022 else if (i > 0)
6023 MyFlags.Flags.setOrigAlign(1);
6024 Ins.push_back(MyFlags);
6025 }
6026 }
6027 }
6028
6029 // Call the target to set up the argument values.
6030 SmallVector<SDValue, 8> InVals;
6031 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6032 F.isVarArg(), Ins,
6033 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006034
6035 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006036 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006037 "LowerFormalArguments didn't return a valid chain!");
6038 assert(InVals.size() == Ins.size() &&
6039 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006040 DEBUG({
6041 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6042 assert(InVals[i].getNode() &&
6043 "LowerFormalArguments emitted a null value!");
6044 assert(Ins[i].VT == InVals[i].getValueType() &&
6045 "LowerFormalArguments emitted a value with the wrong type!");
6046 }
6047 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006048
Dan Gohman5e866062009-08-06 15:37:27 +00006049 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006050 DAG.setRoot(NewRoot);
6051
6052 // Set up the argument values.
6053 unsigned i = 0;
6054 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006055 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006056 // Create a virtual register for the sret pointer, and put in a copy
6057 // from the sret argument into it.
6058 SmallVector<EVT, 1> ValueVTs;
6059 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6060 EVT VT = ValueVTs[0];
6061 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6062 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006063 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006064 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006065
Dan Gohman2048b852009-11-23 18:04:58 +00006066 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006067 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6068 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006069 FuncInfo->DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006070 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6071 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006072 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006073
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006074 // i indexes lowered arguments. Bump it past the hidden sret argument.
6075 // Idx indexes LLVM arguments. Don't touch it.
6076 ++i;
6077 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006078
Dan Gohman46510a72010-04-15 01:51:59 +00006079 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006080 ++I, ++Idx) {
6081 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006082 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006083 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006084 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006085
6086 // If this argument is unused then remember its value. It is used to generate
6087 // debugging information.
6088 if (I->use_empty() && NumValues)
6089 SDB->setUnusedArgValue(I, InVals[i]);
6090
Dan Gohman98ca4f22009-08-05 01:29:28 +00006091 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006092 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006093 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6094 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006095
6096 if (!I->use_empty()) {
6097 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6098 if (F.paramHasAttr(Idx, Attribute::SExt))
6099 AssertOp = ISD::AssertSext;
6100 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6101 AssertOp = ISD::AssertZext;
6102
Bill Wendling46ada192010-03-02 01:55:18 +00006103 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006104 NumParts, PartVT, VT,
6105 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006106 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006107
Dan Gohman98ca4f22009-08-05 01:29:28 +00006108 i += NumParts;
6109 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006110
Dan Gohman98ca4f22009-08-05 01:29:28 +00006111 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00006112 SDValue Res;
6113 if (!ArgValues.empty())
6114 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6115 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006116 SDB->setValue(I, Res);
6117
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006118 // If this argument is live outside of the entry block, insert a copy from
6119 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006120 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006121 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006122 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006123
Dan Gohman98ca4f22009-08-05 01:29:28 +00006124 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006125
6126 // Finally, if the target has anything special to do, allow it to do so.
6127 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006128 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006129}
6130
6131/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6132/// ensure constants are generated when needed. Remember the virtual registers
6133/// that need to be added to the Machine PHI nodes as input. We cannot just
6134/// directly add them, because expansion might result in multiple MBB's for one
6135/// BB. As such, the start of the BB might correspond to a different MBB than
6136/// the end.
6137///
6138void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006139SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006140 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006141
6142 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6143
6144 // Check successor nodes' PHI nodes that expect a constant to be available
6145 // from this block.
6146 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006147 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006148 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006149 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006150
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006151 // If this terminator has multiple identical successors (common for
6152 // switches), only handle each succ once.
6153 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006155 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006156
6157 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6158 // nodes and Machine PHI nodes, but the incoming operands have not been
6159 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006160 for (BasicBlock::const_iterator I = SuccBB->begin();
6161 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006162 // Ignore dead phi's.
6163 if (PN->use_empty()) continue;
6164
6165 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006166 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006167
Dan Gohman46510a72010-04-15 01:51:59 +00006168 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006169 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006170 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00006171 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006172 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006173 }
6174 Reg = RegOut;
6175 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00006176 DenseMap<const Value *, unsigned>::iterator I =
6177 FuncInfo.ValueMap.find(PHIOp);
6178 if (I != FuncInfo.ValueMap.end())
6179 Reg = I->second;
6180 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006181 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006182 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006183 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00006184 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006185 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006186 }
6187 }
6188
6189 // Remember that this register needs to added to the machine PHI node as
6190 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006191 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006192 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6193 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006194 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006195 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006196 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006197 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006198 Reg += NumRegisters;
6199 }
6200 }
6201 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006202 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006203}