blob: 9f8f0c4dd753f0fd1126fb573d1b25749e33e62f [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();
701 PendingLoads.clear();
702 PendingExports.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000703 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000704 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000705}
706
707/// getRoot - Return the current virtual root of the Selection DAG,
708/// flushing any PendingLoad items. This must be done before emitting
709/// a store or any other node that may need to be ordered after any
710/// prior load instructions.
711///
Dan Gohman2048b852009-11-23 18:04:58 +0000712SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000713 if (PendingLoads.empty())
714 return DAG.getRoot();
715
716 if (PendingLoads.size() == 1) {
717 SDValue Root = PendingLoads[0];
718 DAG.setRoot(Root);
719 PendingLoads.clear();
720 return Root;
721 }
722
723 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000725 &PendingLoads[0], PendingLoads.size());
726 PendingLoads.clear();
727 DAG.setRoot(Root);
728 return Root;
729}
730
731/// getControlRoot - Similar to getRoot, but instead of flushing all the
732/// PendingLoad items, flush all the PendingExports items. It is necessary
733/// to do this before emitting a terminator instruction.
734///
Dan Gohman2048b852009-11-23 18:04:58 +0000735SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000736 SDValue Root = DAG.getRoot();
737
738 if (PendingExports.empty())
739 return Root;
740
741 // Turn all of the CopyToReg chains into one factored node.
742 if (Root.getOpcode() != ISD::EntryToken) {
743 unsigned i = 0, e = PendingExports.size();
744 for (; i != e; ++i) {
745 assert(PendingExports[i].getNode()->getNumOperands() > 1);
746 if (PendingExports[i].getNode()->getOperand(0) == Root)
747 break; // Don't add the root if we already indirectly depend on it.
748 }
749
750 if (i == e)
751 PendingExports.push_back(Root);
752 }
753
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000755 &PendingExports[0],
756 PendingExports.size());
757 PendingExports.clear();
758 DAG.setRoot(Root);
759 return Root;
760}
761
Bill Wendling4533cac2010-01-28 21:51:40 +0000762void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
763 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
764 DAG.AssignOrdering(Node, SDNodeOrder);
765
766 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
767 AssignOrderingToNode(Node->getOperand(I).getNode());
768}
769
Dan Gohman46510a72010-04-15 01:51:59 +0000770void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000771 // Set up outgoing PHI node register values before emitting the terminator.
772 if (isa<TerminatorInst>(&I))
773 HandlePHINodesInSuccessorBlocks(I.getParent());
774
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000775 CurDebugLoc = I.getDebugLoc();
776
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000777 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000778
Dan Gohman92884f72010-04-20 15:03:56 +0000779 if (!isa<TerminatorInst>(&I) && !HasTailCall)
780 CopyToExportRegsIfNeeded(&I);
781
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000782 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000783}
784
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000785void SelectionDAGBuilder::visitPHI(const PHINode &) {
786 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
787}
788
Dan Gohman46510a72010-04-15 01:51:59 +0000789void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000790 // Note: this doesn't use InstVisitor, because it has to work with
791 // ConstantExpr's in addition to instructions.
792 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000793 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000794 // Build the switch statement using the Instruction.def file.
795#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000796 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000797#include "llvm/Instruction.def"
798 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000799
800 // Assign the ordering to the freshly created DAG nodes.
801 if (NodeMap.count(&I)) {
802 ++SDNodeOrder;
803 AssignOrderingToNode(getValue(&I).getNode());
804 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000805}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000806
Dan Gohman2048b852009-11-23 18:04:58 +0000807SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000808 SDValue &N = NodeMap[V];
809 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000810
Dan Gohman383b5f62010-04-17 15:32:28 +0000811 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000812 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000813
Dan Gohman383b5f62010-04-17 15:32:28 +0000814 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000815 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000816
Dan Gohman383b5f62010-04-17 15:32:28 +0000817 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000818 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000820 if (isa<ConstantPointerNull>(C))
821 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000822
Dan Gohman383b5f62010-04-17 15:32:28 +0000823 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000824 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000825
Nate Begeman9008ca62009-04-27 18:41:29 +0000826 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000827 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000828
Dan Gohman383b5f62010-04-17 15:32:28 +0000829 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000830 visit(CE->getOpcode(), *CE);
831 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000832 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000833 return N1;
834 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000835
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000836 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
837 SmallVector<SDValue, 4> Constants;
838 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
839 OI != OE; ++OI) {
840 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000841 // If the operand is an empty aggregate, there are no values.
842 if (!Val) continue;
843 // Add each leaf value from the operand to the Constants list
844 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000845 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
846 Constants.push_back(SDValue(Val, i));
847 }
Bill Wendling87710f02009-12-21 23:47:40 +0000848
Bill Wendling4533cac2010-01-28 21:51:40 +0000849 return DAG.getMergeValues(&Constants[0], Constants.size(),
850 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000851 }
852
Duncan Sands1df98592010-02-16 11:11:14 +0000853 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000854 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
855 "Unknown struct or array constant!");
856
Owen Andersone50ed302009-08-10 22:56:29 +0000857 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000858 ComputeValueVTs(TLI, C->getType(), ValueVTs);
859 unsigned NumElts = ValueVTs.size();
860 if (NumElts == 0)
861 return SDValue(); // empty struct
862 SmallVector<SDValue, 4> Constants(NumElts);
863 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000864 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000865 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000866 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000867 else if (EltVT.isFloatingPoint())
868 Constants[i] = DAG.getConstantFP(0, EltVT);
869 else
870 Constants[i] = DAG.getConstant(0, EltVT);
871 }
Bill Wendling87710f02009-12-21 23:47:40 +0000872
Bill Wendling4533cac2010-01-28 21:51:40 +0000873 return DAG.getMergeValues(&Constants[0], NumElts,
874 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000875 }
876
Dan Gohman383b5f62010-04-17 15:32:28 +0000877 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000878 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000879
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000880 const VectorType *VecTy = cast<VectorType>(V->getType());
881 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000882
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000883 // Now that we know the number and type of the elements, get that number of
884 // elements into the Ops array based on what kind of constant it is.
885 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000886 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000887 for (unsigned i = 0; i != NumElements; ++i)
888 Ops.push_back(getValue(CP->getOperand(i)));
889 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000890 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000891 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000892
893 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000894 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000895 Op = DAG.getConstantFP(0, EltVT);
896 else
897 Op = DAG.getConstant(0, EltVT);
898 Ops.assign(NumElements, Op);
899 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000900
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000901 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000902 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
903 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000904 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000905
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000906 // If this is a static alloca, generate it as the frameindex instead of
907 // computation.
908 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
909 DenseMap<const AllocaInst*, int>::iterator SI =
910 FuncInfo.StaticAllocaMap.find(AI);
911 if (SI != FuncInfo.StaticAllocaMap.end())
912 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
913 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915 unsigned InReg = FuncInfo.ValueMap[V];
916 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000917
Owen Anderson23b9b192009-08-12 00:36:31 +0000918 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000919 SDValue Chain = DAG.getEntryNode();
Dan Gohman7451d3e2010-05-29 17:03:36 +0000920 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000921}
922
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000923/// Get the EVTs and ArgFlags collections that represent the legalized return
924/// type of the given function. This does not require a DAG or a return value,
925/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000926static void getReturnInfo(const Type* ReturnType,
927 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000928 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000929 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000930 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000931 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000932 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000933 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000934 if (NumValues == 0) return;
935 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000936
937 for (unsigned j = 0, f = NumValues; j != f; ++j) {
938 EVT VT = ValueVTs[j];
939 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000940
941 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000942 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000943 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000944 ExtendKind = ISD::ZERO_EXTEND;
945
946 // FIXME: C calling convention requires the return type to be promoted to
947 // at least 32-bit. But this is not necessary for non-C calling
948 // conventions. The frontend should mark functions whose return values
949 // require promoting with signext or zeroext attributes.
950 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000951 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000952 if (VT.bitsLT(MinVT))
953 VT = MinVT;
954 }
955
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000956 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
957 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000958 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
959 PartVT.getTypeForEVT(ReturnType->getContext()));
960
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000961 // 'inreg' on function refers to return value
962 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000963 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000964 Flags.setInReg();
965
966 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000967 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000968 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000969 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000970 Flags.setZExt();
971
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000972 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000973 OutVTs.push_back(PartVT);
974 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000975 if (Offsets)
976 {
977 Offsets->push_back(Offset);
978 Offset += PartSize;
979 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000980 }
981 }
982}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000983
Dan Gohman46510a72010-04-15 01:51:59 +0000984void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000985 SDValue Chain = getControlRoot();
986 SmallVector<ISD::OutputArg, 8> Outs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000987
Dan Gohman7451d3e2010-05-29 17:03:36 +0000988 if (!FuncInfo.CanLowerReturn) {
989 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000990 const Function *F = I.getParent()->getParent();
991
992 // Emit a store of the return value through the virtual register.
993 // Leave Outs empty so that LowerReturn won't try to load return
994 // registers the usual way.
995 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000996 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000997 PtrValueVTs);
998
999 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1000 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001001
Owen Andersone50ed302009-08-10 22:56:29 +00001002 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001003 SmallVector<uint64_t, 4> Offsets;
1004 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001005 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001006
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001007 SmallVector<SDValue, 4> Chains(NumValues);
1008 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +00001009 for (unsigned i = 0; i != NumValues; ++i) {
1010 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
1011 DAG.getConstant(Offsets[i], PtrVT));
1012 Chains[i] =
1013 DAG.getStore(Chain, getCurDebugLoc(),
1014 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00001015 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001016 }
1017
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001018 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
1019 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001020 } else if (I.getNumOperands() != 0) {
1021 SmallVector<EVT, 4> ValueVTs;
1022 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
1023 unsigned NumValues = ValueVTs.size();
1024 if (NumValues) {
1025 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001026 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1027 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001028
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001029 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001030
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001031 const Function *F = I.getParent()->getParent();
1032 if (F->paramHasAttr(0, Attribute::SExt))
1033 ExtendKind = ISD::SIGN_EXTEND;
1034 else if (F->paramHasAttr(0, Attribute::ZExt))
1035 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001036
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001037 // FIXME: C calling convention requires the return type to be promoted
1038 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001039 // conventions. The frontend should mark functions whose return values
1040 // require promoting with signext or zeroext attributes.
1041 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
1042 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
1043 if (VT.bitsLT(MinVT))
1044 VT = MinVT;
1045 }
1046
1047 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
1048 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
1049 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +00001050 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001051 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
1052 &Parts[0], NumParts, PartVT, ExtendKind);
1053
1054 // 'inreg' on function refers to return value
1055 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1056 if (F->paramHasAttr(0, Attribute::InReg))
1057 Flags.setInReg();
1058
1059 // Propagate extension type if any
1060 if (F->paramHasAttr(0, Attribute::SExt))
1061 Flags.setSExt();
1062 else if (F->paramHasAttr(0, Attribute::ZExt))
1063 Flags.setZExt();
1064
1065 for (unsigned i = 0; i < NumParts; ++i)
1066 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +00001067 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001068 }
1069 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001070
1071 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001072 CallingConv::ID CallConv =
1073 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001074 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
1075 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001076
1077 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001078 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001079 "LowerReturn didn't return a valid chain!");
1080
1081 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001082 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083}
1084
Dan Gohmanad62f532009-04-23 23:13:24 +00001085/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1086/// created for it, emit nodes to copy the value into the virtual
1087/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001088void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +00001089 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1090 if (VMI != FuncInfo.ValueMap.end()) {
1091 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1092 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001093 }
1094}
1095
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001096/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1097/// the current basic block, add it to ValueMap now so that we'll get a
1098/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001099void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001100 // No need to export constants.
1101 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001103 // Already exported?
1104 if (FuncInfo.isExportedInst(V)) return;
1105
1106 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1107 CopyValueToVirtualRegister(V, Reg);
1108}
1109
Dan Gohman46510a72010-04-15 01:51:59 +00001110bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001111 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001112 // The operands of the setcc have to be in this block. We don't know
1113 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001114 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001115 // Can export from current BB.
1116 if (VI->getParent() == FromBB)
1117 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001118
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001119 // Is already exported, noop.
1120 return FuncInfo.isExportedInst(V);
1121 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001122
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001123 // If this is an argument, we can export it if the BB is the entry block or
1124 // if it is already exported.
1125 if (isa<Argument>(V)) {
1126 if (FromBB == &FromBB->getParent()->getEntryBlock())
1127 return true;
1128
1129 // Otherwise, can only export this if it is already exported.
1130 return FuncInfo.isExportedInst(V);
1131 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001132
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001133 // Otherwise, constants can always be exported.
1134 return true;
1135}
1136
1137static bool InBlock(const Value *V, const BasicBlock *BB) {
1138 if (const Instruction *I = dyn_cast<Instruction>(V))
1139 return I->getParent() == BB;
1140 return true;
1141}
1142
Dan Gohmanc2277342008-10-17 21:16:08 +00001143/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1144/// This function emits a branch and is used at the leaves of an OR or an
1145/// AND operator tree.
1146///
1147void
Dan Gohman46510a72010-04-15 01:51:59 +00001148SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001149 MachineBasicBlock *TBB,
1150 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001151 MachineBasicBlock *CurBB,
1152 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001153 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001154
Dan Gohmanc2277342008-10-17 21:16:08 +00001155 // If the leaf of the tree is a comparison, merge the condition into
1156 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001157 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001158 // The operands of the cmp have to be in this block. We don't know
1159 // how to export them from some other block. If this is the first block
1160 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001161 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001162 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1163 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001164 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001165 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001166 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001167 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001168 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001169 } else {
1170 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001171 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001173
1174 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001175 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1176 SwitchCases.push_back(CB);
1177 return;
1178 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001179 }
1180
1181 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001182 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001183 NULL, TBB, FBB, CurBB);
1184 SwitchCases.push_back(CB);
1185}
1186
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001187/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001188void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001189 MachineBasicBlock *TBB,
1190 MachineBasicBlock *FBB,
1191 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001192 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001193 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001194 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001195 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001196 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001197 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1198 BOp->getParent() != CurBB->getBasicBlock() ||
1199 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1200 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001201 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001202 return;
1203 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205 // Create TmpBB after CurBB.
1206 MachineFunction::iterator BBI = CurBB;
1207 MachineFunction &MF = DAG.getMachineFunction();
1208 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1209 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001211 if (Opc == Instruction::Or) {
1212 // Codegen X | Y as:
1213 // jmp_if_X TBB
1214 // jmp TmpBB
1215 // TmpBB:
1216 // jmp_if_Y TBB
1217 // jmp FBB
1218 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001220 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001221 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001224 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001225 } else {
1226 assert(Opc == Instruction::And && "Unknown merge op!");
1227 // Codegen X & Y as:
1228 // jmp_if_X TmpBB
1229 // jmp FBB
1230 // TmpBB:
1231 // jmp_if_Y TBB
1232 // jmp FBB
1233 //
1234 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001235
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001237 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001238
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001240 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001241 }
1242}
1243
1244/// If the set of cases should be emitted as a series of branches, return true.
1245/// If we should emit this as a bunch of and/or'd together conditions, return
1246/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001247bool
Dan Gohman2048b852009-11-23 18:04:58 +00001248SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001249 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001251 // If this is two comparisons of the same values or'd or and'd together, they
1252 // will get folded into a single comparison, so don't emit two blocks.
1253 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1254 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1255 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1256 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1257 return false;
1258 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001259
Chris Lattner133ce872010-01-02 00:00:03 +00001260 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1261 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1262 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1263 Cases[0].CC == Cases[1].CC &&
1264 isa<Constant>(Cases[0].CmpRHS) &&
1265 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1266 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1267 return false;
1268 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1269 return false;
1270 }
1271
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001272 return true;
1273}
1274
Dan Gohman46510a72010-04-15 01:51:59 +00001275void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001276 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 // Update machine-CFG edges.
1279 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1280
1281 // Figure out which block is immediately after the current one.
1282 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001283 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001284 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001285 NextBlock = BBI;
1286
1287 if (I.isUnconditional()) {
1288 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001289 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001292 if (Succ0MBB != NextBlock)
1293 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001294 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001295 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001297 return;
1298 }
1299
1300 // If this condition is one of the special cases we handle, do special stuff
1301 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001302 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1304
1305 // If this is a series of conditions that are or'd or and'd together, emit
1306 // this as a sequence of branches instead of setcc's with and/or operations.
1307 // For example, instead of something like:
1308 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001309 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001310 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001311 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001312 // or C, F
1313 // jnz foo
1314 // Emit:
1315 // cmp A, B
1316 // je foo
1317 // cmp D, E
1318 // jle foo
1319 //
Dan Gohman46510a72010-04-15 01:51:59 +00001320 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001321 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001322 (BOp->getOpcode() == Instruction::And ||
1323 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001324 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1325 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001326 // If the compares in later blocks need to use values not currently
1327 // exported from this block, export them now. This block should always
1328 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001329 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001330
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331 // Allow some cases to be rejected.
1332 if (ShouldEmitAsBranches(SwitchCases)) {
1333 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1334 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1335 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1336 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001337
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001338 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001339 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001340 SwitchCases.erase(SwitchCases.begin());
1341 return;
1342 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001344 // Okay, we decided not to do this, remove any inserted MBB's and clear
1345 // SwitchCases.
1346 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001347 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001348
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001349 SwitchCases.clear();
1350 }
1351 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001352
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001353 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001354 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001355 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001356
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001357 // Use visitSwitchCase to actually insert the fast branch sequence for this
1358 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001359 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001360}
1361
1362/// visitSwitchCase - Emits the necessary code to represent a single node in
1363/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001364void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1365 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001366 SDValue Cond;
1367 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001368 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001369
1370 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001371 if (CB.CmpMHS == NULL) {
1372 // Fold "(X == true)" to X and "(X == false)" to !X to
1373 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001374 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001375 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001376 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001377 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001378 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001379 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001380 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001381 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001382 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001383 } else {
1384 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1385
Anton Korobeynikov23218582008-12-23 22:25:27 +00001386 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1387 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001388
1389 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001390 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001391
1392 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001393 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001394 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001395 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001396 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001397 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001398 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001399 DAG.getConstant(High-Low, VT), ISD::SETULE);
1400 }
1401 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001402
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001403 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001404 SwitchBB->addSuccessor(CB.TrueBB);
1405 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001406
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001407 // Set NextBlock to be the MBB immediately after the current one, if any.
1408 // This is used to avoid emitting unnecessary branches to the next block.
1409 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001410 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001411 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001412 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001413
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001414 // If the lhs block is the next block, invert the condition so that we can
1415 // fall through to the lhs instead of the rhs block.
1416 if (CB.TrueBB == NextBlock) {
1417 std::swap(CB.TrueBB, CB.FalseBB);
1418 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001419 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001420 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001421
Dale Johannesenf5d97892009-02-04 01:48:28 +00001422 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001424 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001425
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001426 // If the branch was constant folded, fix up the CFG.
1427 if (BrCond.getOpcode() == ISD::BR) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001428 SwitchBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001429 } else {
1430 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001431 if (BrCond == getControlRoot())
Dan Gohman99be8ae2010-04-19 22:41:47 +00001432 SwitchBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001433
Bill Wendling4533cac2010-01-28 21:51:40 +00001434 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001435 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1436 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001437 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001438
1439 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001440}
1441
1442/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001443void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001444 // Emit the code for the jump table
1445 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001446 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001447 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1448 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001450 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1451 MVT::Other, Index.getValue(1),
1452 Table, Index);
1453 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454}
1455
1456/// visitJumpTableHeader - This function emits necessary code to produce index
1457/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001458void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001459 JumpTableHeader &JTH,
1460 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001461 // Subtract the lowest switch case value from the value being switched on and
1462 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001463 // difference between smallest and largest cases.
1464 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001465 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001466 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001467 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001468
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001469 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001470 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001471 // can be used as an index into the jump table in a subsequent basic block.
1472 // This value may be smaller or larger than the target's pointer type, and
1473 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001474 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001475
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001476 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001477 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1478 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001479 JT.Reg = JumpTableReg;
1480
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001481 // Emit the range check for the jump table, and branch to the default block
1482 // for the switch statement if the value being switched on exceeds the largest
1483 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001484 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001485 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001486 DAG.getConstant(JTH.Last-JTH.First,VT),
1487 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001488
1489 // Set NextBlock to be the MBB immediately after the current one, if any.
1490 // This is used to avoid emitting unnecessary branches to the next block.
1491 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001492 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001493
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001494 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001495 NextBlock = BBI;
1496
Dale Johannesen66978ee2009-01-31 02:22:37 +00001497 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001499 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001500
Bill Wendling4533cac2010-01-28 21:51:40 +00001501 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001502 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1503 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001504
Bill Wendling87710f02009-12-21 23:47:40 +00001505 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001506}
1507
1508/// visitBitTestHeader - This function emits necessary code to produce value
1509/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001510void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1511 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001512 // Subtract the minimum value
1513 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001514 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001515 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001516 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001517
1518 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001519 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001520 TLI.getSetCCResultType(Sub.getValueType()),
1521 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001522 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001523
Bill Wendling87710f02009-12-21 23:47:40 +00001524 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1525 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001526
Duncan Sands92abc622009-01-31 15:50:11 +00001527 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001528 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1529 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001530
1531 // Set NextBlock to be the MBB immediately after the current one, if any.
1532 // This is used to avoid emitting unnecessary branches to the next block.
1533 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001534 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001535 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001536 NextBlock = BBI;
1537
1538 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1539
Dan Gohman99be8ae2010-04-19 22:41:47 +00001540 SwitchBB->addSuccessor(B.Default);
1541 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542
Dale Johannesen66978ee2009-01-31 02:22:37 +00001543 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001544 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001545 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001546
Bill Wendling4533cac2010-01-28 21:51:40 +00001547 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001548 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1549 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001550
Bill Wendling87710f02009-12-21 23:47:40 +00001551 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552}
1553
1554/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001555void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1556 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001557 BitTestCase &B,
1558 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001559 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001560 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001561 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001562 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001563 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001564 DAG.getConstant(1, TLI.getPointerTy()),
1565 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001566
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001567 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001568 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001569 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001570 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001571 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1572 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001573 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001574 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001575
Dan Gohman99be8ae2010-04-19 22:41:47 +00001576 SwitchBB->addSuccessor(B.TargetBB);
1577 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001578
Dale Johannesen66978ee2009-01-31 02:22:37 +00001579 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001580 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001581 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001582
1583 // Set NextBlock to be the MBB immediately after the current one, if any.
1584 // This is used to avoid emitting unnecessary branches to the next block.
1585 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001586 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001587 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001588 NextBlock = BBI;
1589
Bill Wendling4533cac2010-01-28 21:51:40 +00001590 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001591 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1592 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001593
Bill Wendling87710f02009-12-21 23:47:40 +00001594 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001595}
1596
Dan Gohman46510a72010-04-15 01:51:59 +00001597void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001598 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1599
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001600 // Retrieve successors.
1601 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1602 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1603
Gabor Greifb67e6b32009-01-15 11:10:44 +00001604 const Value *Callee(I.getCalledValue());
1605 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001606 visitInlineAsm(&I);
1607 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001608 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609
1610 // If the value of the invoke is used outside of its defining block, make it
1611 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001612 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001613
1614 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001615 InvokeMBB->addSuccessor(Return);
1616 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001617
1618 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001619 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1620 MVT::Other, getControlRoot(),
1621 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622}
1623
Dan Gohman46510a72010-04-15 01:51:59 +00001624void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001625}
1626
1627/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1628/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001629bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1630 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001631 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001632 MachineBasicBlock *Default,
1633 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001634 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001635
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001636 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001637 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001639 return false;
1640
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001641 // Get the MachineFunction which holds the current MBB. This is used when
1642 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001643 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001644
1645 // Figure out which block is immediately after the current one.
1646 MachineBasicBlock *NextBlock = 0;
1647 MachineFunction::iterator BBI = CR.CaseBB;
1648
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001649 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001650 NextBlock = BBI;
1651
1652 // TODO: If any two of the cases has the same destination, and if one value
1653 // is the same as the other, but has one bit unset that the other has set,
1654 // use bit manipulation to do two compares at once. For example:
1655 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001656
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001657 // Rearrange the case blocks so that the last one falls through if possible.
1658 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1659 // The last case block won't fall through into 'NextBlock' if we emit the
1660 // branches in this order. See if rearranging a case value would help.
1661 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1662 if (I->BB == NextBlock) {
1663 std::swap(*I, BackCase);
1664 break;
1665 }
1666 }
1667 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001668
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001669 // Create a CaseBlock record representing a conditional branch to
1670 // the Case's target mbb if the value being switched on SV is equal
1671 // to C.
1672 MachineBasicBlock *CurBlock = CR.CaseBB;
1673 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1674 MachineBasicBlock *FallThrough;
1675 if (I != E-1) {
1676 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1677 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001678
1679 // Put SV in a virtual register to make it available from the new blocks.
1680 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681 } else {
1682 // If the last case doesn't match, go to the default block.
1683 FallThrough = Default;
1684 }
1685
Dan Gohman46510a72010-04-15 01:51:59 +00001686 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001687 ISD::CondCode CC;
1688 if (I->High == I->Low) {
1689 // This is just small small case range :) containing exactly 1 case
1690 CC = ISD::SETEQ;
1691 LHS = SV; RHS = I->High; MHS = NULL;
1692 } else {
1693 CC = ISD::SETLE;
1694 LHS = I->Low; MHS = SV; RHS = I->High;
1695 }
1696 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001697
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001698 // If emitting the first comparison, just call visitSwitchCase to emit the
1699 // code into the current block. Otherwise, push the CaseBlock onto the
1700 // vector to be later processed by SDISel, and insert the node's MBB
1701 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001702 if (CurBlock == SwitchBB)
1703 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001704 else
1705 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001706
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001707 CurBlock = FallThrough;
1708 }
1709
1710 return true;
1711}
1712
1713static inline bool areJTsAllowed(const TargetLowering &TLI) {
1714 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001715 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1716 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001717}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001718
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001719static APInt ComputeRange(const APInt &First, const APInt &Last) {
1720 APInt LastExt(Last), FirstExt(First);
1721 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1722 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1723 return (LastExt - FirstExt + 1ULL);
1724}
1725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001727bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1728 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001729 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001730 MachineBasicBlock* Default,
1731 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001732 Case& FrontCase = *CR.Range.first;
1733 Case& BackCase = *(CR.Range.second-1);
1734
Chris Lattnere880efe2009-11-07 07:50:34 +00001735 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1736 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737
Chris Lattnere880efe2009-11-07 07:50:34 +00001738 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001739 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1740 I!=E; ++I)
1741 TSize += I->size();
1742
Dan Gohmane0567812010-04-08 23:03:40 +00001743 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001745
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001746 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001747 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748 if (Density < 0.4)
1749 return false;
1750
David Greene4b69d992010-01-05 01:24:57 +00001751 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001752 << "First entry: " << First << ". Last entry: " << Last << '\n'
1753 << "Range: " << Range
1754 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001755
1756 // Get the MachineFunction which holds the current MBB. This is used when
1757 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001758 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001759
1760 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001761 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001762 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001763
1764 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1765
1766 // Create a new basic block to hold the code for loading the address
1767 // of the jump table, and jumping to it. Update successor information;
1768 // we will either branch to the default case for the switch, or the jump
1769 // table.
1770 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1771 CurMF->insert(BBI, JumpTableBB);
1772 CR.CaseBB->addSuccessor(Default);
1773 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001774
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001775 // Build a vector of destination BBs, corresponding to each target
1776 // of the jump table. If the value of the jump table slot corresponds to
1777 // a case statement, push the case's BB onto the vector, otherwise, push
1778 // the default BB.
1779 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001780 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001782 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1783 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001784
1785 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001786 DestBBs.push_back(I->BB);
1787 if (TEI==High)
1788 ++I;
1789 } else {
1790 DestBBs.push_back(Default);
1791 }
1792 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001793
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001794 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001795 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1796 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001797 E = DestBBs.end(); I != E; ++I) {
1798 if (!SuccsHandled[(*I)->getNumber()]) {
1799 SuccsHandled[(*I)->getNumber()] = true;
1800 JumpTableBB->addSuccessor(*I);
1801 }
1802 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001803
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001804 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001805 unsigned JTEncoding = TLI.getJumpTableEncoding();
1806 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001807 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001808
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001809 // Set the jump table information so that we can codegen it as a second
1810 // MachineBasicBlock
1811 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001812 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1813 if (CR.CaseBB == SwitchBB)
1814 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001815
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816 JTCases.push_back(JumpTableBlock(JTH, JT));
1817
1818 return true;
1819}
1820
1821/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1822/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001823bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1824 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001825 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001826 MachineBasicBlock *Default,
1827 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001828 // Get the MachineFunction which holds the current MBB. This is used when
1829 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001830 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001831
1832 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001833 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001834 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001835
1836 Case& FrontCase = *CR.Range.first;
1837 Case& BackCase = *(CR.Range.second-1);
1838 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1839
1840 // Size is the number of Cases represented by this range.
1841 unsigned Size = CR.Range.second - CR.Range.first;
1842
Chris Lattnere880efe2009-11-07 07:50:34 +00001843 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1844 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001845 double FMetric = 0;
1846 CaseItr Pivot = CR.Range.first + Size/2;
1847
1848 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1849 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001850 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001851 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1852 I!=E; ++I)
1853 TSize += I->size();
1854
Chris Lattnere880efe2009-11-07 07:50:34 +00001855 APInt LSize = FrontCase.size();
1856 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001857 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001858 << "First: " << First << ", Last: " << Last <<'\n'
1859 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001860 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1861 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001862 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1863 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001864 APInt Range = ComputeRange(LEnd, RBegin);
1865 assert((Range - 2ULL).isNonNegative() &&
1866 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001867 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001868 (LEnd - First + 1ULL).roundToDouble();
1869 double RDensity = (double)RSize.roundToDouble() /
1870 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001871 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001872 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001873 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001874 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1875 << "LDensity: " << LDensity
1876 << ", RDensity: " << RDensity << '\n'
1877 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001878 if (FMetric < Metric) {
1879 Pivot = J;
1880 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001881 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 }
1883
1884 LSize += J->size();
1885 RSize -= J->size();
1886 }
1887 if (areJTsAllowed(TLI)) {
1888 // If our case is dense we *really* should handle it earlier!
1889 assert((FMetric > 0) && "Should handle dense range earlier!");
1890 } else {
1891 Pivot = CR.Range.first + Size/2;
1892 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001893
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001894 CaseRange LHSR(CR.Range.first, Pivot);
1895 CaseRange RHSR(Pivot, CR.Range.second);
1896 Constant *C = Pivot->Low;
1897 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001898
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001899 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001900 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001901 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001902 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903 // Pivot's Value, then we can branch directly to the LHS's Target,
1904 // rather than creating a leaf node for it.
1905 if ((LHSR.second - LHSR.first) == 1 &&
1906 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001907 cast<ConstantInt>(C)->getValue() ==
1908 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001909 TrueBB = LHSR.first->BB;
1910 } else {
1911 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1912 CurMF->insert(BBI, TrueBB);
1913 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001914
1915 // Put SV in a virtual register to make it available from the new blocks.
1916 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001917 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001918
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919 // Similar to the optimization above, if the Value being switched on is
1920 // known to be less than the Constant CR.LT, and the current Case Value
1921 // is CR.LT - 1, then we can branch directly to the target block for
1922 // the current Case Value, rather than emitting a RHS leaf node for it.
1923 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001924 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1925 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001926 FalseBB = RHSR.first->BB;
1927 } else {
1928 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1929 CurMF->insert(BBI, FalseBB);
1930 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001931
1932 // Put SV in a virtual register to make it available from the new blocks.
1933 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934 }
1935
1936 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001937 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001938 // Otherwise, branch to LHS.
1939 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1940
Dan Gohman99be8ae2010-04-19 22:41:47 +00001941 if (CR.CaseBB == SwitchBB)
1942 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001943 else
1944 SwitchCases.push_back(CB);
1945
1946 return true;
1947}
1948
1949/// handleBitTestsSwitchCase - if current case range has few destination and
1950/// range span less, than machine word bitwidth, encode case range into series
1951/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001952bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1953 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001954 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001955 MachineBasicBlock* Default,
1956 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001957 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001958 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001959
1960 Case& FrontCase = *CR.Range.first;
1961 Case& BackCase = *(CR.Range.second-1);
1962
1963 // Get the MachineFunction which holds the current MBB. This is used when
1964 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001965 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001966
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001967 // If target does not have legal shift left, do not emit bit tests at all.
1968 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1969 return false;
1970
Anton Korobeynikov23218582008-12-23 22:25:27 +00001971 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001972 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1973 I!=E; ++I) {
1974 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001975 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001976 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001977
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001978 // Count unique destinations
1979 SmallSet<MachineBasicBlock*, 4> Dests;
1980 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1981 Dests.insert(I->BB);
1982 if (Dests.size() > 3)
1983 // Don't bother the code below, if there are too much unique destinations
1984 return false;
1985 }
David Greene4b69d992010-01-05 01:24:57 +00001986 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001987 << Dests.size() << '\n'
1988 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001991 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1992 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001993 APInt cmpRange = maxValue - minValue;
1994
David Greene4b69d992010-01-05 01:24:57 +00001995 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001996 << "Low bound: " << minValue << '\n'
1997 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001998
Dan Gohmane0567812010-04-08 23:03:40 +00001999 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002000 (!(Dests.size() == 1 && numCmps >= 3) &&
2001 !(Dests.size() == 2 && numCmps >= 5) &&
2002 !(Dests.size() >= 3 && numCmps >= 6)))
2003 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002004
David Greene4b69d992010-01-05 01:24:57 +00002005 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002006 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2007
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002008 // Optimize the case where all the case values fit in a
2009 // word without having to subtract minValue. In this case,
2010 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00002011 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002012 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002013 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002014 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002015 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 CaseBitsVector CasesBits;
2018 unsigned i, count = 0;
2019
2020 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2021 MachineBasicBlock* Dest = I->BB;
2022 for (i = 0; i < count; ++i)
2023 if (Dest == CasesBits[i].BB)
2024 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002025
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026 if (i == count) {
2027 assert((count < 3) && "Too much destinations to test!");
2028 CasesBits.push_back(CaseBits(0, Dest, 0));
2029 count++;
2030 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002031
2032 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2033 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2034
2035 uint64_t lo = (lowValue - lowBound).getZExtValue();
2036 uint64_t hi = (highValue - lowBound).getZExtValue();
2037
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002038 for (uint64_t j = lo; j <= hi; j++) {
2039 CasesBits[i].Mask |= 1ULL << j;
2040 CasesBits[i].Bits++;
2041 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002042
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002043 }
2044 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002046 BitTestInfo BTC;
2047
2048 // Figure out which block is immediately after the current one.
2049 MachineFunction::iterator BBI = CR.CaseBB;
2050 ++BBI;
2051
2052 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2053
David Greene4b69d992010-01-05 01:24:57 +00002054 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002055 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002056 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002057 << ", Bits: " << CasesBits[i].Bits
2058 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002059
2060 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2061 CurMF->insert(BBI, CaseBB);
2062 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2063 CaseBB,
2064 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002065
2066 // Put SV in a virtual register to make it available from the new blocks.
2067 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002068 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002069
2070 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002071 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 CR.CaseBB, Default, BTC);
2073
Dan Gohman99be8ae2010-04-19 22:41:47 +00002074 if (CR.CaseBB == SwitchBB)
2075 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002076
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002077 BitTestCases.push_back(BTB);
2078
2079 return true;
2080}
2081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002082/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002083size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2084 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002085 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002086
2087 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002088 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002089 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2090 Cases.push_back(Case(SI.getSuccessorValue(i),
2091 SI.getSuccessorValue(i),
2092 SMBB));
2093 }
2094 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2095
2096 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002097 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002098 // Must recompute end() each iteration because it may be
2099 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002100 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2101 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2102 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002103 MachineBasicBlock* nextBB = J->BB;
2104 MachineBasicBlock* currentBB = I->BB;
2105
2106 // If the two neighboring cases go to the same destination, merge them
2107 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002108 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002109 I->High = J->High;
2110 J = Cases.erase(J);
2111 } else {
2112 I = J++;
2113 }
2114 }
2115
2116 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2117 if (I->Low != I->High)
2118 // A range counts double, since it requires two compares.
2119 ++numCmps;
2120 }
2121
2122 return numCmps;
2123}
2124
Dan Gohman46510a72010-04-15 01:51:59 +00002125void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002126 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
2127
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002128 // Figure out which block is immediately after the current one.
2129 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002130 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2131
2132 // If there is only the default destination, branch to it if it is not the
2133 // next basic block. Otherwise, just fall through.
2134 if (SI.getNumOperands() == 2) {
2135 // Update machine-CFG edges.
2136
2137 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002138 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002139 if (Default != NextBlock)
2140 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
2141 MVT::Other, getControlRoot(),
2142 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002143
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002144 return;
2145 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002147 // If there are any non-default case statements, create a vector of Cases
2148 // representing each one, and sort the vector so that we can efficiently
2149 // create a binary search tree from them.
2150 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002151 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002152 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002153 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002154 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002155
2156 // Get the Value to be switched on and default basic blocks, which will be
2157 // inserted into CaseBlock records, representing basic blocks in the binary
2158 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002159 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002160
2161 // Push the initial CaseRec onto the worklist
2162 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002163 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2164 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002165
2166 while (!WorkList.empty()) {
2167 // Grab a record representing a case range to process off the worklist
2168 CaseRec CR = WorkList.back();
2169 WorkList.pop_back();
2170
Dan Gohman99be8ae2010-04-19 22:41:47 +00002171 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002172 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002174 // If the range has few cases (two or less) emit a series of specific
2175 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002176 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002177 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002178
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002179 // If the switch has more than 5 blocks, and at least 40% dense, and the
2180 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002181 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002182 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002183 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002185 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2186 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002187 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002188 }
2189}
2190
Dan Gohman46510a72010-04-15 01:51:59 +00002191void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002192 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2193
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002194 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002195 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002196 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002197 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002198 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002199 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002200 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2201 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002202 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002203
Bill Wendling4533cac2010-01-28 21:51:40 +00002204 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2205 MVT::Other, getControlRoot(),
2206 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002207}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208
Dan Gohman46510a72010-04-15 01:51:59 +00002209void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002210 // -0.0 - X --> fneg
2211 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002212 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002213 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2214 const VectorType *DestTy = cast<VectorType>(I.getType());
2215 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002216 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002217 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002218 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002219 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002220 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002221 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2222 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223 return;
2224 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002225 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002227
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002228 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002229 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002230 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002231 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2232 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002233 return;
2234 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002236 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002237}
2238
Dan Gohman46510a72010-04-15 01:51:59 +00002239void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240 SDValue Op1 = getValue(I.getOperand(0));
2241 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002242 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2243 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002244}
2245
Dan Gohman46510a72010-04-15 01:51:59 +00002246void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002247 SDValue Op1 = getValue(I.getOperand(0));
2248 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002249 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002250 Op2.getValueType() != TLI.getShiftAmountTy()) {
2251 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002252 EVT PTy = TLI.getPointerTy();
2253 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002254 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002255 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2256 TLI.getShiftAmountTy(), Op2);
2257 // If the operand is larger than the shift count type but the shift
2258 // count type has enough bits to represent any shift value, truncate
2259 // it now. This is a common case and it exposes the truncate to
2260 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002261 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002262 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2263 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2264 TLI.getShiftAmountTy(), Op2);
2265 // Otherwise we'll need to temporarily settle for some other
2266 // convenient type; type legalization will make adjustments as
2267 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002268 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002269 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002270 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002271 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002272 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002273 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002275
Bill Wendling4533cac2010-01-28 21:51:40 +00002276 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2277 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002278}
2279
Dan Gohman46510a72010-04-15 01:51:59 +00002280void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002281 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002282 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002283 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002284 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002285 predicate = ICmpInst::Predicate(IC->getPredicate());
2286 SDValue Op1 = getValue(I.getOperand(0));
2287 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002288 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002289
Owen Andersone50ed302009-08-10 22:56:29 +00002290 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002291 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002292}
2293
Dan Gohman46510a72010-04-15 01:51:59 +00002294void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002296 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002297 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002298 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002299 predicate = FCmpInst::Predicate(FC->getPredicate());
2300 SDValue Op1 = getValue(I.getOperand(0));
2301 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002302 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002303 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002304 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002305}
2306
Dan Gohman46510a72010-04-15 01:51:59 +00002307void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002308 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002309 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2310 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002311 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002312
Bill Wendling49fcff82009-12-21 22:30:11 +00002313 SmallVector<SDValue, 4> Values(NumValues);
2314 SDValue Cond = getValue(I.getOperand(0));
2315 SDValue TrueVal = getValue(I.getOperand(1));
2316 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002317
Bill Wendling4533cac2010-01-28 21:51:40 +00002318 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002319 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002320 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2321 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002322 SDValue(TrueVal.getNode(),
2323 TrueVal.getResNo() + i),
2324 SDValue(FalseVal.getNode(),
2325 FalseVal.getResNo() + i));
2326
Bill Wendling4533cac2010-01-28 21:51:40 +00002327 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2328 DAG.getVTList(&ValueVTs[0], NumValues),
2329 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002330}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002331
Dan Gohman46510a72010-04-15 01:51:59 +00002332void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002333 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2334 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002335 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002336 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002337}
2338
Dan Gohman46510a72010-04-15 01:51:59 +00002339void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002340 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2341 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2342 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002343 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002344 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002345}
2346
Dan Gohman46510a72010-04-15 01:51:59 +00002347void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002348 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2349 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2350 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002351 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002352 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002353}
2354
Dan Gohman46510a72010-04-15 01:51:59 +00002355void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002356 // FPTrunc is never a no-op cast, no need to check
2357 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002358 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002359 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2360 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002361}
2362
Dan Gohman46510a72010-04-15 01:51:59 +00002363void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002364 // FPTrunc is never a no-op cast, no need to check
2365 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002366 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002367 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002368}
2369
Dan Gohman46510a72010-04-15 01:51:59 +00002370void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002371 // FPToUI is never a no-op cast, no need to check
2372 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002373 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002374 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002375}
2376
Dan Gohman46510a72010-04-15 01:51:59 +00002377void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002378 // FPToSI is never a no-op cast, no need to check
2379 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002380 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002381 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002382}
2383
Dan Gohman46510a72010-04-15 01:51:59 +00002384void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002385 // UIToFP is never a no-op cast, no need to check
2386 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002387 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002388 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002389}
2390
Dan Gohman46510a72010-04-15 01:51:59 +00002391void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002392 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002393 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002394 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002395 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002396}
2397
Dan Gohman46510a72010-04-15 01:51:59 +00002398void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002399 // What to do depends on the size of the integer and the size of the pointer.
2400 // We can either truncate, zero extend, or no-op, accordingly.
2401 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002402 EVT SrcVT = N.getValueType();
2403 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002404 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002405}
2406
Dan Gohman46510a72010-04-15 01:51:59 +00002407void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002408 // What to do depends on the size of the integer and the size of the pointer.
2409 // We can either truncate, zero extend, or no-op, accordingly.
2410 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002411 EVT SrcVT = N.getValueType();
2412 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002413 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002414}
2415
Dan Gohman46510a72010-04-15 01:51:59 +00002416void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002417 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002418 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002419
Bill Wendling49fcff82009-12-21 22:30:11 +00002420 // BitCast assures us that source and destination are the same size so this is
2421 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002422 if (DestVT != N.getValueType())
2423 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2424 DestVT, N)); // convert types.
2425 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002426 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002427}
2428
Dan Gohman46510a72010-04-15 01:51:59 +00002429void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002430 SDValue InVec = getValue(I.getOperand(0));
2431 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002432 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002433 TLI.getPointerTy(),
2434 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002435 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2436 TLI.getValueType(I.getType()),
2437 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002438}
2439
Dan Gohman46510a72010-04-15 01:51:59 +00002440void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002441 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002442 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002443 TLI.getPointerTy(),
2444 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002445 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2446 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002447}
2448
Mon P Wangaeb06d22008-11-10 04:46:22 +00002449// Utility for visitShuffleVector - Returns true if the mask is mask starting
2450// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002451static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2452 unsigned MaskNumElts = Mask.size();
2453 for (unsigned i = 0; i != MaskNumElts; ++i)
2454 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002455 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002456 return true;
2457}
2458
Dan Gohman46510a72010-04-15 01:51:59 +00002459void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002460 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002461 SDValue Src1 = getValue(I.getOperand(0));
2462 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002463
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 // Convert the ConstantVector mask operand into an array of ints, with -1
2465 // representing undef values.
2466 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002467 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002468 unsigned MaskNumElts = MaskElts.size();
2469 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002470 if (isa<UndefValue>(MaskElts[i]))
2471 Mask.push_back(-1);
2472 else
2473 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2474 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002475
Owen Andersone50ed302009-08-10 22:56:29 +00002476 EVT VT = TLI.getValueType(I.getType());
2477 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002478 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002479
Mon P Wangc7849c22008-11-16 05:06:27 +00002480 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002481 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2482 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002483 return;
2484 }
2485
2486 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002487 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2488 // Mask is longer than the source vectors and is a multiple of the source
2489 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002490 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002491 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2492 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002493 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2494 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002495 return;
2496 }
2497
Mon P Wangc7849c22008-11-16 05:06:27 +00002498 // Pad both vectors with undefs to make them the same length as the mask.
2499 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002500 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2501 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002502 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002503
Nate Begeman9008ca62009-04-27 18:41:29 +00002504 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2505 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002506 MOps1[0] = Src1;
2507 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002508
2509 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2510 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002511 &MOps1[0], NumConcat);
2512 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002513 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002514 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002515
Mon P Wangaeb06d22008-11-10 04:46:22 +00002516 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002517 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002518 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002519 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002520 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002521 MappedOps.push_back(Idx);
2522 else
2523 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002524 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002525
Bill Wendling4533cac2010-01-28 21:51:40 +00002526 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2527 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002528 return;
2529 }
2530
Mon P Wangc7849c22008-11-16 05:06:27 +00002531 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002532 // Analyze the access pattern of the vector to see if we can extract
2533 // two subvectors and do the shuffle. The analysis is done by calculating
2534 // the range of elements the mask access on both vectors.
2535 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2536 int MaxRange[2] = {-1, -1};
2537
Nate Begeman5a5ca152009-04-29 05:20:52 +00002538 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002539 int Idx = Mask[i];
2540 int Input = 0;
2541 if (Idx < 0)
2542 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002543
Nate Begeman5a5ca152009-04-29 05:20:52 +00002544 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002545 Input = 1;
2546 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002547 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002548 if (Idx > MaxRange[Input])
2549 MaxRange[Input] = Idx;
2550 if (Idx < MinRange[Input])
2551 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002552 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002553
Mon P Wangc7849c22008-11-16 05:06:27 +00002554 // Check if the access is smaller than the vector size and can we find
2555 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002556 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2557 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002558 int StartIdx[2]; // StartIdx to extract from
2559 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002560 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002561 RangeUse[Input] = 0; // Unused
2562 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002563 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002564 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002565 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002566 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002567 RangeUse[Input] = 1; // Extract from beginning of the vector
2568 StartIdx[Input] = 0;
2569 } else {
2570 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002571 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002572 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002573 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002574 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002575 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002576 }
2577
Bill Wendling636e2582009-08-21 18:16:06 +00002578 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002579 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002580 return;
2581 }
2582 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2583 // Extract appropriate subvector and generate a vector shuffle
2584 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002585 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002586 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002587 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002588 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002589 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002590 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002591 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002592
Mon P Wangc7849c22008-11-16 05:06:27 +00002593 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002594 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002595 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002596 int Idx = Mask[i];
2597 if (Idx < 0)
2598 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002599 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002600 MappedOps.push_back(Idx - StartIdx[0]);
2601 else
2602 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002603 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002604
Bill Wendling4533cac2010-01-28 21:51:40 +00002605 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2606 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002607 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002608 }
2609 }
2610
Mon P Wangc7849c22008-11-16 05:06:27 +00002611 // We can't use either concat vectors or extract subvectors so fall back to
2612 // replacing the shuffle with extract and build vector.
2613 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002614 EVT EltVT = VT.getVectorElementType();
2615 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002616 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002617 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002618 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002619 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002620 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002621 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002622 SDValue Res;
2623
Nate Begeman5a5ca152009-04-29 05:20:52 +00002624 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002625 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2626 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002627 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002628 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2629 EltVT, Src2,
2630 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2631
2632 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002633 }
2634 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002635
Bill Wendling4533cac2010-01-28 21:51:40 +00002636 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2637 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002638}
2639
Dan Gohman46510a72010-04-15 01:51:59 +00002640void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002641 const Value *Op0 = I.getOperand(0);
2642 const Value *Op1 = I.getOperand(1);
2643 const Type *AggTy = I.getType();
2644 const Type *ValTy = Op1->getType();
2645 bool IntoUndef = isa<UndefValue>(Op0);
2646 bool FromUndef = isa<UndefValue>(Op1);
2647
2648 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2649 I.idx_begin(), I.idx_end());
2650
Owen Andersone50ed302009-08-10 22:56:29 +00002651 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002652 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002653 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002654 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2655
2656 unsigned NumAggValues = AggValueVTs.size();
2657 unsigned NumValValues = ValValueVTs.size();
2658 SmallVector<SDValue, 4> Values(NumAggValues);
2659
2660 SDValue Agg = getValue(Op0);
2661 SDValue Val = getValue(Op1);
2662 unsigned i = 0;
2663 // Copy the beginning value(s) from the original aggregate.
2664 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002665 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002666 SDValue(Agg.getNode(), Agg.getResNo() + i);
2667 // Copy values from the inserted value(s).
2668 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002669 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002670 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2671 // Copy remaining value(s) from the original aggregate.
2672 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002673 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002674 SDValue(Agg.getNode(), Agg.getResNo() + i);
2675
Bill Wendling4533cac2010-01-28 21:51:40 +00002676 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2677 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2678 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002679}
2680
Dan Gohman46510a72010-04-15 01:51:59 +00002681void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002682 const Value *Op0 = I.getOperand(0);
2683 const Type *AggTy = Op0->getType();
2684 const Type *ValTy = I.getType();
2685 bool OutOfUndef = isa<UndefValue>(Op0);
2686
2687 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2688 I.idx_begin(), I.idx_end());
2689
Owen Andersone50ed302009-08-10 22:56:29 +00002690 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002691 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2692
2693 unsigned NumValValues = ValValueVTs.size();
2694 SmallVector<SDValue, 4> Values(NumValValues);
2695
2696 SDValue Agg = getValue(Op0);
2697 // Copy out the selected value(s).
2698 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2699 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002700 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002701 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002702 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002703
Bill Wendling4533cac2010-01-28 21:51:40 +00002704 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2705 DAG.getVTList(&ValValueVTs[0], NumValValues),
2706 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002707}
2708
Dan Gohman46510a72010-04-15 01:51:59 +00002709void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002710 SDValue N = getValue(I.getOperand(0));
2711 const Type *Ty = I.getOperand(0)->getType();
2712
Dan Gohman46510a72010-04-15 01:51:59 +00002713 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002714 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002715 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002716 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2717 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2718 if (Field) {
2719 // N = N + Offset
2720 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002721 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002722 DAG.getIntPtrConstant(Offset));
2723 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002724
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002725 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002726 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2727 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2728
2729 // Offset canonically 0 for unions, but type changes
2730 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002731 } else {
2732 Ty = cast<SequentialType>(Ty)->getElementType();
2733
2734 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002735 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002736 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002737 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002738 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002739 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002740 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002741 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002742 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002743 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2744 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002745 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002746 else
Evan Chengb1032a82009-02-09 20:54:38 +00002747 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002748
Dale Johannesen66978ee2009-01-31 02:22:37 +00002749 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002750 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002751 continue;
2752 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002753
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002754 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002755 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2756 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002757 SDValue IdxN = getValue(Idx);
2758
2759 // If the index is smaller or larger than intptr_t, truncate or extend
2760 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002761 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002762
2763 // If this is a multiply by a power of two, turn it into a shl
2764 // immediately. This is a very common case.
2765 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002766 if (ElementSize.isPowerOf2()) {
2767 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002768 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002769 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002770 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002771 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002772 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002773 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002774 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002775 }
2776 }
2777
Scott Michelfdc40a02009-02-17 22:15:04 +00002778 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002779 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002780 }
2781 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002782
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002783 setValue(&I, N);
2784}
2785
Dan Gohman46510a72010-04-15 01:51:59 +00002786void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002787 // If this is a fixed sized alloca in the entry block of the function,
2788 // allocate it statically on the stack.
2789 if (FuncInfo.StaticAllocaMap.count(&I))
2790 return; // getValue will auto-populate this.
2791
2792 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002793 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002794 unsigned Align =
2795 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2796 I.getAlignment());
2797
2798 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002799
Owen Andersone50ed302009-08-10 22:56:29 +00002800 EVT IntPtr = TLI.getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00002801 if (AllocSize.getValueType() != IntPtr)
2802 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
2803
2804 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr,
2805 AllocSize,
2806 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 // Handle alignment. If the requested alignment is less than or equal to
2809 // the stack alignment, ignore it. If the size is greater than or equal to
2810 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002811 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002812 if (Align <= StackAlign)
2813 Align = 0;
2814
2815 // Round the size of the allocation up to the stack alignment size
2816 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002817 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002818 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002819 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002821 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002822 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002823 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002824 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2825
2826 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002827 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002828 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002829 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002830 setValue(&I, DSA);
2831 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002832
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002833 // Inform the Frame Information that we have just allocated a variable-sized
2834 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002835 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002836}
2837
Dan Gohman46510a72010-04-15 01:51:59 +00002838void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002839 const Value *SV = I.getOperand(0);
2840 SDValue Ptr = getValue(SV);
2841
2842 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002843
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002844 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002845 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002846 unsigned Alignment = I.getAlignment();
2847
Owen Andersone50ed302009-08-10 22:56:29 +00002848 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002849 SmallVector<uint64_t, 4> Offsets;
2850 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2851 unsigned NumValues = ValueVTs.size();
2852 if (NumValues == 0)
2853 return;
2854
2855 SDValue Root;
2856 bool ConstantMemory = false;
2857 if (I.isVolatile())
2858 // Serialize volatile loads with other side effects.
2859 Root = getRoot();
2860 else if (AA->pointsToConstantMemory(SV)) {
2861 // Do not serialize (non-volatile) loads of constant memory with anything.
2862 Root = DAG.getEntryNode();
2863 ConstantMemory = true;
2864 } else {
2865 // Do not serialize non-volatile loads against each other.
2866 Root = DAG.getRoot();
2867 }
2868
2869 SmallVector<SDValue, 4> Values(NumValues);
2870 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002871 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002872 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002873 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2874 PtrVT, Ptr,
2875 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002876 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002877 A, SV, Offsets[i], isVolatile,
2878 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002879
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002880 Values[i] = L;
2881 Chains[i] = L.getValue(1);
2882 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002883
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002884 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002885 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002886 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002887 if (isVolatile)
2888 DAG.setRoot(Chain);
2889 else
2890 PendingLoads.push_back(Chain);
2891 }
2892
Bill Wendling4533cac2010-01-28 21:51:40 +00002893 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2894 DAG.getVTList(&ValueVTs[0], NumValues),
2895 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002896}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002897
Dan Gohman46510a72010-04-15 01:51:59 +00002898void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2899 const Value *SrcV = I.getOperand(0);
2900 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002901
Owen Andersone50ed302009-08-10 22:56:29 +00002902 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002903 SmallVector<uint64_t, 4> Offsets;
2904 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2905 unsigned NumValues = ValueVTs.size();
2906 if (NumValues == 0)
2907 return;
2908
2909 // Get the lowered operands. Note that we do this after
2910 // checking if NumResults is zero, because with zero results
2911 // the operands won't have values in the map.
2912 SDValue Src = getValue(SrcV);
2913 SDValue Ptr = getValue(PtrV);
2914
2915 SDValue Root = getRoot();
2916 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002917 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002918 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002919 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002920 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002921
2922 for (unsigned i = 0; i != NumValues; ++i) {
2923 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2924 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002925 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002926 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002927 Add, PtrV, Offsets[i], isVolatile,
2928 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002929 }
2930
Bill Wendling4533cac2010-01-28 21:51:40 +00002931 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2932 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002933}
2934
2935/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2936/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002937void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002938 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002939 bool HasChain = !I.doesNotAccessMemory();
2940 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2941
2942 // Build the operand list.
2943 SmallVector<SDValue, 8> Ops;
2944 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2945 if (OnlyLoad) {
2946 // We don't need to serialize loads against other loads.
2947 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002948 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002949 Ops.push_back(getRoot());
2950 }
2951 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002952
2953 // Info is set by getTgtMemInstrinsic
2954 TargetLowering::IntrinsicInfo Info;
2955 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2956
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002957 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002958 if (!IsTgtIntrinsic)
2959 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002960
2961 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002962 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002963 SDValue Op = getValue(I.getOperand(i));
2964 assert(TLI.isTypeLegal(Op.getValueType()) &&
2965 "Intrinsic uses a non-legal type?");
2966 Ops.push_back(Op);
2967 }
2968
Owen Andersone50ed302009-08-10 22:56:29 +00002969 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002970 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2971#ifndef NDEBUG
2972 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2973 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2974 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002975 }
Bob Wilson8d919552009-07-31 22:41:21 +00002976#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002977
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002978 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002979 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002980
Bob Wilson8d919552009-07-31 22:41:21 +00002981 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002982
2983 // Create the node.
2984 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002985 if (IsTgtIntrinsic) {
2986 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002987 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002988 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002989 Info.memVT, Info.ptrVal, Info.offset,
2990 Info.align, Info.vol,
2991 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002992 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002993 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002994 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002995 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002996 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002997 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002998 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002999 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003000 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003001 }
3002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003003 if (HasChain) {
3004 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3005 if (OnlyLoad)
3006 PendingLoads.push_back(Chain);
3007 else
3008 DAG.setRoot(Chain);
3009 }
Bill Wendling856ff412009-12-22 00:12:37 +00003010
Benjamin Kramerf0127052010-01-05 13:12:22 +00003011 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003012 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003013 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003014 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003015 }
Bill Wendling856ff412009-12-22 00:12:37 +00003016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003017 setValue(&I, Result);
3018 }
3019}
3020
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003021/// GetSignificand - Get the significand and build it into a floating-point
3022/// number with exponent of 1:
3023///
3024/// Op = (Op & 0x007fffff) | 0x3f800000;
3025///
3026/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003027static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00003028GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3030 DAG.getConstant(0x007fffff, MVT::i32));
3031 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3032 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003033 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003034}
3035
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003036/// GetExponent - Get the exponent:
3037///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003038/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003039///
3040/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003041static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003042GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00003043 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003044 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3045 DAG.getConstant(0x7f800000, MVT::i32));
3046 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003047 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003048 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3049 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003050 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003051}
3052
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003053/// getF32Constant - Get 32-bit floating point constant.
3054static SDValue
3055getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003056 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003057}
3058
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003059/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003060/// visitIntrinsicCall: I is a call instruction
3061/// Op is the associated NodeType for I
3062const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003063SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
3064 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003065 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003066 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003067 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00003068 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003069 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003070 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00003071 getValue(I.getOperand(2)),
3072 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003073 setValue(&I, L);
3074 DAG.setRoot(L.getValue(1));
3075 return 0;
3076}
3077
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003078// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003079const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003080SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00003081 SDValue Op1 = getValue(I.getOperand(1));
3082 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003083
Owen Anderson825b72b2009-08-11 20:47:22 +00003084 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00003085 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003086 return 0;
3087}
Bill Wendling74c37652008-12-09 22:08:41 +00003088
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003089/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3090/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003091void
Dan Gohman46510a72010-04-15 01:51:59 +00003092SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003093 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003094 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003095
Eric Christopher551754c2010-04-16 23:37:20 +00003096 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003097 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003098 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003099
3100 // Put the exponent in the right bit position for later addition to the
3101 // final result:
3102 //
3103 // #define LOG2OFe 1.4426950f
3104 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003105 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003106 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003108
3109 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003110 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3111 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003112
3113 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003114 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003115 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003116
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003117 if (LimitFloatPrecision <= 6) {
3118 // For floating-point precision of 6:
3119 //
3120 // TwoToFractionalPartOfX =
3121 // 0.997535578f +
3122 // (0.735607626f + 0.252464424f * x) * x;
3123 //
3124 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003126 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003128 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003129 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3130 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003131 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003132 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003133
3134 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003136 TwoToFracPartOfX, IntegerPartOfX);
3137
Owen Anderson825b72b2009-08-11 20:47:22 +00003138 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003139 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3140 // For floating-point precision of 12:
3141 //
3142 // TwoToFractionalPartOfX =
3143 // 0.999892986f +
3144 // (0.696457318f +
3145 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3146 //
3147 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003149 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003151 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3153 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003154 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3156 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003157 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003159
3160 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003161 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003162 TwoToFracPartOfX, IntegerPartOfX);
3163
Owen Anderson825b72b2009-08-11 20:47:22 +00003164 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003165 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3166 // For floating-point precision of 18:
3167 //
3168 // TwoToFractionalPartOfX =
3169 // 0.999999982f +
3170 // (0.693148872f +
3171 // (0.240227044f +
3172 // (0.554906021e-1f +
3173 // (0.961591928e-2f +
3174 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3175 //
3176 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003177 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003178 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003179 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003180 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003181 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3182 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003183 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003184 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3185 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003186 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3188 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003189 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003190 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3191 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003192 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003193 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3194 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003195 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003196 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003197 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003198
3199 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003200 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003201 TwoToFracPartOfX, IntegerPartOfX);
3202
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003204 }
3205 } else {
3206 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003207 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003208 getValue(I.getOperand(1)).getValueType(),
3209 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003210 }
3211
Dale Johannesen59e577f2008-09-05 18:38:42 +00003212 setValue(&I, result);
3213}
3214
Bill Wendling39150252008-09-09 20:39:27 +00003215/// visitLog - Lower a log intrinsic. Handles the special sequences for
3216/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003217void
Dan Gohman46510a72010-04-15 01:51:59 +00003218SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003219 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003220 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003221
Eric Christopher551754c2010-04-16 23:37:20 +00003222 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003223 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003224 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003225 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003226
3227 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003228 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003230 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003231
3232 // Get the significand and build it into a floating-point number with
3233 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003234 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003235
3236 if (LimitFloatPrecision <= 6) {
3237 // For floating-point precision of 6:
3238 //
3239 // LogofMantissa =
3240 // -1.1609546f +
3241 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003242 //
Bill Wendling39150252008-09-09 20:39:27 +00003243 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003247 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3249 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003250 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003251
Scott Michelfdc40a02009-02-17 22:15:04 +00003252 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003253 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003254 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3255 // For floating-point precision of 12:
3256 //
3257 // LogOfMantissa =
3258 // -1.7417939f +
3259 // (2.8212026f +
3260 // (-1.4699568f +
3261 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3262 //
3263 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003264 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003265 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003266 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003267 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003268 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3269 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003270 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003271 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3272 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003273 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003274 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3275 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003276 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003277
Scott Michelfdc40a02009-02-17 22:15:04 +00003278 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003279 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003280 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3281 // For floating-point precision of 18:
3282 //
3283 // LogOfMantissa =
3284 // -2.1072184f +
3285 // (4.2372794f +
3286 // (-3.7029485f +
3287 // (2.2781945f +
3288 // (-0.87823314f +
3289 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3290 //
3291 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003293 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003295 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003296 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3297 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003298 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003299 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3300 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003301 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3303 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003304 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3306 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003307 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003308 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3309 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003310 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003311
Scott Michelfdc40a02009-02-17 22:15:04 +00003312 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003314 }
3315 } else {
3316 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003317 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003318 getValue(I.getOperand(1)).getValueType(),
3319 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003320 }
3321
Dale Johannesen59e577f2008-09-05 18:38:42 +00003322 setValue(&I, result);
3323}
3324
Bill Wendling3eb59402008-09-09 00:28:24 +00003325/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3326/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003327void
Dan Gohman46510a72010-04-15 01:51:59 +00003328SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003329 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003330 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003331
Eric Christopher551754c2010-04-16 23:37:20 +00003332 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003333 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003334 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003336
Bill Wendling39150252008-09-09 20:39:27 +00003337 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003338 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003339
Bill Wendling3eb59402008-09-09 00:28:24 +00003340 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003341 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003342 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003343
Bill Wendling3eb59402008-09-09 00:28:24 +00003344 // Different possible minimax approximations of significand in
3345 // floating-point for various degrees of accuracy over [1,2].
3346 if (LimitFloatPrecision <= 6) {
3347 // For floating-point precision of 6:
3348 //
3349 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3350 //
3351 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003353 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003354 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003355 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3357 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003358 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003359
Scott Michelfdc40a02009-02-17 22:15:04 +00003360 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003362 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3363 // For floating-point precision of 12:
3364 //
3365 // Log2ofMantissa =
3366 // -2.51285454f +
3367 // (4.07009056f +
3368 // (-2.12067489f +
3369 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003370 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003371 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003373 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003374 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003375 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3377 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003378 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3380 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003381 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003382 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3383 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003384 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003385
Scott Michelfdc40a02009-02-17 22:15:04 +00003386 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003387 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003388 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3389 // For floating-point precision of 18:
3390 //
3391 // Log2ofMantissa =
3392 // -3.0400495f +
3393 // (6.1129976f +
3394 // (-5.3420409f +
3395 // (3.2865683f +
3396 // (-1.2669343f +
3397 // (0.27515199f -
3398 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3399 //
3400 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003402 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003404 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3406 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003407 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3409 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003410 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3412 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003413 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3415 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003416 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003417 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3418 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003419 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003420
Scott Michelfdc40a02009-02-17 22:15:04 +00003421 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003423 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003424 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003425 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003426 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003427 getValue(I.getOperand(1)).getValueType(),
3428 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003429 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003430
Dale Johannesen59e577f2008-09-05 18:38:42 +00003431 setValue(&I, result);
3432}
3433
Bill Wendling3eb59402008-09-09 00:28:24 +00003434/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3435/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003436void
Dan Gohman46510a72010-04-15 01:51:59 +00003437SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003438 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003439 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003440
Eric Christopher551754c2010-04-16 23:37:20 +00003441 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003442 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003443 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003445
Bill Wendling39150252008-09-09 20:39:27 +00003446 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003447 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003449 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003450
3451 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003452 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003453 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003454
3455 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003456 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003457 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003458 // Log10ofMantissa =
3459 // -0.50419619f +
3460 // (0.60948995f - 0.10380950f * x) * x;
3461 //
3462 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003464 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003466 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3468 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003469 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003470
Scott Michelfdc40a02009-02-17 22:15:04 +00003471 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003472 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003473 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3474 // For floating-point precision of 12:
3475 //
3476 // Log10ofMantissa =
3477 // -0.64831180f +
3478 // (0.91751397f +
3479 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3480 //
3481 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003482 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003483 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003484 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003485 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3487 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003488 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003489 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3490 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003491 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003492
Scott Michelfdc40a02009-02-17 22:15:04 +00003493 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003494 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003495 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003496 // For floating-point precision of 18:
3497 //
3498 // Log10ofMantissa =
3499 // -0.84299375f +
3500 // (1.5327582f +
3501 // (-1.0688956f +
3502 // (0.49102474f +
3503 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3504 //
3505 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003506 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003507 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003509 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003510 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3511 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003512 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003513 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3514 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003515 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003516 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3517 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003518 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003519 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3520 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003521 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003522
Scott Michelfdc40a02009-02-17 22:15:04 +00003523 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003525 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003526 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003527 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003528 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003529 getValue(I.getOperand(1)).getValueType(),
3530 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003531 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003532
Dale Johannesen59e577f2008-09-05 18:38:42 +00003533 setValue(&I, result);
3534}
3535
Bill Wendlinge10c8142008-09-09 22:39:21 +00003536/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3537/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003538void
Dan Gohman46510a72010-04-15 01:51:59 +00003539SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003540 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003541 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003542
Eric Christopher551754c2010-04-16 23:37:20 +00003543 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003544 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003545 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003546
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003548
3549 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3551 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003552
3553 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003555 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003556
3557 if (LimitFloatPrecision <= 6) {
3558 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003559 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003560 // TwoToFractionalPartOfX =
3561 // 0.997535578f +
3562 // (0.735607626f + 0.252464424f * x) * x;
3563 //
3564 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003568 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3570 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003571 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003573 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003575
Scott Michelfdc40a02009-02-17 22:15:04 +00003576 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003578 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3579 // For floating-point precision of 12:
3580 //
3581 // TwoToFractionalPartOfX =
3582 // 0.999892986f +
3583 // (0.696457318f +
3584 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3585 //
3586 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003587 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003588 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003589 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003590 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003591 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3592 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003593 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3595 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003596 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003598 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003600
Scott Michelfdc40a02009-02-17 22:15:04 +00003601 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003603 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3604 // For floating-point precision of 18:
3605 //
3606 // TwoToFractionalPartOfX =
3607 // 0.999999982f +
3608 // (0.693148872f +
3609 // (0.240227044f +
3610 // (0.554906021e-1f +
3611 // (0.961591928e-2f +
3612 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3613 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003615 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003616 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003617 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003618 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3619 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003620 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003621 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3622 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003623 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003624 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3625 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003626 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003627 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3628 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003629 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003630 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3631 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003632 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003633 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003634 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003635 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003636
Scott Michelfdc40a02009-02-17 22:15:04 +00003637 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003638 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003639 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003640 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003641 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003642 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003643 getValue(I.getOperand(1)).getValueType(),
3644 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003645 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003646
Dale Johannesen601d3c02008-09-05 01:48:15 +00003647 setValue(&I, result);
3648}
3649
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003650/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3651/// limited-precision mode with x == 10.0f.
3652void
Dan Gohman46510a72010-04-15 01:51:59 +00003653SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003654 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003655 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003656 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003657 bool IsExp10 = false;
3658
Owen Anderson825b72b2009-08-11 20:47:22 +00003659 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003660 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003661 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3662 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3663 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3664 APFloat Ten(10.0f);
3665 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3666 }
3667 }
3668 }
3669
3670 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003671 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003672
3673 // Put the exponent in the right bit position for later addition to the
3674 // final result:
3675 //
3676 // #define LOG2OF10 3.3219281f
3677 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003678 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003679 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003681
3682 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3684 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003685
3686 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003687 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003688 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003689
3690 if (LimitFloatPrecision <= 6) {
3691 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003692 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003693 // twoToFractionalPartOfX =
3694 // 0.997535578f +
3695 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003696 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003697 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003698 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003699 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003700 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003701 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003702 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3703 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003704 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003705 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003706 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003707 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003708
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003709 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003710 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003711 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3712 // For floating-point precision of 12:
3713 //
3714 // TwoToFractionalPartOfX =
3715 // 0.999892986f +
3716 // (0.696457318f +
3717 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3718 //
3719 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003720 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003721 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003722 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003723 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003724 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3725 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003726 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3728 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003729 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003730 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003731 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003733
Scott Michelfdc40a02009-02-17 22:15:04 +00003734 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003735 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003736 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3737 // For floating-point precision of 18:
3738 //
3739 // TwoToFractionalPartOfX =
3740 // 0.999999982f +
3741 // (0.693148872f +
3742 // (0.240227044f +
3743 // (0.554906021e-1f +
3744 // (0.961591928e-2f +
3745 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3746 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003747 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003748 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003749 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003750 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003751 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3752 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003753 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003754 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3755 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003756 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003757 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3758 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003759 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003760 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3761 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003762 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003763 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3764 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003765 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003766 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003767 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003769
Scott Michelfdc40a02009-02-17 22:15:04 +00003770 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003771 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003772 }
3773 } else {
3774 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003775 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003776 getValue(I.getOperand(1)).getValueType(),
3777 getValue(I.getOperand(1)),
3778 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003779 }
3780
3781 setValue(&I, result);
3782}
3783
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003784
3785/// ExpandPowI - Expand a llvm.powi intrinsic.
3786static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3787 SelectionDAG &DAG) {
3788 // If RHS is a constant, we can expand this out to a multiplication tree,
3789 // otherwise we end up lowering to a call to __powidf2 (for example). When
3790 // optimizing for size, we only want to do this if the expansion would produce
3791 // a small number of multiplies, otherwise we do the full expansion.
3792 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3793 // Get the exponent as a positive value.
3794 unsigned Val = RHSC->getSExtValue();
3795 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003796
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003797 // powi(x, 0) -> 1.0
3798 if (Val == 0)
3799 return DAG.getConstantFP(1.0, LHS.getValueType());
3800
Dan Gohmanae541aa2010-04-15 04:33:49 +00003801 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003802 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3803 // If optimizing for size, don't insert too many multiplies. This
3804 // inserts up to 5 multiplies.
3805 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3806 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003807 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003808 // powi(x,15) generates one more multiply than it should), but this has
3809 // the benefit of being both really simple and much better than a libcall.
3810 SDValue Res; // Logically starts equal to 1.0
3811 SDValue CurSquare = LHS;
3812 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003813 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003814 if (Res.getNode())
3815 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3816 else
3817 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003818 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003819
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003820 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3821 CurSquare, CurSquare);
3822 Val >>= 1;
3823 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003824
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003825 // If the original was negative, invert the result, producing 1/(x*x*x).
3826 if (RHSC->getSExtValue() < 0)
3827 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3828 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3829 return Res;
3830 }
3831 }
3832
3833 // Otherwise, expand to a libcall.
3834 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3835}
3836
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003837/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
3838/// argument, create the corresponding DBG_VALUE machine instruction for it now.
3839/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003840bool
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003841SelectionDAGBuilder::EmitFuncArgumentDbgValue(const DbgValueInst &DI,
3842 const Value *V, MDNode *Variable,
Dan Gohman5d11ea32010-05-01 00:33:16 +00003843 uint64_t Offset,
3844 const SDValue &N) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003845 if (!isa<Argument>(V))
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003846 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003847
Devang Patel719f6a92010-04-29 20:40:36 +00003848 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela83ce982010-04-29 18:50:36 +00003849 // Ignore inlined function arguments here.
3850 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00003851 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00003852 return false;
3853
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003854 MachineBasicBlock *MBB = FuncInfo.MBBMap[DI.getParent()];
3855 if (MBB != &MF.front())
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003856 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003857
3858 unsigned Reg = 0;
3859 if (N.getOpcode() == ISD::CopyFromReg) {
3860 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Evan Cheng1deef272010-04-29 00:59:34 +00003861 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003862 MachineRegisterInfo &RegInfo = MF.getRegInfo();
3863 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
3864 if (PR)
3865 Reg = PR;
3866 }
3867 }
3868
Evan Chenga36acad2010-04-29 06:33:38 +00003869 if (!Reg) {
3870 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
3871 if (VMI == FuncInfo.ValueMap.end())
3872 return false;
3873 Reg = VMI->second;
3874 }
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003875
3876 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
3877 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
3878 TII->get(TargetOpcode::DBG_VALUE))
Evan Chenga36acad2010-04-29 06:33:38 +00003879 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003880 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003881 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003882}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003883
Douglas Gregor7d9663c2010-05-11 06:17:44 +00003884// VisualStudio defines setjmp as _setjmp
3885#if defined(_MSC_VER) && defined(setjmp)
3886#define setjmp_undefined_for_visual_studio
3887#undef setjmp
3888#endif
3889
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003890/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3891/// we want to emit this as a call to a named external function, return the name
3892/// otherwise lower it and return null.
3893const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003894SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003895 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003896 SDValue Res;
3897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003898 switch (Intrinsic) {
3899 default:
3900 // By default, turn this into a target intrinsic node.
3901 visitTargetIntrinsic(I, Intrinsic);
3902 return 0;
3903 case Intrinsic::vastart: visitVAStart(I); return 0;
3904 case Intrinsic::vaend: visitVAEnd(I); return 0;
3905 case Intrinsic::vacopy: visitVACopy(I); return 0;
3906 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003907 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003908 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003909 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003910 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003911 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003912 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003913 return 0;
3914 case Intrinsic::setjmp:
3915 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003916 case Intrinsic::longjmp:
3917 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003918 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003919 // Assert for address < 256 since we support only user defined address
3920 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003921 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003922 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003923 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003924 < 256 &&
3925 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003926 SDValue Op1 = getValue(I.getOperand(1));
3927 SDValue Op2 = getValue(I.getOperand(2));
3928 SDValue Op3 = getValue(I.getOperand(3));
3929 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3930 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003931 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003932 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003933 return 0;
3934 }
Chris Lattner824b9582008-11-21 16:42:48 +00003935 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003936 // Assert for address < 256 since we support only user defined address
3937 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003938 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003939 < 256 &&
3940 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003941 SDValue Op1 = getValue(I.getOperand(1));
3942 SDValue Op2 = getValue(I.getOperand(2));
3943 SDValue Op3 = getValue(I.getOperand(3));
3944 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3945 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003946 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003947 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003948 return 0;
3949 }
Chris Lattner824b9582008-11-21 16:42:48 +00003950 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003951 // Assert for address < 256 since we support only user defined address
3952 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003953 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003954 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003955 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003956 < 256 &&
3957 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003958 SDValue Op1 = getValue(I.getOperand(1));
3959 SDValue Op2 = getValue(I.getOperand(2));
3960 SDValue Op3 = getValue(I.getOperand(3));
3961 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3962 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003963
3964 // If the source and destination are known to not be aliases, we can
3965 // lower memmove as memcpy.
3966 uint64_t Size = -1ULL;
3967 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003968 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003969 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003970 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003971 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003972 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003973 return 0;
3974 }
3975
Mon P Wang20adc9d2010-04-04 03:10:48 +00003976 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003977 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003978 return 0;
3979 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003980 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00003981 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00003982 if (!DIVariable(DI.getVariable()).Verify())
Devang Patel7e1e31f2009-07-02 22:43:26 +00003983 return 0;
3984
Devang Patelac1ceb32009-10-09 22:42:28 +00003985 MDNode *Variable = DI.getVariable();
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003986 // Parameters are handled specially.
Devang Patelf38c6c82010-04-28 23:24:13 +00003987 bool isParameter =
3988 DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable;
Dan Gohman46510a72010-04-15 01:51:59 +00003989 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003990 if (!Address)
3991 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003992 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003993 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003994 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003995 if (AI) {
3996 // Don't handle byval arguments or VLAs, for example.
3997 // Non-byval arguments are handled here (they refer to the stack temporary
3998 // alloca at this point).
3999 DenseMap<const AllocaInst*, int>::iterator SI =
4000 FuncInfo.StaticAllocaMap.find(AI);
4001 if (SI == FuncInfo.StaticAllocaMap.end())
4002 return 0; // VLAs.
4003 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004004
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004005 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4006 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4007 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
4008 }
4009
4010 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4011 // but do not always have a corresponding SDNode built. The SDNodeOrder
4012 // absolute, but not relative, values are different depending on whether
4013 // debug info exists.
4014 ++SDNodeOrder;
4015 SDValue &N = NodeMap[Address];
4016 SDDbgValue *SDV;
4017 if (N.getNode()) {
4018 if (isParameter && !AI) {
4019 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4020 if (FINode)
4021 // Byval parameter. We have a frame index at this point.
4022 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4023 0, dl, SDNodeOrder);
4024 else
4025 // Can't do anything with other non-AI cases yet. This might be a
4026 // parameter of a callee function that got inlined, for example.
4027 return 0;
4028 } else if (AI)
4029 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4030 0, dl, SDNodeOrder);
4031 else
4032 // Can't do anything with other non-AI cases yet.
4033 return 0;
4034 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4035 } else {
4036 // This isn't useful, but it shows what we're missing.
4037 SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()),
4038 0, dl, SDNodeOrder);
4039 DAG.AddDbgValue(SDV, 0, isParameter);
4040 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004041 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004042 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004043 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004044 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004045 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004046 return 0;
4047
4048 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004049 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004050 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004051 if (!V)
4052 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004053
4054 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4055 // but do not always have a corresponding SDNode built. The SDNodeOrder
4056 // absolute, but not relative, values are different depending on whether
4057 // debug info exists.
4058 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004059 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00004060 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004061 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4062 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004063 } else {
Devang Pateld47f3c82010-05-05 22:29:00 +00004064 bool createUndef = false;
4065 // FIXME : Why not use getValue() directly ?
Devang Patel00190342010-03-15 19:15:44 +00004066 SDValue &N = NodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004067 if (N.getNode()) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004068 if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) {
4069 SDV = DAG.getDbgValue(Variable, N.getNode(),
4070 N.getResNo(), Offset, dl, SDNodeOrder);
4071 DAG.AddDbgValue(SDV, N.getNode(), false);
4072 }
Devang Pateld47f3c82010-05-05 22:29:00 +00004073 } else if (isa<PHINode>(V) && !V->use_empty()) {
4074 SDValue N = getValue(V);
4075 if (N.getNode()) {
4076 if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) {
4077 SDV = DAG.getDbgValue(Variable, N.getNode(),
4078 N.getResNo(), Offset, dl, SDNodeOrder);
4079 DAG.AddDbgValue(SDV, N.getNode(), false);
4080 }
4081 } else
4082 createUndef = true;
4083 } else
4084 createUndef = true;
4085 if (createUndef) {
Devang Patel00190342010-03-15 19:15:44 +00004086 // We may expand this to cover more cases. One case where we have no
4087 // data available is an unreferenced parameter; we need this fallback.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004088 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
4089 Offset, dl, SDNodeOrder);
4090 DAG.AddDbgValue(SDV, 0, false);
4091 }
Devang Patel00190342010-03-15 19:15:44 +00004092 }
4093
4094 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004095 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004096 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004097 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004098 // Don't handle byval struct arguments or VLAs, for example.
4099 if (!AI)
4100 return 0;
4101 DenseMap<const AllocaInst*, int>::iterator SI =
4102 FuncInfo.StaticAllocaMap.find(AI);
4103 if (SI == FuncInfo.StaticAllocaMap.end())
4104 return 0; // VLAs.
4105 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00004106
Chris Lattner512063d2010-04-05 06:19:28 +00004107 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4108 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4109 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004110 return 0;
4111 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004112 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004113 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00004114 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
4115 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004116 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004117 SDValue Ops[1];
4118 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004119 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004120 setValue(&I, Op);
4121 DAG.setRoot(Op.getValue(1));
4122 return 0;
4123 }
4124
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004125 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00004126 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00004127 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00004128 if (CallMBB->isLandingPad())
4129 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004130 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004131#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004132 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004133#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004134 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4135 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00004136 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004137 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004138
Chris Lattner3a5815f2009-09-17 23:54:54 +00004139 // Insert the EHSELECTION instruction.
4140 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4141 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00004142 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00004143 Ops[1] = getRoot();
4144 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004145 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004146 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004147 return 0;
4148 }
4149
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004150 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004151 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00004152 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00004153 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4154 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004155 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004156 return 0;
4157 }
4158
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004159 case Intrinsic::eh_return_i32:
4160 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004161 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
4162 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
4163 MVT::Other,
4164 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00004165 getValue(I.getOperand(1)),
4166 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004167 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004168 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004169 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004170 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004171 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00004172 EVT VT = getValue(I.getOperand(1)).getValueType();
4173 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004174 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004175 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004176 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004177 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004178 TLI.getPointerTy()),
4179 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004180 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004181 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004182 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00004183 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4184 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004185 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004186 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004187 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004188 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00004189 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00004190 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004191 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004192
Chris Lattner512063d2010-04-05 06:19:28 +00004193 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004194 return 0;
4195 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004196 case Intrinsic::eh_sjlj_setjmp: {
4197 setValue(&I, DAG.getNode(ISD::EH_SJLJ_SETJMP, dl, MVT::i32, getRoot(),
4198 getValue(I.getOperand(1))));
4199 return 0;
4200 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004201 case Intrinsic::eh_sjlj_longjmp: {
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004202 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, dl, MVT::Other,
4203 getRoot(),
Jim Grosbach5eb19512010-05-22 01:06:18 +00004204 getValue(I.getOperand(1))));
4205 return 0;
4206 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004207
Mon P Wang77cdf302008-11-10 20:54:11 +00004208 case Intrinsic::convertff:
4209 case Intrinsic::convertfsi:
4210 case Intrinsic::convertfui:
4211 case Intrinsic::convertsif:
4212 case Intrinsic::convertuif:
4213 case Intrinsic::convertss:
4214 case Intrinsic::convertsu:
4215 case Intrinsic::convertus:
4216 case Intrinsic::convertuu: {
4217 ISD::CvtCode Code = ISD::CVT_INVALID;
4218 switch (Intrinsic) {
4219 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4220 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4221 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4222 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4223 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4224 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4225 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4226 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4227 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4228 }
Owen Andersone50ed302009-08-10 22:56:29 +00004229 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00004230 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004231 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4232 DAG.getValueType(DestVT),
4233 DAG.getValueType(getValue(Op1).getValueType()),
4234 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004235 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004236 Code);
4237 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004238 return 0;
4239 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004240 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004241 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004242 getValue(I.getOperand(1)).getValueType(),
4243 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004244 return 0;
4245 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00004246 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
4247 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004248 return 0;
4249 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004250 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004251 getValue(I.getOperand(1)).getValueType(),
4252 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004253 return 0;
4254 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004255 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004256 getValue(I.getOperand(1)).getValueType(),
4257 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004258 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004259 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004260 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004261 return 0;
4262 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004263 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004264 return 0;
4265 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004266 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004267 return 0;
4268 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004269 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004270 return 0;
4271 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004272 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004273 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004274 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004275 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004276 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004277 case Intrinsic::convert_to_fp16:
4278 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004279 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004280 return 0;
4281 case Intrinsic::convert_from_fp16:
4282 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004283 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004284 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004285 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00004286 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004287 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004288 return 0;
4289 }
4290 case Intrinsic::readcyclecounter: {
4291 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004292 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4293 DAG.getVTList(MVT::i64, MVT::Other),
4294 &Op, 1);
4295 setValue(&I, Res);
4296 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004297 return 0;
4298 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004299 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004300 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004301 getValue(I.getOperand(1)).getValueType(),
4302 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004303 return 0;
4304 case Intrinsic::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004305 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004306 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004307 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004308 return 0;
4309 }
4310 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004311 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004312 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004313 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004314 return 0;
4315 }
4316 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004317 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004318 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004319 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004320 return 0;
4321 }
4322 case Intrinsic::stacksave: {
4323 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004324 Res = DAG.getNode(ISD::STACKSAVE, dl,
4325 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4326 setValue(&I, Res);
4327 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004328 return 0;
4329 }
4330 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004331 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004332 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004333 return 0;
4334 }
Bill Wendling57344502008-11-18 11:01:33 +00004335 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004336 // Emit code into the DAG to store the stack guard onto the stack.
4337 MachineFunction &MF = DAG.getMachineFunction();
4338 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004339 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004340
Eric Christopher551754c2010-04-16 23:37:20 +00004341 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4342 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004343
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004344 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004345 MFI->setStackProtectorIndex(FI);
4346
4347 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4348
4349 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004350 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4351 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004352 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004353 setValue(&I, Res);
4354 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004355 return 0;
4356 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004357 case Intrinsic::objectsize: {
4358 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004359 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004360
4361 assert(CI && "Non-constant type in __builtin_object_size?");
4362
Eric Christopher551754c2010-04-16 23:37:20 +00004363 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004364 EVT Ty = Arg.getValueType();
4365
Eric Christopherd060b252009-12-23 02:51:48 +00004366 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004367 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004368 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004369 Res = DAG.getConstant(0, Ty);
4370
4371 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004372 return 0;
4373 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004374 case Intrinsic::var_annotation:
4375 // Discard annotate attributes
4376 return 0;
4377
4378 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004379 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004380
4381 SDValue Ops[6];
4382 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004383 Ops[1] = getValue(I.getOperand(1));
4384 Ops[2] = getValue(I.getOperand(2));
4385 Ops[3] = getValue(I.getOperand(3));
4386 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004387 Ops[5] = DAG.getSrcValue(F);
4388
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004389 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4390 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4391 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004393 setValue(&I, Res);
4394 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004395 return 0;
4396 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397 case Intrinsic::gcroot:
4398 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004399 const Value *Alloca = I.getOperand(1);
4400 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004401
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004402 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4403 GFI->addStackRoot(FI->getIndex(), TypeMap);
4404 }
4405 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406 case Intrinsic::gcread:
4407 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004408 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004409 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004410 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004411 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004412 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004413 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004414 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004415 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004416 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004417 return implVisitAluOverflow(I, ISD::UADDO);
4418 case Intrinsic::sadd_with_overflow:
4419 return implVisitAluOverflow(I, ISD::SADDO);
4420 case Intrinsic::usub_with_overflow:
4421 return implVisitAluOverflow(I, ISD::USUBO);
4422 case Intrinsic::ssub_with_overflow:
4423 return implVisitAluOverflow(I, ISD::SSUBO);
4424 case Intrinsic::umul_with_overflow:
4425 return implVisitAluOverflow(I, ISD::UMULO);
4426 case Intrinsic::smul_with_overflow:
4427 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004429 case Intrinsic::prefetch: {
4430 SDValue Ops[4];
4431 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004432 Ops[1] = getValue(I.getOperand(1));
4433 Ops[2] = getValue(I.getOperand(2));
4434 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004435 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004436 return 0;
4437 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004438
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004439 case Intrinsic::memory_barrier: {
4440 SDValue Ops[6];
4441 Ops[0] = getRoot();
4442 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004443 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004444
Bill Wendling4533cac2010-01-28 21:51:40 +00004445 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004446 return 0;
4447 }
4448 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004449 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004450 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004451 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004452 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004453 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004454 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004455 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004456 getValue(I.getOperand(3)),
4457 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004458 setValue(&I, L);
4459 DAG.setRoot(L.getValue(1));
4460 return 0;
4461 }
4462 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004463 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004464 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004465 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004466 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004467 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004468 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004469 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004470 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004471 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004472 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004473 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004474 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004475 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004476 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004477 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004478 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004479 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004480 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004481 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004482 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004483 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004484
4485 case Intrinsic::invariant_start:
4486 case Intrinsic::lifetime_start:
4487 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004488 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004489 return 0;
4490 case Intrinsic::invariant_end:
4491 case Intrinsic::lifetime_end:
4492 // Discard region information.
4493 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004494 }
4495}
4496
Dan Gohman46510a72010-04-15 01:51:59 +00004497void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004498 bool isTailCall,
4499 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004500 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4501 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004502 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004503 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004504 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004505
4506 TargetLowering::ArgListTy Args;
4507 TargetLowering::ArgListEntry Entry;
4508 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004509
4510 // Check whether the function can return without sret-demotion.
4511 SmallVector<EVT, 4> OutVTs;
4512 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4513 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004514 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004515 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004516
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004517 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004518 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4519
4520 SDValue DemoteStackSlot;
4521
4522 if (!CanLowerReturn) {
4523 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4524 FTy->getReturnType());
4525 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4526 FTy->getReturnType());
4527 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004528 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004529 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4530
4531 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4532 Entry.Node = DemoteStackSlot;
4533 Entry.Ty = StackSlotPtrType;
4534 Entry.isSExt = false;
4535 Entry.isZExt = false;
4536 Entry.isInReg = false;
4537 Entry.isSRet = true;
4538 Entry.isNest = false;
4539 Entry.isByVal = false;
4540 Entry.Alignment = Align;
4541 Args.push_back(Entry);
4542 RetTy = Type::getVoidTy(FTy->getContext());
4543 }
4544
Dan Gohman46510a72010-04-15 01:51:59 +00004545 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004546 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004547 SDValue ArgNode = getValue(*i);
4548 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4549
4550 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004551 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4552 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4553 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4554 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4555 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4556 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004557 Entry.Alignment = CS.getParamAlignment(attrInd);
4558 Args.push_back(Entry);
4559 }
4560
Chris Lattner512063d2010-04-05 06:19:28 +00004561 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004562 // Insert a label before the invoke call to mark the try range. This can be
4563 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004564 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004565
Jim Grosbachca752c92010-01-28 01:45:32 +00004566 // For SjLj, keep track of which landing pads go with which invokes
4567 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004568 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004569 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004570 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004571 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004572 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004573 }
4574
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004575 // Both PendingLoads and PendingExports must be flushed here;
4576 // this call might not return.
4577 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004578 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004579 }
4580
Dan Gohman98ca4f22009-08-05 01:29:28 +00004581 // Check if target-independent constraints permit a tail call here.
4582 // Target-dependent constraints are checked within TLI.LowerCallTo.
4583 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004584 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004585 isTailCall = false;
4586
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004587 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004588 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004589 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004590 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004591 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004592 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004593 isTailCall,
4594 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004595 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004596 assert((isTailCall || Result.second.getNode()) &&
4597 "Non-null chain expected with non-tail call!");
4598 assert((Result.second.getNode() || !Result.first.getNode()) &&
4599 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004600 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004601 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004602 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004603 // The instruction result is the result of loading from the
4604 // hidden sret parameter.
4605 SmallVector<EVT, 1> PVTs;
4606 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4607
4608 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4609 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4610 EVT PtrVT = PVTs[0];
4611 unsigned NumValues = OutVTs.size();
4612 SmallVector<SDValue, 4> Values(NumValues);
4613 SmallVector<SDValue, 4> Chains(NumValues);
4614
4615 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004616 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4617 DemoteStackSlot,
4618 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004619 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004620 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004621 Values[i] = L;
4622 Chains[i] = L.getValue(1);
4623 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004624
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004625 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4626 MVT::Other, &Chains[0], NumValues);
4627 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004628
4629 // Collect the legal value parts into potentially illegal values
4630 // that correspond to the original function's return values.
4631 SmallVector<EVT, 4> RetTys;
4632 RetTy = FTy->getReturnType();
4633 ComputeValueVTs(TLI, RetTy, RetTys);
4634 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4635 SmallVector<SDValue, 4> ReturnValues;
4636 unsigned CurReg = 0;
4637 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4638 EVT VT = RetTys[I];
4639 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4640 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4641
4642 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004643 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004644 RegisterVT, VT, AssertOp);
4645 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004646 CurReg += NumRegs;
4647 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004648
Bill Wendling4533cac2010-01-28 21:51:40 +00004649 setValue(CS.getInstruction(),
4650 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4651 DAG.getVTList(&RetTys[0], RetTys.size()),
4652 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004653
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004654 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004655
4656 // As a special case, a null chain means that a tail call has been emitted and
4657 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004658 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004659 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004660 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004661 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004662
Chris Lattner512063d2010-04-05 06:19:28 +00004663 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004664 // Insert a label at the end of the invoke call to mark the try range. This
4665 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004666 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004667 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668
4669 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004670 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004671 }
4672}
4673
Chris Lattner8047d9a2009-12-24 00:37:38 +00004674/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4675/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004676static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4677 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004678 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004679 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004680 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004681 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004682 if (C->isNullValue())
4683 continue;
4684 // Unknown instruction.
4685 return false;
4686 }
4687 return true;
4688}
4689
Dan Gohman46510a72010-04-15 01:51:59 +00004690static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4691 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004692 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004693
Chris Lattner8047d9a2009-12-24 00:37:38 +00004694 // Check to see if this load can be trivially constant folded, e.g. if the
4695 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004696 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004697 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004698 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004699 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004700
Dan Gohman46510a72010-04-15 01:51:59 +00004701 if (const Constant *LoadCst =
4702 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4703 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004704 return Builder.getValue(LoadCst);
4705 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004706
Chris Lattner8047d9a2009-12-24 00:37:38 +00004707 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4708 // still constant memory, the input chain can be the entry node.
4709 SDValue Root;
4710 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004711
Chris Lattner8047d9a2009-12-24 00:37:38 +00004712 // Do not serialize (non-volatile) loads of constant memory with anything.
4713 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4714 Root = Builder.DAG.getEntryNode();
4715 ConstantMemory = true;
4716 } else {
4717 // Do not serialize non-volatile loads against each other.
4718 Root = Builder.DAG.getRoot();
4719 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004720
Chris Lattner8047d9a2009-12-24 00:37:38 +00004721 SDValue Ptr = Builder.getValue(PtrVal);
4722 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4723 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004724 false /*volatile*/,
4725 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004726
Chris Lattner8047d9a2009-12-24 00:37:38 +00004727 if (!ConstantMemory)
4728 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4729 return LoadVal;
4730}
4731
4732
4733/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4734/// If so, return true and lower it, otherwise return false and it will be
4735/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004736bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004737 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4738 if (I.getNumOperands() != 4)
4739 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004740
Eric Christopher551754c2010-04-16 23:37:20 +00004741 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004742 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004743 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004744 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004745 return false;
4746
Eric Christopher551754c2010-04-16 23:37:20 +00004747 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004748
Chris Lattner8047d9a2009-12-24 00:37:38 +00004749 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4750 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004751 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4752 bool ActuallyDoIt = true;
4753 MVT LoadVT;
4754 const Type *LoadTy;
4755 switch (Size->getZExtValue()) {
4756 default:
4757 LoadVT = MVT::Other;
4758 LoadTy = 0;
4759 ActuallyDoIt = false;
4760 break;
4761 case 2:
4762 LoadVT = MVT::i16;
4763 LoadTy = Type::getInt16Ty(Size->getContext());
4764 break;
4765 case 4:
4766 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004767 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004768 break;
4769 case 8:
4770 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004771 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004772 break;
4773 /*
4774 case 16:
4775 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004776 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004777 LoadTy = VectorType::get(LoadTy, 4);
4778 break;
4779 */
4780 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004781
Chris Lattner04b091a2009-12-24 01:07:17 +00004782 // This turns into unaligned loads. We only do this if the target natively
4783 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4784 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004785
Chris Lattner04b091a2009-12-24 01:07:17 +00004786 // Require that we can find a legal MVT, and only do this if the target
4787 // supports unaligned loads of that type. Expanding into byte loads would
4788 // bloat the code.
4789 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4790 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4791 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4792 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4793 ActuallyDoIt = false;
4794 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004795
Chris Lattner04b091a2009-12-24 01:07:17 +00004796 if (ActuallyDoIt) {
4797 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4798 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004799
Chris Lattner04b091a2009-12-24 01:07:17 +00004800 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4801 ISD::SETNE);
4802 EVT CallVT = TLI.getValueType(I.getType(), true);
4803 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4804 return true;
4805 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004806 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004807
4808
Chris Lattner8047d9a2009-12-24 00:37:38 +00004809 return false;
4810}
4811
4812
Dan Gohman46510a72010-04-15 01:51:59 +00004813void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004814 const char *RenameFn = 0;
4815 if (Function *F = I.getCalledFunction()) {
4816 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004817 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004818 if (II) {
4819 if (unsigned IID = II->getIntrinsicID(F)) {
4820 RenameFn = visitIntrinsicCall(I, IID);
4821 if (!RenameFn)
4822 return;
4823 }
4824 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004825 if (unsigned IID = F->getIntrinsicID()) {
4826 RenameFn = visitIntrinsicCall(I, IID);
4827 if (!RenameFn)
4828 return;
4829 }
4830 }
4831
4832 // Check for well-known libc/libm calls. If the function is internal, it
4833 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004834 if (!F->hasLocalLinkage() && F->hasName()) {
4835 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004836 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004837 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004838 I.getOperand(1)->getType()->isFloatingPointTy() &&
4839 I.getType() == I.getOperand(1)->getType() &&
4840 I.getType() == I.getOperand(2)->getType()) {
4841 SDValue LHS = getValue(I.getOperand(1));
4842 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004843 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4844 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004845 return;
4846 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004847 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004848 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004849 I.getOperand(1)->getType()->isFloatingPointTy() &&
4850 I.getType() == I.getOperand(1)->getType()) {
4851 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004852 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4853 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854 return;
4855 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004856 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004857 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004858 I.getOperand(1)->getType()->isFloatingPointTy() &&
4859 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004860 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004861 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004862 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4863 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004864 return;
4865 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004866 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004867 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004868 I.getOperand(1)->getType()->isFloatingPointTy() &&
4869 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004870 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004871 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004872 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4873 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004874 return;
4875 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004876 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4877 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004878 I.getOperand(1)->getType()->isFloatingPointTy() &&
4879 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004880 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004881 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004882 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4883 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004884 return;
4885 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004886 } else if (Name == "memcmp") {
4887 if (visitMemCmpCall(I))
4888 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004889 }
4890 }
Eric Christopher551754c2010-04-16 23:37:20 +00004891 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004892 visitInlineAsm(&I);
4893 return;
4894 }
4895
4896 SDValue Callee;
4897 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004898 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004899 else
Bill Wendling056292f2008-09-16 21:48:12 +00004900 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004901
Bill Wendling0d580132009-12-23 01:28:19 +00004902 // Check if we can potentially perform a tail call. More detailed checking is
4903 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004904 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004905}
4906
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004907namespace llvm {
Dan Gohman462f6b52010-05-29 17:53:24 +00004908
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004909/// AsmOperandInfo - This contains information for each constraint that we are
4910/// lowering.
Duncan Sands16d8f8b2010-05-11 20:16:09 +00004911class LLVM_LIBRARY_VISIBILITY SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004912 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004913public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004914 /// CallOperand - If this is the result output operand or a clobber
4915 /// this is null, otherwise it is the incoming operand to the CallInst.
4916 /// This gets modified as the asm is processed.
4917 SDValue CallOperand;
4918
4919 /// AssignedRegs - If this is a register or register class operand, this
4920 /// contains the set of register corresponding to the operand.
4921 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004922
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004923 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4924 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4925 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004926
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004927 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4928 /// busy in OutputRegs/InputRegs.
4929 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004930 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004931 std::set<unsigned> &InputRegs,
4932 const TargetRegisterInfo &TRI) const {
4933 if (isOutReg) {
4934 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4935 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4936 }
4937 if (isInReg) {
4938 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4939 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4940 }
4941 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004942
Owen Andersone50ed302009-08-10 22:56:29 +00004943 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004944 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004945 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004946 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004947 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004948 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004949 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004950
Chris Lattner81249c92008-10-17 17:05:25 +00004951 if (isa<BasicBlock>(CallOperandVal))
4952 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004953
Chris Lattner81249c92008-10-17 17:05:25 +00004954 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004955
Chris Lattner81249c92008-10-17 17:05:25 +00004956 // If this is an indirect operand, the operand is a pointer to the
4957 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004958 if (isIndirect) {
4959 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4960 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004961 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004962 OpTy = PtrTy->getElementType();
4963 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004964
Chris Lattner81249c92008-10-17 17:05:25 +00004965 // If OpTy is not a single value, it may be a struct/union that we
4966 // can tile with integers.
4967 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4968 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4969 switch (BitSize) {
4970 default: break;
4971 case 1:
4972 case 8:
4973 case 16:
4974 case 32:
4975 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004976 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004977 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004978 break;
4979 }
4980 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004981
Chris Lattner81249c92008-10-17 17:05:25 +00004982 return TLI.getValueType(OpTy, true);
4983 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004985private:
4986 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4987 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004988 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004989 const TargetRegisterInfo &TRI) {
4990 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4991 Regs.insert(Reg);
4992 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4993 for (; *Aliases; ++Aliases)
4994 Regs.insert(*Aliases);
4995 }
4996};
Dan Gohman462f6b52010-05-29 17:53:24 +00004997
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004998} // end llvm namespace.
4999
Dan Gohman462f6b52010-05-29 17:53:24 +00005000/// isAllocatableRegister - If the specified register is safe to allocate,
5001/// i.e. it isn't a stack pointer or some other special register, return the
5002/// register class for the register. Otherwise, return null.
5003static const TargetRegisterClass *
5004isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5005 const TargetLowering &TLI,
5006 const TargetRegisterInfo *TRI) {
5007 EVT FoundVT = MVT::Other;
5008 const TargetRegisterClass *FoundRC = 0;
5009 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5010 E = TRI->regclass_end(); RCI != E; ++RCI) {
5011 EVT ThisVT = MVT::Other;
5012
5013 const TargetRegisterClass *RC = *RCI;
5014 // If none of the value types for this register class are valid, we
5015 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5016 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5017 I != E; ++I) {
5018 if (TLI.isTypeLegal(*I)) {
5019 // If we have already found this register in a different register class,
5020 // choose the one with the largest VT specified. For example, on
5021 // PowerPC, we favor f64 register classes over f32.
5022 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
5023 ThisVT = *I;
5024 break;
5025 }
5026 }
5027 }
5028
5029 if (ThisVT == MVT::Other) continue;
5030
5031 // NOTE: This isn't ideal. In particular, this might allocate the
5032 // frame pointer in functions that need it (due to them not being taken
5033 // out of allocation, because a variable sized allocation hasn't been seen
5034 // yet). This is a slight code pessimization, but should still work.
5035 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5036 E = RC->allocation_order_end(MF); I != E; ++I)
5037 if (*I == Reg) {
5038 // We found a matching register class. Keep looking at others in case
5039 // we find one with larger registers that this physreg is also in.
5040 FoundRC = RC;
5041 FoundVT = ThisVT;
5042 break;
5043 }
5044 }
5045 return FoundRC;
5046}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005047
5048/// GetRegistersForValue - Assign registers (virtual or physical) for the
5049/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005050/// register allocator to handle the assignment process. However, if the asm
5051/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005052/// allocation. This produces generally horrible, but correct, code.
5053///
5054/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005055/// Input and OutputRegs are the set of already allocated physical registers.
5056///
Dan Gohman2048b852009-11-23 18:04:58 +00005057void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005058GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005059 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005060 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005061 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005062
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005063 // Compute whether this value requires an input register, an output register,
5064 // or both.
5065 bool isOutReg = false;
5066 bool isInReg = false;
5067 switch (OpInfo.Type) {
5068 case InlineAsm::isOutput:
5069 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005070
5071 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005072 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005073 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005074 break;
5075 case InlineAsm::isInput:
5076 isInReg = true;
5077 isOutReg = false;
5078 break;
5079 case InlineAsm::isClobber:
5080 isOutReg = true;
5081 isInReg = true;
5082 break;
5083 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005084
5085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005086 MachineFunction &MF = DAG.getMachineFunction();
5087 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005088
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005089 // If this is a constraint for a single physreg, or a constraint for a
5090 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005091 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005092 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5093 OpInfo.ConstraintVT);
5094
5095 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005097 // If this is a FP input in an integer register (or visa versa) insert a bit
5098 // cast of the input value. More generally, handle any case where the input
5099 // value disagrees with the register class we plan to stick this in.
5100 if (OpInfo.Type == InlineAsm::isInput &&
5101 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005102 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005103 // types are identical size, use a bitcast to convert (e.g. two differing
5104 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005105 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005106 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005107 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005108 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005109 OpInfo.ConstraintVT = RegVT;
5110 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5111 // If the input is a FP value and we want it in FP registers, do a
5112 // bitcast to the corresponding integer type. This turns an f64 value
5113 // into i64, which can be passed with two i32 values on a 32-bit
5114 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005115 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005116 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005117 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005118 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005119 OpInfo.ConstraintVT = RegVT;
5120 }
5121 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005122
Owen Anderson23b9b192009-08-12 00:36:31 +00005123 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005124 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005125
Owen Andersone50ed302009-08-10 22:56:29 +00005126 EVT RegVT;
5127 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005128
5129 // If this is a constraint for a specific physical register, like {r17},
5130 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005131 if (unsigned AssignedReg = PhysReg.first) {
5132 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005133 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005134 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005135
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005136 // Get the actual register value type. This is important, because the user
5137 // may have asked for (e.g.) the AX register in i32 type. We need to
5138 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005139 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005141 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005142 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005143
5144 // If this is an expanded reference, add the rest of the regs to Regs.
5145 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005146 TargetRegisterClass::iterator I = RC->begin();
5147 for (; *I != AssignedReg; ++I)
5148 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005149
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005150 // Already added the first reg.
5151 --NumRegs; ++I;
5152 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005153 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005154 Regs.push_back(*I);
5155 }
5156 }
Bill Wendling651ad132009-12-22 01:25:10 +00005157
Dan Gohman7451d3e2010-05-29 17:03:36 +00005158 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005159 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5160 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5161 return;
5162 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005163
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005164 // Otherwise, if this was a reference to an LLVM register class, create vregs
5165 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005166 if (const TargetRegisterClass *RC = PhysReg.second) {
5167 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005168 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005169 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005170
Evan Chengfb112882009-03-23 08:01:15 +00005171 // Create the appropriate number of virtual registers.
5172 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5173 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005174 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005175
Dan Gohman7451d3e2010-05-29 17:03:36 +00005176 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005177 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005178 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005179
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005180 // This is a reference to a register class that doesn't directly correspond
5181 // to an LLVM register class. Allocate NumRegs consecutive, available,
5182 // registers from the class.
5183 std::vector<unsigned> RegClassRegs
5184 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5185 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005186
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005187 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5188 unsigned NumAllocated = 0;
5189 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5190 unsigned Reg = RegClassRegs[i];
5191 // See if this register is available.
5192 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5193 (isInReg && InputRegs.count(Reg))) { // Already used.
5194 // Make sure we find consecutive registers.
5195 NumAllocated = 0;
5196 continue;
5197 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005198
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005199 // Check to see if this register is allocatable (i.e. don't give out the
5200 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005201 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5202 if (!RC) { // Couldn't allocate this register.
5203 // Reset NumAllocated to make sure we return consecutive registers.
5204 NumAllocated = 0;
5205 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005206 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005207
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005208 // Okay, this register is good, we can use it.
5209 ++NumAllocated;
5210
5211 // If we allocated enough consecutive registers, succeed.
5212 if (NumAllocated == NumRegs) {
5213 unsigned RegStart = (i-NumAllocated)+1;
5214 unsigned RegEnd = i+1;
5215 // Mark all of the allocated registers used.
5216 for (unsigned i = RegStart; i != RegEnd; ++i)
5217 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005218
Dan Gohman7451d3e2010-05-29 17:03:36 +00005219 OpInfo.AssignedRegs = RegsForValue(Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 OpInfo.ConstraintVT);
5221 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5222 return;
5223 }
5224 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005225
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005226 // Otherwise, we couldn't allocate enough registers for this.
5227}
5228
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005229/// visitInlineAsm - Handle a call to an InlineAsm object.
5230///
Dan Gohman46510a72010-04-15 01:51:59 +00005231void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5232 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005233
5234 /// ConstraintOperands - Information about all of the constraints.
5235 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005236
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005237 std::set<unsigned> OutputRegs, InputRegs;
5238
5239 // Do a prepass over the constraints, canonicalizing them, and building up the
5240 // ConstraintOperands list.
5241 std::vector<InlineAsm::ConstraintInfo>
5242 ConstraintInfos = IA->ParseConstraints();
5243
Evan Chengda43bcf2008-09-24 00:05:32 +00005244 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005245
Chris Lattner6c147292009-04-30 00:48:50 +00005246 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005247
Chris Lattner6c147292009-04-30 00:48:50 +00005248 // We won't need to flush pending loads if this asm doesn't touch
5249 // memory and is nonvolatile.
5250 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005251 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005252 else
5253 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005254
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005255 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5256 unsigned ResNo = 0; // ResNo - The result number of the next output.
5257 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5258 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5259 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005260
Owen Anderson825b72b2009-08-11 20:47:22 +00005261 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262
5263 // Compute the value type for each operand.
5264 switch (OpInfo.Type) {
5265 case InlineAsm::isOutput:
5266 // Indirect outputs just consume an argument.
5267 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005268 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005269 break;
5270 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005271
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005272 // The return value of the call is this value. As such, there is no
5273 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005274 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005275 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005276 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5277 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5278 } else {
5279 assert(ResNo == 0 && "Asm only has one result!");
5280 OpVT = TLI.getValueType(CS.getType());
5281 }
5282 ++ResNo;
5283 break;
5284 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005285 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005286 break;
5287 case InlineAsm::isClobber:
5288 // Nothing to do.
5289 break;
5290 }
5291
5292 // If this is an input or an indirect output, process the call argument.
5293 // BasicBlocks are labels, currently appearing only in asm's.
5294 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005295 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005296 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5297
Dan Gohman46510a72010-04-15 01:51:59 +00005298 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005299 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005300 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005301 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005302 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005303
Owen Anderson1d0be152009-08-13 21:58:54 +00005304 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005307 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005308 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005309
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005310 // Second pass over the constraints: compute which constraint option to use
5311 // and assign registers to constraints that want a specific physreg.
5312 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5313 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005314
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005315 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005316 // matching input. If their types mismatch, e.g. one is an integer, the
5317 // other is floating point, or their sizes are different, flag it as an
5318 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005319 if (OpInfo.hasMatchingInput()) {
5320 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005321
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005322 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005323 if ((OpInfo.ConstraintVT.isInteger() !=
5324 Input.ConstraintVT.isInteger()) ||
5325 (OpInfo.ConstraintVT.getSizeInBits() !=
5326 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005327 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005328 " with a matching output constraint of"
5329 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005330 }
5331 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005332 }
5333 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005336 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005337
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005338 // If this is a memory input, and if the operand is not indirect, do what we
5339 // need to to provide an address for the memory input.
5340 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5341 !OpInfo.isIndirect) {
5342 assert(OpInfo.Type == InlineAsm::isInput &&
5343 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005344
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005345 // Memory operands really want the address of the value. If we don't have
5346 // an indirect input, put it in the constpool if we can, otherwise spill
5347 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005348
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005349 // If the operand is a float, integer, or vector constant, spill to a
5350 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005351 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5353 isa<ConstantVector>(OpVal)) {
5354 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5355 TLI.getPointerTy());
5356 } else {
5357 // Otherwise, create a stack slot and emit a store to it before the
5358 // asm.
5359 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005360 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005361 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5362 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005363 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005364 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005365 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005366 OpInfo.CallOperand, StackSlot, NULL, 0,
5367 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005368 OpInfo.CallOperand = StackSlot;
5369 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005370
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005371 // There is no longer a Value* corresponding to this operand.
5372 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005373
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005374 // It is now an indirect operand.
5375 OpInfo.isIndirect = true;
5376 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005377
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005378 // If this constraint is for a specific register, allocate it before
5379 // anything else.
5380 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005381 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005382 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005383
Bill Wendling651ad132009-12-22 01:25:10 +00005384 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005385
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005386 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005387 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005388 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5389 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005390
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005391 // C_Register operands have already been allocated, Other/Memory don't need
5392 // to be.
5393 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005394 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005395 }
5396
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005397 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5398 std::vector<SDValue> AsmNodeOperands;
5399 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5400 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005401 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5402 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005403
Chris Lattnerdecc2672010-04-07 05:20:54 +00005404 // If we have a !srcloc metadata node associated with it, we want to attach
5405 // this to the ultimately generated inline asm machineinstr. To do this, we
5406 // pass in the third operand as this (potentially null) inline asm MDNode.
5407 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5408 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005409
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005410 // Loop over all of the inputs, copying the operand values into the
5411 // appropriate registers and processing the output regs.
5412 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005413
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005414 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5415 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005416
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5418 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5419
5420 switch (OpInfo.Type) {
5421 case InlineAsm::isOutput: {
5422 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5423 OpInfo.ConstraintType != TargetLowering::C_Register) {
5424 // Memory output, or 'other' output (e.g. 'X' constraint).
5425 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5426
5427 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005428 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5429 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005430 TLI.getPointerTy()));
5431 AsmNodeOperands.push_back(OpInfo.CallOperand);
5432 break;
5433 }
5434
5435 // Otherwise, this is a register or register class output.
5436
5437 // Copy the output from the appropriate register. Find a register that
5438 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005439 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005440 report_fatal_error("Couldn't allocate output reg for constraint '" +
5441 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005442
5443 // If this is an indirect operand, store through the pointer after the
5444 // asm.
5445 if (OpInfo.isIndirect) {
5446 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5447 OpInfo.CallOperandVal));
5448 } else {
5449 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005450 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005451 // Concatenate this output onto the outputs list.
5452 RetValRegs.append(OpInfo.AssignedRegs);
5453 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005454
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005455 // Add information to the INLINEASM node to know that this register is
5456 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005457 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005458 InlineAsm::Kind_RegDefEarlyClobber :
5459 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005460 false,
5461 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005462 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005463 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005464 break;
5465 }
5466 case InlineAsm::isInput: {
5467 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005468
Chris Lattner6bdcda32008-10-17 16:47:46 +00005469 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005470 // If this is required to match an output register we have already set,
5471 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005472 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 // Scan until we find the definition we already emitted of this operand.
5475 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005476 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005477 for (; OperandNo; --OperandNo) {
5478 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005479 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005480 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005481 assert((InlineAsm::isRegDefKind(OpFlag) ||
5482 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5483 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005484 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005485 }
5486
Evan Cheng697cbbf2009-03-20 18:03:34 +00005487 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005488 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005489 if (InlineAsm::isRegDefKind(OpFlag) ||
5490 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005491 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005492 if (OpInfo.isIndirect) {
5493 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005494 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005495 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5496 " don't know how to handle tied "
5497 "indirect register inputs");
5498 }
5499
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005500 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005501 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005502 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005503 MatchedRegs.RegVTs.push_back(RegVT);
5504 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005505 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005506 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005507 MatchedRegs.Regs.push_back
5508 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005509
5510 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005511 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005512 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005513 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005514 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005515 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005516 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005517 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005518
5519 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5520 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5521 "Unexpected number of operands");
5522 // Add information to the INLINEASM node to know about this input.
5523 // See InlineAsm.h isUseOperandTiedToDef.
5524 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5525 OpInfo.getMatchedOperand());
5526 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5527 TLI.getPointerTy()));
5528 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5529 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005530 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005531
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005532 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005533 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005534 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005535
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005536 std::vector<SDValue> Ops;
5537 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005538 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005539 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005540 report_fatal_error("Invalid operand for inline asm constraint '" +
5541 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005542
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005543 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005544 unsigned ResOpType =
5545 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005546 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005547 TLI.getPointerTy()));
5548 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5549 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005550 }
5551
5552 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005553 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5554 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5555 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005556
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005557 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005558 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005559 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005560 TLI.getPointerTy()));
5561 AsmNodeOperands.push_back(InOperandVal);
5562 break;
5563 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005564
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005565 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5566 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5567 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005568 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005569 "Don't know how to handle indirect register inputs yet!");
5570
5571 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005572 if (OpInfo.AssignedRegs.Regs.empty() ||
Dan Gohman7451d3e2010-05-29 17:03:36 +00005573 !OpInfo.AssignedRegs.areValueTypesLegal(TLI))
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005574 report_fatal_error("Couldn't allocate input reg for constraint '" +
5575 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005576
Dale Johannesen66978ee2009-01-31 02:22:37 +00005577 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005578 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005579
Chris Lattnerdecc2672010-04-07 05:20:54 +00005580 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005581 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005582 break;
5583 }
5584 case InlineAsm::isClobber: {
5585 // Add the clobbered value to the operand list, so that the register
5586 // allocator is aware that the physreg got clobbered.
5587 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005588 OpInfo.AssignedRegs.AddInlineAsmOperands(
5589 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005590 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005591 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005592 break;
5593 }
5594 }
5595 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005596
Chris Lattnerdecc2672010-04-07 05:20:54 +00005597 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005598 AsmNodeOperands[0] = Chain;
5599 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005600
Dale Johannesen66978ee2009-01-31 02:22:37 +00005601 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005602 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005603 &AsmNodeOperands[0], AsmNodeOperands.size());
5604 Flag = Chain.getValue(1);
5605
5606 // If this asm returns a register value, copy the result from that register
5607 // and set it as the value of the call.
5608 if (!RetValRegs.Regs.empty()) {
Dan Gohman7451d3e2010-05-29 17:03:36 +00005609 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005610 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005611
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005612 // FIXME: Why don't we do this for inline asms with MRVs?
5613 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005614 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005615
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005616 // If any of the results of the inline asm is a vector, it may have the
5617 // wrong width/num elts. This can happen for register classes that can
5618 // contain multiple different value types. The preg or vreg allocated may
5619 // not have the same VT as was expected. Convert it to the right type
5620 // with bit_convert.
5621 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005622 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005623 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005624
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005625 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005626 ResultType.isInteger() && Val.getValueType().isInteger()) {
5627 // If a result value was tied to an input value, the computed result may
5628 // have a wider width than the expected result. Extract the relevant
5629 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005630 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005631 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005632
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005633 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005634 }
Dan Gohman95915732008-10-18 01:03:45 +00005635
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005636 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005637 // Don't need to use this as a chain in this case.
5638 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5639 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005640 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005641
Dan Gohman46510a72010-04-15 01:51:59 +00005642 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005643
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005644 // Process indirect outputs, first output all of the flagged copies out of
5645 // physregs.
5646 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5647 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005648 const Value *Ptr = IndirectStoresToEmit[i].second;
Dan Gohman7451d3e2010-05-29 17:03:36 +00005649 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005650 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005651 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5652 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005653
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 // Emit the non-flagged stores from the physregs.
5655 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005656 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5657 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5658 StoresToEmit[i].first,
5659 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005660 StoresToEmit[i].second, 0,
5661 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005662 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005663 }
5664
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005665 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005667 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005668
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005669 DAG.setRoot(Chain);
5670}
5671
Dan Gohman46510a72010-04-15 01:51:59 +00005672void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005673 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5674 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005675 getValue(I.getOperand(1)),
5676 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677}
5678
Dan Gohman46510a72010-04-15 01:51:59 +00005679void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005680 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5681 getRoot(), getValue(I.getOperand(0)),
5682 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005683 setValue(&I, V);
5684 DAG.setRoot(V.getValue(1));
5685}
5686
Dan Gohman46510a72010-04-15 01:51:59 +00005687void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005688 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5689 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005690 getValue(I.getOperand(1)),
5691 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005692}
5693
Dan Gohman46510a72010-04-15 01:51:59 +00005694void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005695 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5696 MVT::Other, getRoot(),
5697 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005698 getValue(I.getOperand(2)),
5699 DAG.getSrcValue(I.getOperand(1)),
5700 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005701}
5702
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005703/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005704/// implementation, which just calls LowerCall.
5705/// FIXME: When all targets are
5706/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005707std::pair<SDValue, SDValue>
5708TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5709 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005710 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005711 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005712 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005713 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005714 ArgListTy &Args, SelectionDAG &DAG,
5715 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005716 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005717 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005718 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005719 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005720 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5721 for (unsigned Value = 0, NumValues = ValueVTs.size();
5722 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005723 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005724 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005725 SDValue Op = SDValue(Args[i].Node.getNode(),
5726 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005727 ISD::ArgFlagsTy Flags;
5728 unsigned OriginalAlignment =
5729 getTargetData()->getABITypeAlignment(ArgTy);
5730
5731 if (Args[i].isZExt)
5732 Flags.setZExt();
5733 if (Args[i].isSExt)
5734 Flags.setSExt();
5735 if (Args[i].isInReg)
5736 Flags.setInReg();
5737 if (Args[i].isSRet)
5738 Flags.setSRet();
5739 if (Args[i].isByVal) {
5740 Flags.setByVal();
5741 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5742 const Type *ElementTy = Ty->getElementType();
5743 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005744 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005745 // For ByVal, alignment should come from FE. BE will guess if this
5746 // info is not there but there are cases it cannot get right.
5747 if (Args[i].Alignment)
5748 FrameAlign = Args[i].Alignment;
5749 Flags.setByValAlign(FrameAlign);
5750 Flags.setByValSize(FrameSize);
5751 }
5752 if (Args[i].isNest)
5753 Flags.setNest();
5754 Flags.setOrigAlign(OriginalAlignment);
5755
Owen Anderson23b9b192009-08-12 00:36:31 +00005756 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5757 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005758 SmallVector<SDValue, 4> Parts(NumParts);
5759 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5760
5761 if (Args[i].isSExt)
5762 ExtendKind = ISD::SIGN_EXTEND;
5763 else if (Args[i].isZExt)
5764 ExtendKind = ISD::ZERO_EXTEND;
5765
Bill Wendling46ada192010-03-02 01:55:18 +00005766 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005767 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005768
Dan Gohman98ca4f22009-08-05 01:29:28 +00005769 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005770 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005771 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5772 if (NumParts > 1 && j == 0)
5773 MyFlags.Flags.setSplit();
5774 else if (j != 0)
5775 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005776
Dan Gohman98ca4f22009-08-05 01:29:28 +00005777 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005778 }
5779 }
5780 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005781
Dan Gohman98ca4f22009-08-05 01:29:28 +00005782 // Handle the incoming return values from the call.
5783 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005784 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005785 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005786 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005787 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005788 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5789 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005790 for (unsigned i = 0; i != NumRegs; ++i) {
5791 ISD::InputArg MyFlags;
5792 MyFlags.VT = RegisterVT;
5793 MyFlags.Used = isReturnValueUsed;
5794 if (RetSExt)
5795 MyFlags.Flags.setSExt();
5796 if (RetZExt)
5797 MyFlags.Flags.setZExt();
5798 if (isInreg)
5799 MyFlags.Flags.setInReg();
5800 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005801 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 }
5803
Dan Gohman98ca4f22009-08-05 01:29:28 +00005804 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005805 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005806 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005807
5808 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005810 "LowerCall didn't return a valid chain!");
5811 assert((!isTailCall || InVals.empty()) &&
5812 "LowerCall emitted a return value for a tail call!");
5813 assert((isTailCall || InVals.size() == Ins.size()) &&
5814 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005815
5816 // For a tail call, the return value is merely live-out and there aren't
5817 // any nodes in the DAG representing it. Return a special value to
5818 // indicate that a tail call has been emitted and no more Instructions
5819 // should be processed in the current block.
5820 if (isTailCall) {
5821 DAG.setRoot(Chain);
5822 return std::make_pair(SDValue(), SDValue());
5823 }
5824
Evan Chengaf1871f2010-03-11 19:38:18 +00005825 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5826 assert(InVals[i].getNode() &&
5827 "LowerCall emitted a null value!");
5828 assert(Ins[i].VT == InVals[i].getValueType() &&
5829 "LowerCall emitted a value with the wrong type!");
5830 });
5831
Dan Gohman98ca4f22009-08-05 01:29:28 +00005832 // Collect the legal value parts into potentially illegal values
5833 // that correspond to the original function's return values.
5834 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5835 if (RetSExt)
5836 AssertOp = ISD::AssertSext;
5837 else if (RetZExt)
5838 AssertOp = ISD::AssertZext;
5839 SmallVector<SDValue, 4> ReturnValues;
5840 unsigned CurReg = 0;
5841 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005842 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005843 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5844 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005845
Bill Wendling46ada192010-03-02 01:55:18 +00005846 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005847 NumRegs, RegisterVT, VT,
5848 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005849 CurReg += NumRegs;
5850 }
5851
5852 // For a function returning void, there is no return value. We can't create
5853 // such a node, so we just return a null return value in that case. In
5854 // that case, nothing will actualy look at the value.
5855 if (ReturnValues.empty())
5856 return std::make_pair(SDValue(), Chain);
5857
5858 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5859 DAG.getVTList(&RetTys[0], RetTys.size()),
5860 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005861 return std::make_pair(Res, Chain);
5862}
5863
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005864void TargetLowering::LowerOperationWrapper(SDNode *N,
5865 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005866 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005867 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005868 if (Res.getNode())
5869 Results.push_back(Res);
5870}
5871
Dan Gohmand858e902010-04-17 15:26:15 +00005872SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005873 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005874 return SDValue();
5875}
5876
Dan Gohman46510a72010-04-15 01:51:59 +00005877void
5878SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005879 SDValue Op = getValue(V);
5880 assert((Op.getOpcode() != ISD::CopyFromReg ||
5881 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5882 "Copy from a reg to the same reg!");
5883 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5884
Owen Anderson23b9b192009-08-12 00:36:31 +00005885 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005886 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005887 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005888 PendingExports.push_back(Chain);
5889}
5890
5891#include "llvm/CodeGen/SelectionDAGISel.h"
5892
Dan Gohman46510a72010-04-15 01:51:59 +00005893void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005894 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005895 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005896 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005897 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005898 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005899 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005900 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005901
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005902 // Check whether the function can return without sret-demotion.
5903 SmallVector<EVT, 4> OutVTs;
5904 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005905 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005906 OutVTs, OutsFlags, TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005907
Dan Gohman7451d3e2010-05-29 17:03:36 +00005908 FuncInfo->CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(),
5909 F.isVarArg(),
5910 OutVTs, OutsFlags, DAG);
5911 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005912 // Put in an sret pointer parameter before all the other parameters.
5913 SmallVector<EVT, 1> ValueVTs;
5914 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5915
5916 // NOTE: Assuming that a pointer will never break down to more than one VT
5917 // or one register.
5918 ISD::ArgFlagsTy Flags;
5919 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00005920 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005921 ISD::InputArg RetArg(Flags, RegisterVT, true);
5922 Ins.push_back(RetArg);
5923 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005924
Dan Gohman98ca4f22009-08-05 01:29:28 +00005925 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005926 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005927 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005928 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005929 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005930 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5931 bool isArgValueUsed = !I->use_empty();
5932 for (unsigned Value = 0, NumValues = ValueVTs.size();
5933 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005934 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005935 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005936 ISD::ArgFlagsTy Flags;
5937 unsigned OriginalAlignment =
5938 TD->getABITypeAlignment(ArgTy);
5939
5940 if (F.paramHasAttr(Idx, Attribute::ZExt))
5941 Flags.setZExt();
5942 if (F.paramHasAttr(Idx, Attribute::SExt))
5943 Flags.setSExt();
5944 if (F.paramHasAttr(Idx, Attribute::InReg))
5945 Flags.setInReg();
5946 if (F.paramHasAttr(Idx, Attribute::StructRet))
5947 Flags.setSRet();
5948 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5949 Flags.setByVal();
5950 const PointerType *Ty = cast<PointerType>(I->getType());
5951 const Type *ElementTy = Ty->getElementType();
5952 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5953 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5954 // For ByVal, alignment should be passed from FE. BE will guess if
5955 // this info is not there but there are cases it cannot get right.
5956 if (F.getParamAlignment(Idx))
5957 FrameAlign = F.getParamAlignment(Idx);
5958 Flags.setByValAlign(FrameAlign);
5959 Flags.setByValSize(FrameSize);
5960 }
5961 if (F.paramHasAttr(Idx, Attribute::Nest))
5962 Flags.setNest();
5963 Flags.setOrigAlign(OriginalAlignment);
5964
Owen Anderson23b9b192009-08-12 00:36:31 +00005965 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5966 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005967 for (unsigned i = 0; i != NumRegs; ++i) {
5968 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5969 if (NumRegs > 1 && i == 0)
5970 MyFlags.Flags.setSplit();
5971 // if it isn't first piece, alignment must be 1
5972 else if (i > 0)
5973 MyFlags.Flags.setOrigAlign(1);
5974 Ins.push_back(MyFlags);
5975 }
5976 }
5977 }
5978
5979 // Call the target to set up the argument values.
5980 SmallVector<SDValue, 8> InVals;
5981 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5982 F.isVarArg(), Ins,
5983 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005984
5985 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005987 "LowerFormalArguments didn't return a valid chain!");
5988 assert(InVals.size() == Ins.size() &&
5989 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005990 DEBUG({
5991 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5992 assert(InVals[i].getNode() &&
5993 "LowerFormalArguments emitted a null value!");
5994 assert(Ins[i].VT == InVals[i].getValueType() &&
5995 "LowerFormalArguments emitted a value with the wrong type!");
5996 }
5997 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005998
Dan Gohman5e866062009-08-06 15:37:27 +00005999 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006000 DAG.setRoot(NewRoot);
6001
6002 // Set up the argument values.
6003 unsigned i = 0;
6004 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006005 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006006 // Create a virtual register for the sret pointer, and put in a copy
6007 // from the sret argument into it.
6008 SmallVector<EVT, 1> ValueVTs;
6009 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6010 EVT VT = ValueVTs[0];
6011 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6012 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006013 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006014 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006015
Dan Gohman2048b852009-11-23 18:04:58 +00006016 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006017 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6018 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006019 FuncInfo->DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006020 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6021 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006022 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006023
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006024 // i indexes lowered arguments. Bump it past the hidden sret argument.
6025 // Idx indexes LLVM arguments. Don't touch it.
6026 ++i;
6027 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006028
Dan Gohman46510a72010-04-15 01:51:59 +00006029 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006030 ++I, ++Idx) {
6031 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006032 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006033 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006034 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006035 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006036 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006037 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6038 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006039
6040 if (!I->use_empty()) {
6041 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6042 if (F.paramHasAttr(Idx, Attribute::SExt))
6043 AssertOp = ISD::AssertSext;
6044 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6045 AssertOp = ISD::AssertZext;
6046
Bill Wendling46ada192010-03-02 01:55:18 +00006047 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006048 NumParts, PartVT, VT,
6049 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006050 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006051
Dan Gohman98ca4f22009-08-05 01:29:28 +00006052 i += NumParts;
6053 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006054
Dan Gohman98ca4f22009-08-05 01:29:28 +00006055 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00006056 SDValue Res;
6057 if (!ArgValues.empty())
6058 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6059 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006060 SDB->setValue(I, Res);
6061
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006062 // If this argument is live outside of the entry block, insert a copy from
6063 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006064 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006065 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006066 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006067
Dan Gohman98ca4f22009-08-05 01:29:28 +00006068 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006069
6070 // Finally, if the target has anything special to do, allow it to do so.
6071 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006072 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006073}
6074
6075/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6076/// ensure constants are generated when needed. Remember the virtual registers
6077/// that need to be added to the Machine PHI nodes as input. We cannot just
6078/// directly add them, because expansion might result in multiple MBB's for one
6079/// BB. As such, the start of the BB might correspond to a different MBB than
6080/// the end.
6081///
6082void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006083SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006084 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006085
6086 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6087
6088 // Check successor nodes' PHI nodes that expect a constant to be available
6089 // from this block.
6090 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006091 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006092 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006093 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006095 // If this terminator has multiple identical successors (common for
6096 // switches), only handle each succ once.
6097 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006099 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006100
6101 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6102 // nodes and Machine PHI nodes, but the incoming operands have not been
6103 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006104 for (BasicBlock::const_iterator I = SuccBB->begin();
6105 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006106 // Ignore dead phi's.
6107 if (PN->use_empty()) continue;
6108
6109 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006110 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006111
Dan Gohman46510a72010-04-15 01:51:59 +00006112 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006113 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006114 if (RegOut == 0) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006115 RegOut = FuncInfo.CreateRegForValue(C);
6116 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006117 }
6118 Reg = RegOut;
6119 } else {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006120 Reg = FuncInfo.ValueMap[PHIOp];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006121 if (Reg == 0) {
6122 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006123 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006124 "Didn't codegen value into a register!??");
Dan Gohmanf81eca02010-04-22 20:46:50 +00006125 Reg = FuncInfo.CreateRegForValue(PHIOp);
6126 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006127 }
6128 }
6129
6130 // Remember that this register needs to added to the machine PHI node as
6131 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006132 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006133 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6134 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006135 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006136 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006137 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006138 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006139 Reg += NumRegisters;
6140 }
6141 }
6142 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006143 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006144}