blob: 5177c9b2eaa8b62514c277f0ae7a78d6b24379b9 [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Devang Patel00190342010-03-15 19:15:44 +000015#include "SDNodeDbgValue.h"
Dan Gohman2048b852009-11-23 18:04:58 +000016#include "SelectionDAGBuilder.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
Michael J. Spencer84ac4d52010-10-16 08:25:41 +000018#include "llvm/ADT/PostOrderIterator.h"
Dan Gohman5b229802008-09-04 20:49:27 +000019#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000021#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000022#include "llvm/Constants.h"
23#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Function.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/InlineAsm.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/IntrinsicInst.h"
Chris Lattner6129c372010-04-08 00:09:16 +000031#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000032#include "llvm/Module.h"
Dan Gohman5eb6d652010-04-21 01:22:34 +000033#include "llvm/CodeGen/Analysis.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000034#include "llvm/CodeGen/FastISel.h"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000035#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000036#include "llvm/CodeGen/GCStrategy.h"
37#include "llvm/CodeGen/GCMetadata.h"
38#include "llvm/CodeGen/MachineFunction.h"
39#include "llvm/CodeGen/MachineFrameInfo.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
41#include "llvm/CodeGen/MachineJumpTableInfo.h"
42#include "llvm/CodeGen/MachineModuleInfo.h"
43#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000044#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000045#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000046#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000047#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000048#include "llvm/Target/TargetFrameLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000049#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000050#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000052#include "llvm/Target/TargetOptions.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000053#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000054#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000055#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000056#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000057#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000058#include <algorithm>
59using namespace llvm;
60
Dale Johannesen601d3c02008-09-05 01:48:15 +000061/// LimitFloatPrecision - Generate low-precision inline sequences for
62/// some float libcalls (6, 8 or 12 bits).
63static unsigned LimitFloatPrecision;
64
65static cl::opt<unsigned, true>
66LimitFPPrecision("limit-float-precision",
67 cl::desc("Generate low-precision inline sequences "
68 "for some float libcalls"),
69 cl::location(LimitFloatPrecision),
70 cl::init(0));
71
Andrew Trickde91f3c2010-11-12 17:50:46 +000072// Limit the width of DAG chains. This is important in general to prevent
73// prevent DAG-based analysis from blowing up. For example, alias analysis and
74// load clustering may not complete in reasonable time. It is difficult to
75// recognize and avoid this situation within each individual analysis, and
76// future analyses are likely to have the same behavior. Limiting DAG width is
Andrew Trickb9e6fe12010-11-20 07:26:51 +000077// the safe approach, and will be especially important with global DAGs.
Andrew Trickde91f3c2010-11-12 17:50:46 +000078//
79// MaxParallelChains default is arbitrarily high to avoid affecting
80// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
Andrew Trickb9e6fe12010-11-20 07:26:51 +000081// sequence over this should have been converted to llvm.memcpy by the
82// frontend. It easy to induce this behavior with .ll code such as:
83// %buffer = alloca [4096 x i8]
84// %data = load [4096 x i8]* %argPtr
85// store [4096 x i8] %data, [4096 x i8]* %buffer
Andrew Trick778583a2011-03-11 17:46:59 +000086static const unsigned MaxParallelChains = 64;
Andrew Trickde91f3c2010-11-12 17:50:46 +000087
Chris Lattner3ac18842010-08-24 23:20:40 +000088static SDValue getCopyFromPartsVector(SelectionDAG &DAG, DebugLoc DL,
89 const SDValue *Parts, unsigned NumParts,
90 EVT PartVT, EVT ValueVT);
Michael J. Spencere70c5262010-10-16 08:25:21 +000091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000092/// getCopyFromParts - Create a value that contains the specified legal parts
93/// combined into the value they represent. If the parts combine to a type
94/// larger then ValueVT then AssertOp can be used to specify whether the extra
95/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
96/// (ISD::AssertSext).
Chris Lattner3ac18842010-08-24 23:20:40 +000097static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc DL,
Dale Johannesen66978ee2009-01-31 02:22:37 +000098 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +000099 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000100 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000101 if (ValueVT.isVector())
102 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000104 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000105 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000106 SDValue Val = Parts[0];
107
108 if (NumParts > 1) {
109 // Assemble the value from multiple parts.
Chris Lattner3ac18842010-08-24 23:20:40 +0000110 if (ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 unsigned PartBits = PartVT.getSizeInBits();
112 unsigned ValueBits = ValueVT.getSizeInBits();
113
114 // Assemble the power of 2 part.
115 unsigned RoundParts = NumParts & (NumParts - 1) ?
116 1 << Log2_32(NumParts) : NumParts;
117 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000118 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000119 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000120 SDValue Lo, Hi;
121
Owen Anderson23b9b192009-08-12 00:36:31 +0000122 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000124 if (RoundParts > 2) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000125 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000126 PartVT, HalfVT);
Chris Lattner3ac18842010-08-24 23:20:40 +0000127 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000128 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000129 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000130 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
131 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000132 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 if (TLI.isBigEndian())
135 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000136
Chris Lattner3ac18842010-08-24 23:20:40 +0000137 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000138
139 if (RoundParts < NumParts) {
140 // Assemble the trailing non-power-of-2 part.
141 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000142 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000143 Hi = getCopyFromParts(DAG, DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000144 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000145
146 // Combine the round and odd parts.
147 Lo = Val;
148 if (TLI.isBigEndian())
149 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000150 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000151 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
152 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000153 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000154 TLI.getPointerTy()));
Chris Lattner3ac18842010-08-24 23:20:40 +0000155 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
156 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000158 } else if (PartVT.isFloatingPoint()) {
159 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000161 "Unexpected split");
162 SDValue Lo, Hi;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000163 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
164 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000165 if (TLI.isBigEndian())
166 std::swap(Lo, Hi);
Chris Lattner3ac18842010-08-24 23:20:40 +0000167 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000168 } else {
169 // FP split into integer parts (soft fp)
170 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
171 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000172 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Chris Lattner3ac18842010-08-24 23:20:40 +0000173 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000174 }
175 }
176
177 // There is now one part, held in Val. Correct it to match ValueVT.
178 PartVT = Val.getValueType();
179
180 if (PartVT == ValueVT)
181 return Val;
182
Chris Lattner3ac18842010-08-24 23:20:40 +0000183 if (PartVT.isInteger() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000184 if (ValueVT.bitsLT(PartVT)) {
185 // For a truncate, see if we have any information to
186 // indicate whether the truncated bits will always be
187 // zero or sign-extension.
188 if (AssertOp != ISD::DELETED_NODE)
Chris Lattner3ac18842010-08-24 23:20:40 +0000189 Val = DAG.getNode(AssertOp, DL, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000190 DAG.getValueType(ValueVT));
Chris Lattner3ac18842010-08-24 23:20:40 +0000191 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000192 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000193 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000194 }
195
196 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000197 // FP_ROUND's are always exact here.
198 if (ValueVT.bitsLT(Val.getValueType()))
199 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val,
Bill Wendling4533cac2010-01-28 21:51:40 +0000200 DAG.getIntPtrConstant(1));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000201
Chris Lattner3ac18842010-08-24 23:20:40 +0000202 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000203 }
204
Bill Wendling4533cac2010-01-28 21:51:40 +0000205 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000206 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000207
Torok Edwinc23197a2009-07-14 16:55:14 +0000208 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000209 return SDValue();
210}
211
Chris Lattner3ac18842010-08-24 23:20:40 +0000212/// getCopyFromParts - Create a value that contains the specified legal parts
213/// combined into the value they represent. If the parts combine to a type
214/// larger then ValueVT then AssertOp can be used to specify whether the extra
215/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
216/// (ISD::AssertSext).
217static SDValue getCopyFromPartsVector(SelectionDAG &DAG, DebugLoc DL,
218 const SDValue *Parts, unsigned NumParts,
219 EVT PartVT, EVT ValueVT) {
220 assert(ValueVT.isVector() && "Not a vector value");
221 assert(NumParts > 0 && "No parts to assemble!");
222 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
223 SDValue Val = Parts[0];
Michael J. Spencere70c5262010-10-16 08:25:21 +0000224
Chris Lattner3ac18842010-08-24 23:20:40 +0000225 // Handle a multi-element vector.
226 if (NumParts > 1) {
227 EVT IntermediateVT, RegisterVT;
228 unsigned NumIntermediates;
229 unsigned NumRegs =
230 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
231 NumIntermediates, RegisterVT);
232 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
233 NumParts = NumRegs; // Silence a compiler warning.
234 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
235 assert(RegisterVT == Parts[0].getValueType() &&
236 "Part type doesn't match part!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000237
Chris Lattner3ac18842010-08-24 23:20:40 +0000238 // Assemble the parts into intermediate operands.
239 SmallVector<SDValue, 8> Ops(NumIntermediates);
240 if (NumIntermediates == NumParts) {
241 // If the register was not expanded, truncate or copy the value,
242 // as appropriate.
243 for (unsigned i = 0; i != NumParts; ++i)
244 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
245 PartVT, IntermediateVT);
246 } else if (NumParts > 0) {
247 // If the intermediate type was expanded, build the intermediate
248 // operands from the parts.
249 assert(NumParts % NumIntermediates == 0 &&
250 "Must expand into a divisible number of parts!");
251 unsigned Factor = NumParts / NumIntermediates;
252 for (unsigned i = 0; i != NumIntermediates; ++i)
253 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
254 PartVT, IntermediateVT);
255 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000256
Chris Lattner3ac18842010-08-24 23:20:40 +0000257 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
258 // intermediate operands.
259 Val = DAG.getNode(IntermediateVT.isVector() ?
260 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL,
261 ValueVT, &Ops[0], NumIntermediates);
262 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000263
Chris Lattner3ac18842010-08-24 23:20:40 +0000264 // There is now one part, held in Val. Correct it to match ValueVT.
265 PartVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000266
Chris Lattner3ac18842010-08-24 23:20:40 +0000267 if (PartVT == ValueVT)
268 return Val;
Michael J. Spencere70c5262010-10-16 08:25:21 +0000269
Chris Lattnere6f7c262010-08-25 22:49:25 +0000270 if (PartVT.isVector()) {
271 // If the element type of the source/dest vectors are the same, but the
272 // parts vector has more elements than the value vector, then we have a
273 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
274 // elements we want.
275 if (PartVT.getVectorElementType() == ValueVT.getVectorElementType()) {
276 assert(PartVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
277 "Cannot narrow, it would be a lossy transformation");
278 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
279 DAG.getIntPtrConstant(0));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000280 }
281
Chris Lattnere6f7c262010-08-25 22:49:25 +0000282 // Vector/Vector bitcast.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000283 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000284 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000285
Chris Lattner3ac18842010-08-24 23:20:40 +0000286 assert(ValueVT.getVectorElementType() == PartVT &&
287 ValueVT.getVectorNumElements() == 1 &&
288 "Only trivial scalar-to-vector conversions should get here!");
289 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
290}
291
292
293
Chris Lattnera13b8602010-08-24 23:10:06 +0000294
295static void getCopyToPartsVector(SelectionDAG &DAG, DebugLoc dl,
296 SDValue Val, SDValue *Parts, unsigned NumParts,
297 EVT PartVT);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000298
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000299/// getCopyToParts - Create a series of nodes that contain the specified value
300/// split into legal parts. If the parts contain more bits than Val, then, for
301/// integers, ExtendKind can be used to specify how to generate the extra bits.
Chris Lattnera13b8602010-08-24 23:10:06 +0000302static void getCopyToParts(SelectionDAG &DAG, DebugLoc DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000303 SDValue Val, SDValue *Parts, unsigned NumParts,
304 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000305 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Owen Andersone50ed302009-08-10 22:56:29 +0000306 EVT ValueVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000307
Chris Lattnera13b8602010-08-24 23:10:06 +0000308 // Handle the vector case separately.
309 if (ValueVT.isVector())
310 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000311
Chris Lattnera13b8602010-08-24 23:10:06 +0000312 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000313 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000314 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000315 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
316
Chris Lattnera13b8602010-08-24 23:10:06 +0000317 if (NumParts == 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000318 return;
319
Chris Lattnera13b8602010-08-24 23:10:06 +0000320 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
321 if (PartVT == ValueVT) {
322 assert(NumParts == 1 && "No-op copy with multiple parts!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000323 Parts[0] = Val;
324 return;
325 }
326
Chris Lattnera13b8602010-08-24 23:10:06 +0000327 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
328 // If the parts cover more bits than the value has, promote the value.
329 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
330 assert(NumParts == 1 && "Do not know what to promote to!");
331 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
332 } else {
333 assert(PartVT.isInteger() && ValueVT.isInteger() &&
Michael J. Spencere70c5262010-10-16 08:25:21 +0000334 "Unknown mismatch!");
Chris Lattnera13b8602010-08-24 23:10:06 +0000335 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
336 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
337 }
338 } else if (PartBits == ValueVT.getSizeInBits()) {
339 // Different types of the same size.
340 assert(NumParts == 1 && PartVT != ValueVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000341 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000342 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
343 // If the parts cover less bits than value has, truncate the value.
344 assert(PartVT.isInteger() && ValueVT.isInteger() &&
345 "Unknown mismatch!");
346 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
347 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
348 }
349
350 // The value may have changed - recompute ValueVT.
351 ValueVT = Val.getValueType();
352 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
353 "Failed to tile the value with PartVT!");
354
355 if (NumParts == 1) {
356 assert(PartVT == ValueVT && "Type conversion failed!");
357 Parts[0] = Val;
358 return;
359 }
360
361 // Expand the value into multiple parts.
362 if (NumParts & (NumParts - 1)) {
363 // The number of parts is not a power of 2. Split off and copy the tail.
364 assert(PartVT.isInteger() && ValueVT.isInteger() &&
365 "Do not know what to expand to!");
366 unsigned RoundParts = 1 << Log2_32(NumParts);
367 unsigned RoundBits = RoundParts * PartBits;
368 unsigned OddParts = NumParts - RoundParts;
369 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
370 DAG.getIntPtrConstant(RoundBits));
371 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT);
372
373 if (TLI.isBigEndian())
374 // The odd parts were reversed by getCopyToParts - unreverse them.
375 std::reverse(Parts + RoundParts, Parts + NumParts);
376
377 NumParts = RoundParts;
378 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
379 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
380 }
381
382 // The number of parts is a power of 2. Repeatedly bisect the value using
383 // EXTRACT_ELEMENT.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000384 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000385 EVT::getIntegerVT(*DAG.getContext(),
386 ValueVT.getSizeInBits()),
387 Val);
388
389 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
390 for (unsigned i = 0; i < NumParts; i += StepSize) {
391 unsigned ThisBits = StepSize * PartBits / 2;
392 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
393 SDValue &Part0 = Parts[i];
394 SDValue &Part1 = Parts[i+StepSize/2];
395
396 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
397 ThisVT, Part0, DAG.getIntPtrConstant(1));
398 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
399 ThisVT, Part0, DAG.getIntPtrConstant(0));
400
401 if (ThisBits == PartBits && ThisVT != PartVT) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000402 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
403 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
Chris Lattnera13b8602010-08-24 23:10:06 +0000404 }
405 }
406 }
407
408 if (TLI.isBigEndian())
409 std::reverse(Parts, Parts + OrigNumParts);
410}
411
412
413/// getCopyToPartsVector - Create a series of nodes that contain the specified
414/// value split into legal parts.
415static void getCopyToPartsVector(SelectionDAG &DAG, DebugLoc DL,
416 SDValue Val, SDValue *Parts, unsigned NumParts,
417 EVT PartVT) {
418 EVT ValueVT = Val.getValueType();
419 assert(ValueVT.isVector() && "Not a vector");
420 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000421
Chris Lattnera13b8602010-08-24 23:10:06 +0000422 if (NumParts == 1) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000423 if (PartVT == ValueVT) {
424 // Nothing to do.
425 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
426 // Bitconvert vector->vector case.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000427 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000428 } else if (PartVT.isVector() &&
429 PartVT.getVectorElementType() == ValueVT.getVectorElementType()&&
430 PartVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
431 EVT ElementVT = PartVT.getVectorElementType();
432 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
433 // undef elements.
434 SmallVector<SDValue, 16> Ops;
435 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
436 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
437 ElementVT, Val, DAG.getIntPtrConstant(i)));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000438
Chris Lattnere6f7c262010-08-25 22:49:25 +0000439 for (unsigned i = ValueVT.getVectorNumElements(),
440 e = PartVT.getVectorNumElements(); i != e; ++i)
441 Ops.push_back(DAG.getUNDEF(ElementVT));
442
443 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size());
444
445 // FIXME: Use CONCAT for 2x -> 4x.
Michael J. Spencere70c5262010-10-16 08:25:21 +0000446
Chris Lattnere6f7c262010-08-25 22:49:25 +0000447 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
448 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
449 } else {
450 // Vector -> scalar conversion.
451 assert(ValueVT.getVectorElementType() == PartVT &&
452 ValueVT.getVectorNumElements() == 1 &&
453 "Only trivial vector-to-scalar conversions should get here!");
454 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
455 PartVT, Val, DAG.getIntPtrConstant(0));
Chris Lattnera13b8602010-08-24 23:10:06 +0000456 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000457
Chris Lattnera13b8602010-08-24 23:10:06 +0000458 Parts[0] = Val;
459 return;
460 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000461
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000462 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000463 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000464 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000465 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
Devang Patel8f09bea2010-08-26 20:32:32 +0000466 IntermediateVT,
467 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000468 unsigned NumElements = ValueVT.getVectorNumElements();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000469
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000470 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
471 NumParts = NumRegs; // Silence a compiler warning.
472 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000474 // Split the vector into intermediate operands.
475 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000476 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000477 if (IntermediateVT.isVector())
Chris Lattnera13b8602010-08-24 23:10:06 +0000478 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000479 IntermediateVT, Val,
Chris Lattnera13b8602010-08-24 23:10:06 +0000480 DAG.getIntPtrConstant(i * (NumElements / NumIntermediates)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000481 else
Chris Lattnera13b8602010-08-24 23:10:06 +0000482 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Chris Lattnere6f7c262010-08-25 22:49:25 +0000483 IntermediateVT, Val, DAG.getIntPtrConstant(i));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000484 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000485
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000486 // Split the intermediate operands into legal parts.
487 if (NumParts == NumIntermediates) {
488 // If the register was not expanded, promote or copy the value,
489 // as appropriate.
490 for (unsigned i = 0; i != NumParts; ++i)
Chris Lattnera13b8602010-08-24 23:10:06 +0000491 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000492 } else if (NumParts > 0) {
493 // If the intermediate type was expanded, split each the value into
494 // legal parts.
495 assert(NumParts % NumIntermediates == 0 &&
496 "Must expand into a divisible number of parts!");
497 unsigned Factor = NumParts / NumIntermediates;
498 for (unsigned i = 0; i != NumIntermediates; ++i)
Chris Lattnera13b8602010-08-24 23:10:06 +0000499 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000500 }
501}
502
Chris Lattnera13b8602010-08-24 23:10:06 +0000503
504
505
Dan Gohman462f6b52010-05-29 17:53:24 +0000506namespace {
507 /// RegsForValue - This struct represents the registers (physical or virtual)
508 /// that a particular set of values is assigned, and the type information
509 /// about the value. The most common situation is to represent one value at a
510 /// time, but struct or array values are handled element-wise as multiple
511 /// values. The splitting of aggregates is performed recursively, so that we
512 /// never have aggregate-typed registers. The values at this point do not
513 /// necessarily have legal types, so each value may require one or more
514 /// registers of some legal type.
515 ///
516 struct RegsForValue {
517 /// ValueVTs - The value types of the values, which may not be legal, and
518 /// may need be promoted or synthesized from one or more registers.
519 ///
520 SmallVector<EVT, 4> ValueVTs;
521
522 /// RegVTs - The value types of the registers. This is the same size as
523 /// ValueVTs and it records, for each value, what the type of the assigned
524 /// register or registers are. (Individual values are never synthesized
525 /// from more than one type of register.)
526 ///
527 /// With virtual registers, the contents of RegVTs is redundant with TLI's
528 /// getRegisterType member function, however when with physical registers
529 /// it is necessary to have a separate record of the types.
530 ///
531 SmallVector<EVT, 4> RegVTs;
532
533 /// Regs - This list holds the registers assigned to the values.
534 /// Each legal or promoted value requires one register, and each
535 /// expanded value requires multiple registers.
536 ///
537 SmallVector<unsigned, 4> Regs;
538
539 RegsForValue() {}
540
541 RegsForValue(const SmallVector<unsigned, 4> &regs,
542 EVT regvt, EVT valuevt)
543 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
544
Dan Gohman462f6b52010-05-29 17:53:24 +0000545 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
546 unsigned Reg, const Type *Ty) {
547 ComputeValueVTs(tli, Ty, ValueVTs);
548
549 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
550 EVT ValueVT = ValueVTs[Value];
551 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
552 EVT RegisterVT = tli.getRegisterType(Context, ValueVT);
553 for (unsigned i = 0; i != NumRegs; ++i)
554 Regs.push_back(Reg + i);
555 RegVTs.push_back(RegisterVT);
556 Reg += NumRegs;
557 }
558 }
559
560 /// areValueTypesLegal - Return true if types of all the values are legal.
561 bool areValueTypesLegal(const TargetLowering &TLI) {
562 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
563 EVT RegisterVT = RegVTs[Value];
564 if (!TLI.isTypeLegal(RegisterVT))
565 return false;
566 }
567 return true;
568 }
569
570 /// append - Add the specified values to this one.
571 void append(const RegsForValue &RHS) {
572 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
573 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
574 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
575 }
576
577 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
578 /// this value and returns the result as a ValueVTs value. This uses
579 /// Chain/Flag as the input and updates them for the output Chain/Flag.
580 /// If the Flag pointer is NULL, no flag is used.
581 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
582 DebugLoc dl,
583 SDValue &Chain, SDValue *Flag) const;
584
585 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
586 /// specified value into the registers specified by this object. This uses
587 /// Chain/Flag as the input and updates them for the output Chain/Flag.
588 /// If the Flag pointer is NULL, no flag is used.
589 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
590 SDValue &Chain, SDValue *Flag) const;
591
592 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
593 /// operand list. This adds the code marker, matching input operand index
594 /// (if applicable), and includes the number of values added into it.
595 void AddInlineAsmOperands(unsigned Kind,
596 bool HasMatching, unsigned MatchingIdx,
597 SelectionDAG &DAG,
598 std::vector<SDValue> &Ops) const;
599 };
600}
601
602/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
603/// this value and returns the result as a ValueVT value. This uses
604/// Chain/Flag as the input and updates them for the output Chain/Flag.
605/// If the Flag pointer is NULL, no flag is used.
606SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
607 FunctionLoweringInfo &FuncInfo,
608 DebugLoc dl,
609 SDValue &Chain, SDValue *Flag) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000610 // A Value with type {} or [0 x %t] needs no registers.
611 if (ValueVTs.empty())
612 return SDValue();
613
Dan Gohman462f6b52010-05-29 17:53:24 +0000614 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
615
616 // Assemble the legal parts into the final values.
617 SmallVector<SDValue, 4> Values(ValueVTs.size());
618 SmallVector<SDValue, 8> Parts;
619 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
620 // Copy the legal parts from the registers.
621 EVT ValueVT = ValueVTs[Value];
622 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
623 EVT RegisterVT = RegVTs[Value];
624
625 Parts.resize(NumRegs);
626 for (unsigned i = 0; i != NumRegs; ++i) {
627 SDValue P;
628 if (Flag == 0) {
629 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
630 } else {
631 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
632 *Flag = P.getValue(2);
633 }
634
635 Chain = P.getValue(1);
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000636 Parts[i] = P;
Dan Gohman462f6b52010-05-29 17:53:24 +0000637
638 // If the source register was virtual and if we know something about it,
639 // add an assert node.
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000640 if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) ||
Cameron Zwariche1497b92011-02-24 10:00:08 +0000641 !RegisterVT.isInteger() || RegisterVT.isVector())
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000642 continue;
Cameron Zwariche1497b92011-02-24 10:00:08 +0000643
644 const FunctionLoweringInfo::LiveOutInfo *LOI =
645 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
646 if (!LOI)
647 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000648
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000649 unsigned RegSize = RegisterVT.getSizeInBits();
Cameron Zwariche1497b92011-02-24 10:00:08 +0000650 unsigned NumSignBits = LOI->NumSignBits;
651 unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes();
Dan Gohman462f6b52010-05-29 17:53:24 +0000652
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000653 // FIXME: We capture more information than the dag can represent. For
654 // now, just use the tightest assertzext/assertsext possible.
655 bool isSExt = true;
656 EVT FromVT(MVT::Other);
657 if (NumSignBits == RegSize)
658 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
659 else if (NumZeroBits >= RegSize-1)
660 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
661 else if (NumSignBits > RegSize-8)
662 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
663 else if (NumZeroBits >= RegSize-8)
664 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
665 else if (NumSignBits > RegSize-16)
666 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
667 else if (NumZeroBits >= RegSize-16)
668 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
669 else if (NumSignBits > RegSize-32)
670 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
671 else if (NumZeroBits >= RegSize-32)
672 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
673 else
674 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000675
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000676 // Add an assertion node.
677 assert(FromVT != MVT::Other);
678 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
679 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohman462f6b52010-05-29 17:53:24 +0000680 }
681
682 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
683 NumRegs, RegisterVT, ValueVT);
684 Part += NumRegs;
685 Parts.clear();
686 }
687
688 return DAG.getNode(ISD::MERGE_VALUES, dl,
689 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
690 &Values[0], ValueVTs.size());
691}
692
693/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
694/// specified value into the registers specified by this object. This uses
695/// Chain/Flag as the input and updates them for the output Chain/Flag.
696/// If the Flag pointer is NULL, no flag is used.
697void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
698 SDValue &Chain, SDValue *Flag) const {
699 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
700
701 // Get the list of the values's legal parts.
702 unsigned NumRegs = Regs.size();
703 SmallVector<SDValue, 8> Parts(NumRegs);
704 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
705 EVT ValueVT = ValueVTs[Value];
706 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
707 EVT RegisterVT = RegVTs[Value];
708
Chris Lattner3ac18842010-08-24 23:20:40 +0000709 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohman462f6b52010-05-29 17:53:24 +0000710 &Parts[Part], NumParts, RegisterVT);
711 Part += NumParts;
712 }
713
714 // Copy the parts into the registers.
715 SmallVector<SDValue, 8> Chains(NumRegs);
716 for (unsigned i = 0; i != NumRegs; ++i) {
717 SDValue Part;
718 if (Flag == 0) {
719 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
720 } else {
721 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
722 *Flag = Part.getValue(1);
723 }
724
725 Chains[i] = Part.getValue(0);
726 }
727
728 if (NumRegs == 1 || Flag)
729 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
730 // flagged to it. That is the CopyToReg nodes and the user are considered
731 // a single scheduling unit. If we create a TokenFactor and return it as
732 // chain, then the TokenFactor is both a predecessor (operand) of the
733 // user as well as a successor (the TF operands are flagged to the user).
734 // c1, f1 = CopyToReg
735 // c2, f2 = CopyToReg
736 // c3 = TokenFactor c1, c2
737 // ...
738 // = op c3, ..., f2
739 Chain = Chains[NumRegs-1];
740 else
741 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
742}
743
744/// AddInlineAsmOperands - Add this value to the specified inlineasm node
745/// operand list. This adds the code marker and includes the number of
746/// values added into it.
747void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
748 unsigned MatchingIdx,
749 SelectionDAG &DAG,
750 std::vector<SDValue> &Ops) const {
751 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
752
753 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
754 if (HasMatching)
755 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
756 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
757 Ops.push_back(Res);
758
759 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
760 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
761 EVT RegisterVT = RegVTs[Value];
762 for (unsigned i = 0; i != NumRegs; ++i) {
763 assert(Reg < Regs.size() && "Mismatch in # registers expected");
764 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
765 }
766 }
767}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000768
Dan Gohman2048b852009-11-23 18:04:58 +0000769void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000770 AA = &aa;
771 GFI = gfi;
772 TD = DAG.getTarget().getTargetData();
773}
774
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000775/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000776/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000777/// for a new block. This doesn't clear out information about
778/// additional blocks that are needed to complete switch lowering
779/// or PHI node updating; that information is cleared out as it is
780/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000781void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000782 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000783 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000784 PendingLoads.clear();
785 PendingExports.clear();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000786 DanglingDebugInfoMap.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000787 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000788 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000789}
790
791/// getRoot - Return the current virtual root of the Selection DAG,
792/// flushing any PendingLoad items. This must be done before emitting
793/// a store or any other node that may need to be ordered after any
794/// prior load instructions.
795///
Dan Gohman2048b852009-11-23 18:04:58 +0000796SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000797 if (PendingLoads.empty())
798 return DAG.getRoot();
799
800 if (PendingLoads.size() == 1) {
801 SDValue Root = PendingLoads[0];
802 DAG.setRoot(Root);
803 PendingLoads.clear();
804 return Root;
805 }
806
807 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000809 &PendingLoads[0], PendingLoads.size());
810 PendingLoads.clear();
811 DAG.setRoot(Root);
812 return Root;
813}
814
815/// getControlRoot - Similar to getRoot, but instead of flushing all the
816/// PendingLoad items, flush all the PendingExports items. It is necessary
817/// to do this before emitting a terminator instruction.
818///
Dan Gohman2048b852009-11-23 18:04:58 +0000819SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000820 SDValue Root = DAG.getRoot();
821
822 if (PendingExports.empty())
823 return Root;
824
825 // Turn all of the CopyToReg chains into one factored node.
826 if (Root.getOpcode() != ISD::EntryToken) {
827 unsigned i = 0, e = PendingExports.size();
828 for (; i != e; ++i) {
829 assert(PendingExports[i].getNode()->getNumOperands() > 1);
830 if (PendingExports[i].getNode()->getOperand(0) == Root)
831 break; // Don't add the root if we already indirectly depend on it.
832 }
833
834 if (i == e)
835 PendingExports.push_back(Root);
836 }
837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000839 &PendingExports[0],
840 PendingExports.size());
841 PendingExports.clear();
842 DAG.setRoot(Root);
843 return Root;
844}
845
Bill Wendling4533cac2010-01-28 21:51:40 +0000846void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
847 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
848 DAG.AssignOrdering(Node, SDNodeOrder);
849
850 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
851 AssignOrderingToNode(Node->getOperand(I).getNode());
852}
853
Dan Gohman46510a72010-04-15 01:51:59 +0000854void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000855 // Set up outgoing PHI node register values before emitting the terminator.
856 if (isa<TerminatorInst>(&I))
857 HandlePHINodesInSuccessorBlocks(I.getParent());
858
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000859 CurDebugLoc = I.getDebugLoc();
860
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000861 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000862
Dan Gohman92884f72010-04-20 15:03:56 +0000863 if (!isa<TerminatorInst>(&I) && !HasTailCall)
864 CopyToExportRegsIfNeeded(&I);
865
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000866 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000867}
868
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000869void SelectionDAGBuilder::visitPHI(const PHINode &) {
870 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
871}
872
Dan Gohman46510a72010-04-15 01:51:59 +0000873void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000874 // Note: this doesn't use InstVisitor, because it has to work with
875 // ConstantExpr's in addition to instructions.
876 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000877 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000878 // Build the switch statement using the Instruction.def file.
879#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000880 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000881#include "llvm/Instruction.def"
882 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000883
884 // Assign the ordering to the freshly created DAG nodes.
885 if (NodeMap.count(&I)) {
886 ++SDNodeOrder;
887 AssignOrderingToNode(getValue(&I).getNode());
888 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000889}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000890
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000891// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
892// generate the debug data structures now that we've seen its definition.
893void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
894 SDValue Val) {
895 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
Devang Patel4cf81c42010-08-26 23:35:15 +0000896 if (DDI.getDI()) {
897 const DbgValueInst *DI = DDI.getDI();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000898 DebugLoc dl = DDI.getdl();
899 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
Devang Patel4cf81c42010-08-26 23:35:15 +0000900 MDNode *Variable = DI->getVariable();
901 uint64_t Offset = DI->getOffset();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000902 SDDbgValue *SDV;
903 if (Val.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +0000904 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000905 SDV = DAG.getDbgValue(Variable, Val.getNode(),
906 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
907 DAG.AddDbgValue(SDV, Val.getNode(), false);
908 }
Owen Anderson95771af2011-02-25 21:41:48 +0000909 } else
Devang Patelafeaae72010-12-06 22:39:26 +0000910 DEBUG(dbgs() << "Dropping debug info for " << DI);
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000911 DanglingDebugInfoMap[V] = DanglingDebugInfo();
912 }
913}
914
Dan Gohman28a17352010-07-01 01:59:43 +0000915// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +0000916SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +0000917 // If we already have an SDValue for this value, use it. It's important
918 // to do this first, so that we don't create a CopyFromReg if we already
919 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000920 SDValue &N = NodeMap[V];
921 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000922
Dan Gohman28a17352010-07-01 01:59:43 +0000923 // If there's a virtual register allocated and initialized for this
924 // value, use it.
925 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
926 if (It != FuncInfo.ValueMap.end()) {
927 unsigned InReg = It->second;
928 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
929 SDValue Chain = DAG.getEntryNode();
Devang Patel8f314282011-01-25 18:09:58 +0000930 N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain,NULL);
931 resolveDanglingDebugInfo(V, N);
932 return N;
Dan Gohman28a17352010-07-01 01:59:43 +0000933 }
934
935 // Otherwise create a new SDValue and remember it.
936 SDValue Val = getValueImpl(V);
937 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000938 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +0000939 return Val;
940}
941
942/// getNonRegisterValue - Return an SDValue for the given Value, but
943/// don't look in FuncInfo.ValueMap for a virtual register.
944SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
945 // If we already have an SDValue for this value, use it.
946 SDValue &N = NodeMap[V];
947 if (N.getNode()) return N;
948
949 // Otherwise create a new SDValue and remember it.
950 SDValue Val = getValueImpl(V);
951 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000952 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +0000953 return Val;
954}
955
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000956/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +0000957/// Create an SDValue for the given value.
958SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Dan Gohman383b5f62010-04-17 15:32:28 +0000959 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000960 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000961
Dan Gohman383b5f62010-04-17 15:32:28 +0000962 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000963 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000964
Dan Gohman383b5f62010-04-17 15:32:28 +0000965 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Devang Patel0d881da2010-07-06 22:08:15 +0000966 return DAG.getGlobalAddress(GV, getCurDebugLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000967
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000968 if (isa<ConstantPointerNull>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000969 return DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000970
Dan Gohman383b5f62010-04-17 15:32:28 +0000971 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +0000972 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000973
Nate Begeman9008ca62009-04-27 18:41:29 +0000974 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +0000975 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000976
Dan Gohman383b5f62010-04-17 15:32:28 +0000977 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000978 visit(CE->getOpcode(), *CE);
979 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000980 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000981 return N1;
982 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000983
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000984 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
985 SmallVector<SDValue, 4> Constants;
986 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
987 OI != OE; ++OI) {
988 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000989 // If the operand is an empty aggregate, there are no values.
990 if (!Val) continue;
991 // Add each leaf value from the operand to the Constants list
992 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000993 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
994 Constants.push_back(SDValue(Val, i));
995 }
Bill Wendling87710f02009-12-21 23:47:40 +0000996
Bill Wendling4533cac2010-01-28 21:51:40 +0000997 return DAG.getMergeValues(&Constants[0], Constants.size(),
998 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000999 }
1000
Duncan Sands1df98592010-02-16 11:11:14 +00001001 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001002 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1003 "Unknown struct or array constant!");
1004
Owen Andersone50ed302009-08-10 22:56:29 +00001005 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001006 ComputeValueVTs(TLI, C->getType(), ValueVTs);
1007 unsigned NumElts = ValueVTs.size();
1008 if (NumElts == 0)
1009 return SDValue(); // empty struct
1010 SmallVector<SDValue, 4> Constants(NumElts);
1011 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001012 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001013 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001014 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001015 else if (EltVT.isFloatingPoint())
1016 Constants[i] = DAG.getConstantFP(0, EltVT);
1017 else
1018 Constants[i] = DAG.getConstant(0, EltVT);
1019 }
Bill Wendling87710f02009-12-21 23:47:40 +00001020
Bill Wendling4533cac2010-01-28 21:51:40 +00001021 return DAG.getMergeValues(&Constants[0], NumElts,
1022 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001023 }
1024
Dan Gohman383b5f62010-04-17 15:32:28 +00001025 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001026 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001027
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001028 const VectorType *VecTy = cast<VectorType>(V->getType());
1029 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001031 // Now that we know the number and type of the elements, get that number of
1032 // elements into the Ops array based on what kind of constant it is.
1033 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +00001034 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001035 for (unsigned i = 0; i != NumElements; ++i)
1036 Ops.push_back(getValue(CP->getOperand(i)));
1037 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001038 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +00001039 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001040
1041 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001042 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001043 Op = DAG.getConstantFP(0, EltVT);
1044 else
1045 Op = DAG.getConstant(0, EltVT);
1046 Ops.assign(NumElements, Op);
1047 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001048
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001049 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +00001050 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
1051 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001052 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001054 // If this is a static alloca, generate it as the frameindex instead of
1055 // computation.
1056 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1057 DenseMap<const AllocaInst*, int>::iterator SI =
1058 FuncInfo.StaticAllocaMap.find(AI);
1059 if (SI != FuncInfo.StaticAllocaMap.end())
1060 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
1061 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001062
Dan Gohman28a17352010-07-01 01:59:43 +00001063 // If this is an instruction which fast-isel has deferred, select it now.
1064 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001065 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
1066 RegsForValue RFV(*DAG.getContext(), TLI, InReg, Inst->getType());
1067 SDValue Chain = DAG.getEntryNode();
1068 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(), Chain, NULL);
Dan Gohman28a17352010-07-01 01:59:43 +00001069 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001070
Dan Gohman28a17352010-07-01 01:59:43 +00001071 llvm_unreachable("Can't get register for value!");
1072 return SDValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001073}
1074
Dan Gohman46510a72010-04-15 01:51:59 +00001075void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001076 SDValue Chain = getControlRoot();
1077 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001078 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001079
Dan Gohman7451d3e2010-05-29 17:03:36 +00001080 if (!FuncInfo.CanLowerReturn) {
1081 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001082 const Function *F = I.getParent()->getParent();
1083
1084 // Emit a store of the return value through the virtual register.
1085 // Leave Outs empty so that LowerReturn won't try to load return
1086 // registers the usual way.
1087 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001088 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001089 PtrValueVTs);
1090
1091 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1092 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001093
Owen Andersone50ed302009-08-10 22:56:29 +00001094 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001095 SmallVector<uint64_t, 4> Offsets;
1096 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001097 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001098
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001099 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001100 for (unsigned i = 0; i != NumValues; ++i) {
Chris Lattnera13b8602010-08-24 23:10:06 +00001101 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(),
1102 RetPtr.getValueType(), RetPtr,
1103 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001104 Chains[i] =
1105 DAG.getStore(Chain, getCurDebugLoc(),
1106 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
Chris Lattner84bd98a2010-09-21 18:58:22 +00001107 // FIXME: better loc info would be nice.
1108 Add, MachinePointerInfo(), false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001109 }
1110
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001111 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
1112 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001113 } else if (I.getNumOperands() != 0) {
1114 SmallVector<EVT, 4> ValueVTs;
1115 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
1116 unsigned NumValues = ValueVTs.size();
1117 if (NumValues) {
1118 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001119 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1120 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001121
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001122 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001123
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001124 const Function *F = I.getParent()->getParent();
1125 if (F->paramHasAttr(0, Attribute::SExt))
1126 ExtendKind = ISD::SIGN_EXTEND;
1127 else if (F->paramHasAttr(0, Attribute::ZExt))
1128 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001129
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001130 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
1131 VT = TLI.getTypeForExtArgOrReturn(*DAG.getContext(), VT, ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001132
1133 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
1134 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
1135 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +00001136 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001137 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
1138 &Parts[0], NumParts, PartVT, ExtendKind);
1139
1140 // 'inreg' on function refers to return value
1141 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1142 if (F->paramHasAttr(0, Attribute::InReg))
1143 Flags.setInReg();
1144
1145 // Propagate extension type if any
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001146 if (ExtendKind == ISD::SIGN_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001147 Flags.setSExt();
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001148 else if (ExtendKind == ISD::ZERO_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001149 Flags.setZExt();
1150
Dan Gohmanc9403652010-07-07 15:54:55 +00001151 for (unsigned i = 0; i < NumParts; ++i) {
1152 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
1153 /*isfixed=*/true));
1154 OutVals.push_back(Parts[i]);
1155 }
Evan Cheng3927f432009-03-25 20:20:11 +00001156 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001157 }
1158 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001159
1160 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001161 CallingConv::ID CallConv =
1162 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001163 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
Dan Gohmanc9403652010-07-07 15:54:55 +00001164 Outs, OutVals, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001165
1166 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001167 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001168 "LowerReturn didn't return a valid chain!");
1169
1170 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001171 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172}
1173
Dan Gohmanad62f532009-04-23 23:13:24 +00001174/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1175/// created for it, emit nodes to copy the value into the virtual
1176/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001177void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00001178 // Skip empty types
1179 if (V->getType()->isEmptyTy())
1180 return;
1181
Dan Gohman33b7a292010-04-16 17:15:02 +00001182 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1183 if (VMI != FuncInfo.ValueMap.end()) {
1184 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1185 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001186 }
1187}
1188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001189/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1190/// the current basic block, add it to ValueMap now so that we'll get a
1191/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001192void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001193 // No need to export constants.
1194 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001196 // Already exported?
1197 if (FuncInfo.isExportedInst(V)) return;
1198
1199 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1200 CopyValueToVirtualRegister(V, Reg);
1201}
1202
Dan Gohman46510a72010-04-15 01:51:59 +00001203bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001204 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205 // The operands of the setcc have to be in this block. We don't know
1206 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001207 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001208 // Can export from current BB.
1209 if (VI->getParent() == FromBB)
1210 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001211
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212 // Is already exported, noop.
1213 return FuncInfo.isExportedInst(V);
1214 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001215
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001216 // If this is an argument, we can export it if the BB is the entry block or
1217 // if it is already exported.
1218 if (isa<Argument>(V)) {
1219 if (FromBB == &FromBB->getParent()->getEntryBlock())
1220 return true;
1221
1222 // Otherwise, can only export this if it is already exported.
1223 return FuncInfo.isExportedInst(V);
1224 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001225
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001226 // Otherwise, constants can always be exported.
1227 return true;
1228}
1229
1230static bool InBlock(const Value *V, const BasicBlock *BB) {
1231 if (const Instruction *I = dyn_cast<Instruction>(V))
1232 return I->getParent() == BB;
1233 return true;
1234}
1235
Dan Gohmanc2277342008-10-17 21:16:08 +00001236/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1237/// This function emits a branch and is used at the leaves of an OR or an
1238/// AND operator tree.
1239///
1240void
Dan Gohman46510a72010-04-15 01:51:59 +00001241SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001242 MachineBasicBlock *TBB,
1243 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001244 MachineBasicBlock *CurBB,
1245 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001246 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001247
Dan Gohmanc2277342008-10-17 21:16:08 +00001248 // If the leaf of the tree is a comparison, merge the condition into
1249 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001250 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001251 // The operands of the cmp have to be in this block. We don't know
1252 // how to export them from some other block. If this is the first block
1253 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001254 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001255 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1256 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001257 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001258 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001259 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001260 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001261 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001262 } else {
1263 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001264 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001265 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001266
1267 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001268 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1269 SwitchCases.push_back(CB);
1270 return;
1271 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001272 }
1273
1274 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001275 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001276 NULL, TBB, FBB, CurBB);
1277 SwitchCases.push_back(CB);
1278}
1279
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001280/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001281void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001282 MachineBasicBlock *TBB,
1283 MachineBasicBlock *FBB,
1284 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001285 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001286 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001287 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001288 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001289 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001290 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1291 BOp->getParent() != CurBB->getBasicBlock() ||
1292 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1293 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001294 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001295 return;
1296 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001297
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001298 // Create TmpBB after CurBB.
1299 MachineFunction::iterator BBI = CurBB;
1300 MachineFunction &MF = DAG.getMachineFunction();
1301 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1302 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001304 if (Opc == Instruction::Or) {
1305 // Codegen X | Y as:
1306 // jmp_if_X TBB
1307 // jmp TmpBB
1308 // TmpBB:
1309 // jmp_if_Y TBB
1310 // jmp FBB
1311 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001312
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001313 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001314 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001315
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001316 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001317 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001318 } else {
1319 assert(Opc == Instruction::And && "Unknown merge op!");
1320 // Codegen X & Y as:
1321 // jmp_if_X TmpBB
1322 // jmp FBB
1323 // TmpBB:
1324 // jmp_if_Y TBB
1325 // jmp FBB
1326 //
1327 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001328
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001329 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001330 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001331
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001332 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001333 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 }
1335}
1336
1337/// If the set of cases should be emitted as a series of branches, return true.
1338/// If we should emit this as a bunch of and/or'd together conditions, return
1339/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001340bool
Dan Gohman2048b852009-11-23 18:04:58 +00001341SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001342 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001344 // If this is two comparisons of the same values or'd or and'd together, they
1345 // will get folded into a single comparison, so don't emit two blocks.
1346 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1347 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1348 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1349 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1350 return false;
1351 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001352
Chris Lattner133ce872010-01-02 00:00:03 +00001353 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1354 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1355 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1356 Cases[0].CC == Cases[1].CC &&
1357 isa<Constant>(Cases[0].CmpRHS) &&
1358 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1359 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1360 return false;
1361 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1362 return false;
1363 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00001364
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 return true;
1366}
1367
Dan Gohman46510a72010-04-15 01:51:59 +00001368void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001369 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001370
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001371 // Update machine-CFG edges.
1372 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1373
1374 // Figure out which block is immediately after the current one.
1375 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001376 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001377 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001378 NextBlock = BBI;
1379
1380 if (I.isUnconditional()) {
1381 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001382 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001383
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001384 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001385 if (Succ0MBB != NextBlock)
1386 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001387 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001388 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001389
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001390 return;
1391 }
1392
1393 // If this condition is one of the special cases we handle, do special stuff
1394 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001395 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1397
1398 // If this is a series of conditions that are or'd or and'd together, emit
1399 // this as a sequence of branches instead of setcc's with and/or operations.
Chris Lattnerde189be2010-11-30 18:12:52 +00001400 // As long as jumps are not expensive, this should improve performance.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001401 // For example, instead of something like:
1402 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001403 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001404 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001405 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406 // or C, F
1407 // jnz foo
1408 // Emit:
1409 // cmp A, B
1410 // je foo
1411 // cmp D, E
1412 // jle foo
1413 //
Dan Gohman46510a72010-04-15 01:51:59 +00001414 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Owen Anderson95771af2011-02-25 21:41:48 +00001415 if (!TLI.isJumpExpensive() &&
Chris Lattnerde189be2010-11-30 18:12:52 +00001416 BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001417 (BOp->getOpcode() == Instruction::And ||
1418 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001419 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1420 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421 // If the compares in later blocks need to use values not currently
1422 // exported from this block, export them now. This block should always
1423 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001424 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001425
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001426 // Allow some cases to be rejected.
1427 if (ShouldEmitAsBranches(SwitchCases)) {
1428 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1429 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1430 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1431 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001432
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001433 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001434 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001435 SwitchCases.erase(SwitchCases.begin());
1436 return;
1437 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001438
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001439 // Okay, we decided not to do this, remove any inserted MBB's and clear
1440 // SwitchCases.
1441 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001442 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001443
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001444 SwitchCases.clear();
1445 }
1446 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001447
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001448 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001449 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001450 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001451
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001452 // Use visitSwitchCase to actually insert the fast branch sequence for this
1453 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001454 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001455}
1456
1457/// visitSwitchCase - Emits the necessary code to represent a single node in
1458/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001459void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1460 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001461 SDValue Cond;
1462 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001463 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001464
1465 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001466 if (CB.CmpMHS == NULL) {
1467 // Fold "(X == true)" to X and "(X == false)" to !X to
1468 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001469 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001470 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001471 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001472 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001473 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001474 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001475 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001476 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001477 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001478 } else {
1479 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1480
Anton Korobeynikov23218582008-12-23 22:25:27 +00001481 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1482 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001483
1484 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001485 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486
1487 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001488 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001489 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001490 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001491 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001492 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001494 DAG.getConstant(High-Low, VT), ISD::SETULE);
1495 }
1496 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001497
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001498 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001499 SwitchBB->addSuccessor(CB.TrueBB);
1500 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001501
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001502 // Set NextBlock to be the MBB immediately after the current one, if any.
1503 // This is used to avoid emitting unnecessary branches to the next block.
1504 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001505 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001506 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001507 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001508
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001509 // If the lhs block is the next block, invert the condition so that we can
1510 // fall through to the lhs instead of the rhs block.
1511 if (CB.TrueBB == NextBlock) {
1512 std::swap(CB.TrueBB, CB.FalseBB);
1513 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001514 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001515 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001516
Dale Johannesenf5d97892009-02-04 01:48:28 +00001517 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001518 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001519 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001520
Evan Cheng266a99d2010-09-23 06:51:55 +00001521 // Insert the false branch. Do this even if it's a fall through branch,
1522 // this makes it easier to do DAG optimizations which require inverting
1523 // the branch condition.
1524 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1525 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001526
1527 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001528}
1529
1530/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001531void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001532 // Emit the code for the jump table
1533 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001534 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001535 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1536 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001537 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001538 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1539 MVT::Other, Index.getValue(1),
1540 Table, Index);
1541 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542}
1543
1544/// visitJumpTableHeader - This function emits necessary code to produce index
1545/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001546void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001547 JumpTableHeader &JTH,
1548 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001549 // Subtract the lowest switch case value from the value being switched on and
1550 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 // difference between smallest and largest cases.
1552 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001553 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001554 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001555 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001556
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001557 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001558 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001559 // can be used as an index into the jump table in a subsequent basic block.
1560 // This value may be smaller or larger than the target's pointer type, and
1561 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001562 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001563
Dan Gohman89496d02010-07-02 00:10:16 +00001564 unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001565 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1566 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567 JT.Reg = JumpTableReg;
1568
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001569 // Emit the range check for the jump table, and branch to the default block
1570 // for the switch statement if the value being switched on exceeds the largest
1571 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001572 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001573 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001574 DAG.getConstant(JTH.Last-JTH.First,VT),
1575 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001576
1577 // Set NextBlock to be the MBB immediately after the current one, if any.
1578 // This is used to avoid emitting unnecessary branches to the next block.
1579 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001580 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001581
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001582 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001583 NextBlock = BBI;
1584
Dale Johannesen66978ee2009-01-31 02:22:37 +00001585 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001587 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001588
Bill Wendling4533cac2010-01-28 21:51:40 +00001589 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001590 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1591 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001592
Bill Wendling87710f02009-12-21 23:47:40 +00001593 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001594}
1595
1596/// visitBitTestHeader - This function emits necessary code to produce value
1597/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001598void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1599 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001600 // Subtract the minimum value
1601 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001602 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001603 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001604 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001605
1606 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001607 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001608 TLI.getSetCCResultType(Sub.getValueType()),
1609 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001610 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001611
Evan Chengd08e5b42011-01-06 01:02:44 +00001612 // Determine the type of the test operands.
1613 bool UsePtrType = false;
1614 if (!TLI.isTypeLegal(VT))
1615 UsePtrType = true;
1616 else {
1617 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
1618 if ((uint64_t)((int64_t)B.Cases[i].Mask >> VT.getSizeInBits()) + 1 >= 2) {
1619 // Switch table case range are encoded into series of masks.
1620 // Just use pointer type, it's guaranteed to fit.
1621 UsePtrType = true;
1622 break;
1623 }
1624 }
1625 if (UsePtrType) {
1626 VT = TLI.getPointerTy();
1627 Sub = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), VT);
1628 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001629
Evan Chengd08e5b42011-01-06 01:02:44 +00001630 B.RegVT = VT;
1631 B.Reg = FuncInfo.CreateReg(VT);
Dale Johannesena04b7572009-02-03 23:04:43 +00001632 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001633 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001634
1635 // Set NextBlock to be the MBB immediately after the current one, if any.
1636 // This is used to avoid emitting unnecessary branches to the next block.
1637 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001638 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001639 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640 NextBlock = BBI;
1641
1642 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1643
Dan Gohman99be8ae2010-04-19 22:41:47 +00001644 SwitchBB->addSuccessor(B.Default);
1645 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001646
Dale Johannesen66978ee2009-01-31 02:22:37 +00001647 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001648 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001649 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001650
Evan Cheng8c1f4322010-09-23 18:32:19 +00001651 if (MBB != NextBlock)
1652 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1653 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001654
Bill Wendling87710f02009-12-21 23:47:40 +00001655 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656}
1657
1658/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001659void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1660 MachineBasicBlock* NextMBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001661 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001662 BitTestCase &B,
1663 MachineBasicBlock *SwitchBB) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001664 EVT VT = BB.RegVT;
1665 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1666 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001667 SDValue Cmp;
1668 if (CountPopulation_64(B.Mask) == 1) {
1669 // Testing for a single bit; just compare the shift count with what it
1670 // would need to be to shift a 1 bit in that position.
1671 Cmp = DAG.getSetCC(getCurDebugLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001672 TLI.getSetCCResultType(VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001673 ShiftOp,
Evan Chengd08e5b42011-01-06 01:02:44 +00001674 DAG.getConstant(CountTrailingZeros_64(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001675 ISD::SETEQ);
1676 } else {
1677 // Make desired shift
Evan Chengd08e5b42011-01-06 01:02:44 +00001678 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(), VT,
1679 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001680
Dan Gohman8e0163a2010-06-24 02:06:24 +00001681 // Emit bit tests and jumps
1682 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001683 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Dan Gohman8e0163a2010-06-24 02:06:24 +00001684 Cmp = DAG.getSetCC(getCurDebugLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001685 TLI.getSetCCResultType(VT),
1686 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001687 ISD::SETNE);
1688 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001689
Dan Gohman99be8ae2010-04-19 22:41:47 +00001690 SwitchBB->addSuccessor(B.TargetBB);
1691 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001692
Dale Johannesen66978ee2009-01-31 02:22:37 +00001693 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001695 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696
1697 // Set NextBlock to be the MBB immediately after the current one, if any.
1698 // This is used to avoid emitting unnecessary branches to the next block.
1699 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001700 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001701 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001702 NextBlock = BBI;
1703
Evan Cheng8c1f4322010-09-23 18:32:19 +00001704 if (NextMBB != NextBlock)
1705 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1706 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001707
Bill Wendling87710f02009-12-21 23:47:40 +00001708 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001709}
1710
Dan Gohman46510a72010-04-15 01:51:59 +00001711void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001712 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001713
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 // Retrieve successors.
1715 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1716 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1717
Gabor Greifb67e6b32009-01-15 11:10:44 +00001718 const Value *Callee(I.getCalledValue());
1719 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001720 visitInlineAsm(&I);
1721 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001722 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001723
1724 // If the value of the invoke is used outside of its defining block, make it
1725 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001726 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001727
1728 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001729 InvokeMBB->addSuccessor(Return);
1730 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001731
1732 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001733 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1734 MVT::Other, getControlRoot(),
1735 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001736}
1737
Dan Gohman46510a72010-04-15 01:51:59 +00001738void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001739}
1740
1741/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1742/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001743bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1744 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001745 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001746 MachineBasicBlock *Default,
1747 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001749
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001750 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001751 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001752 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001753 return false;
1754
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001755 // Get the MachineFunction which holds the current MBB. This is used when
1756 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001757 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001758
1759 // Figure out which block is immediately after the current one.
1760 MachineBasicBlock *NextBlock = 0;
1761 MachineFunction::iterator BBI = CR.CaseBB;
1762
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001763 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001764 NextBlock = BBI;
1765
Benjamin Kramerce750f02010-11-22 09:45:38 +00001766 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001767 // is the same as the other, but has one bit unset that the other has set,
1768 // use bit manipulation to do two compares at once. For example:
1769 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00001770 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
1771 // TODO: Handle cases where CR.CaseBB != SwitchBB.
1772 if (Size == 2 && CR.CaseBB == SwitchBB) {
1773 Case &Small = *CR.Range.first;
1774 Case &Big = *(CR.Range.second-1);
1775
1776 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
1777 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
1778 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
1779
1780 // Check that there is only one bit different.
1781 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
1782 (SmallValue | BigValue) == BigValue) {
1783 // Isolate the common bit.
1784 APInt CommonBit = BigValue & ~SmallValue;
1785 assert((SmallValue | CommonBit) == BigValue &&
1786 CommonBit.countPopulation() == 1 && "Not a common bit?");
1787
1788 SDValue CondLHS = getValue(SV);
1789 EVT VT = CondLHS.getValueType();
1790 DebugLoc DL = getCurDebugLoc();
1791
1792 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
1793 DAG.getConstant(CommonBit, VT));
1794 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
1795 Or, DAG.getConstant(BigValue, VT),
1796 ISD::SETEQ);
1797
1798 // Update successor info.
1799 SwitchBB->addSuccessor(Small.BB);
1800 SwitchBB->addSuccessor(Default);
1801
1802 // Insert the true branch.
1803 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
1804 getControlRoot(), Cond,
1805 DAG.getBasicBlock(Small.BB));
1806
1807 // Insert the false branch.
1808 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
1809 DAG.getBasicBlock(Default));
1810
1811 DAG.setRoot(BrCond);
1812 return true;
1813 }
1814 }
1815 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001816
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001817 // Rearrange the case blocks so that the last one falls through if possible.
1818 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1819 // The last case block won't fall through into 'NextBlock' if we emit the
1820 // branches in this order. See if rearranging a case value would help.
1821 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1822 if (I->BB == NextBlock) {
1823 std::swap(*I, BackCase);
1824 break;
1825 }
1826 }
1827 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001828
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001829 // Create a CaseBlock record representing a conditional branch to
1830 // the Case's target mbb if the value being switched on SV is equal
1831 // to C.
1832 MachineBasicBlock *CurBlock = CR.CaseBB;
1833 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1834 MachineBasicBlock *FallThrough;
1835 if (I != E-1) {
1836 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1837 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001838
1839 // Put SV in a virtual register to make it available from the new blocks.
1840 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001841 } else {
1842 // If the last case doesn't match, go to the default block.
1843 FallThrough = Default;
1844 }
1845
Dan Gohman46510a72010-04-15 01:51:59 +00001846 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001847 ISD::CondCode CC;
1848 if (I->High == I->Low) {
1849 // This is just small small case range :) containing exactly 1 case
1850 CC = ISD::SETEQ;
1851 LHS = SV; RHS = I->High; MHS = NULL;
1852 } else {
1853 CC = ISD::SETLE;
1854 LHS = I->Low; MHS = SV; RHS = I->High;
1855 }
1856 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001857
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001858 // If emitting the first comparison, just call visitSwitchCase to emit the
1859 // code into the current block. Otherwise, push the CaseBlock onto the
1860 // vector to be later processed by SDISel, and insert the node's MBB
1861 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001862 if (CurBlock == SwitchBB)
1863 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001864 else
1865 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001866
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001867 CurBlock = FallThrough;
1868 }
1869
1870 return true;
1871}
1872
1873static inline bool areJTsAllowed(const TargetLowering &TLI) {
1874 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1876 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001877}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001878
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001879static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001880 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Jay Foad40f8f622010-12-07 08:25:19 +00001881 APInt LastExt = Last.sext(BitWidth), FirstExt = First.sext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001882 return (LastExt - FirstExt + 1ULL);
1883}
1884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001885/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001886bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1887 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001888 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001889 MachineBasicBlock* Default,
1890 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001891 Case& FrontCase = *CR.Range.first;
1892 Case& BackCase = *(CR.Range.second-1);
1893
Chris Lattnere880efe2009-11-07 07:50:34 +00001894 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1895 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001896
Chris Lattnere880efe2009-11-07 07:50:34 +00001897 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001898 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1899 I!=E; ++I)
1900 TSize += I->size();
1901
Dan Gohmane0567812010-04-08 23:03:40 +00001902 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001904
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001905 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001906 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001907 if (Density < 0.4)
1908 return false;
1909
David Greene4b69d992010-01-05 01:24:57 +00001910 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001911 << "First entry: " << First << ". Last entry: " << Last << '\n'
1912 << "Range: " << Range
Jim Grosbach3fc83172011-02-25 03:59:03 +00001913 << ". Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001914
1915 // Get the MachineFunction which holds the current MBB. This is used when
1916 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001917 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001918
1919 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001920 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001921 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001922
1923 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1924
1925 // Create a new basic block to hold the code for loading the address
1926 // of the jump table, and jumping to it. Update successor information;
1927 // we will either branch to the default case for the switch, or the jump
1928 // table.
1929 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1930 CurMF->insert(BBI, JumpTableBB);
1931 CR.CaseBB->addSuccessor(Default);
1932 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934 // Build a vector of destination BBs, corresponding to each target
1935 // of the jump table. If the value of the jump table slot corresponds to
1936 // a case statement, push the case's BB onto the vector, otherwise, push
1937 // the default BB.
1938 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001939 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001940 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001941 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1942 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001943
1944 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945 DestBBs.push_back(I->BB);
1946 if (TEI==High)
1947 ++I;
1948 } else {
1949 DestBBs.push_back(Default);
1950 }
1951 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001952
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001953 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001954 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1955 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001956 E = DestBBs.end(); I != E; ++I) {
1957 if (!SuccsHandled[(*I)->getNumber()]) {
1958 SuccsHandled[(*I)->getNumber()] = true;
1959 JumpTableBB->addSuccessor(*I);
1960 }
1961 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001962
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001963 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001964 unsigned JTEncoding = TLI.getJumpTableEncoding();
1965 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001966 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001967
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001968 // Set the jump table information so that we can codegen it as a second
1969 // MachineBasicBlock
1970 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001971 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1972 if (CR.CaseBB == SwitchBB)
1973 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001974
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001975 JTCases.push_back(JumpTableBlock(JTH, JT));
1976
1977 return true;
1978}
1979
1980/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1981/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001982bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1983 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001984 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001985 MachineBasicBlock *Default,
1986 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001987 // Get the MachineFunction which holds the current MBB. This is used when
1988 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001989 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990
1991 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001993 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001994
1995 Case& FrontCase = *CR.Range.first;
1996 Case& BackCase = *(CR.Range.second-1);
1997 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1998
1999 // Size is the number of Cases represented by this range.
2000 unsigned Size = CR.Range.second - CR.Range.first;
2001
Chris Lattnere880efe2009-11-07 07:50:34 +00002002 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2003 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002004 double FMetric = 0;
2005 CaseItr Pivot = CR.Range.first + Size/2;
2006
2007 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2008 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002009 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002010 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2011 I!=E; ++I)
2012 TSize += I->size();
2013
Chris Lattnere880efe2009-11-07 07:50:34 +00002014 APInt LSize = FrontCase.size();
2015 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002016 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002017 << "First: " << First << ", Last: " << Last <<'\n'
2018 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002019 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2020 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002021 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2022 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002023 APInt Range = ComputeRange(LEnd, RBegin);
2024 assert((Range - 2ULL).isNonNegative() &&
2025 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002026 // Use volatile double here to avoid excess precision issues on some hosts,
2027 // e.g. that use 80-bit X87 registers.
2028 volatile double LDensity =
2029 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002030 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002031 volatile double RDensity =
2032 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002033 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002034 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002035 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002036 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002037 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2038 << "LDensity: " << LDensity
2039 << ", RDensity: " << RDensity << '\n'
2040 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002041 if (FMetric < Metric) {
2042 Pivot = J;
2043 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002044 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002045 }
2046
2047 LSize += J->size();
2048 RSize -= J->size();
2049 }
2050 if (areJTsAllowed(TLI)) {
2051 // If our case is dense we *really* should handle it earlier!
2052 assert((FMetric > 0) && "Should handle dense range earlier!");
2053 } else {
2054 Pivot = CR.Range.first + Size/2;
2055 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002057 CaseRange LHSR(CR.Range.first, Pivot);
2058 CaseRange RHSR(Pivot, CR.Range.second);
2059 Constant *C = Pivot->Low;
2060 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002061
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002062 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002063 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002064 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002065 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002066 // Pivot's Value, then we can branch directly to the LHS's Target,
2067 // rather than creating a leaf node for it.
2068 if ((LHSR.second - LHSR.first) == 1 &&
2069 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002070 cast<ConstantInt>(C)->getValue() ==
2071 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 TrueBB = LHSR.first->BB;
2073 } else {
2074 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2075 CurMF->insert(BBI, TrueBB);
2076 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002077
2078 // Put SV in a virtual register to make it available from the new blocks.
2079 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002080 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002082 // Similar to the optimization above, if the Value being switched on is
2083 // known to be less than the Constant CR.LT, and the current Case Value
2084 // is CR.LT - 1, then we can branch directly to the target block for
2085 // the current Case Value, rather than emitting a RHS leaf node for it.
2086 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002087 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2088 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002089 FalseBB = RHSR.first->BB;
2090 } else {
2091 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2092 CurMF->insert(BBI, FalseBB);
2093 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002094
2095 // Put SV in a virtual register to make it available from the new blocks.
2096 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002097 }
2098
2099 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002100 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002101 // Otherwise, branch to LHS.
2102 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
2103
Dan Gohman99be8ae2010-04-19 22:41:47 +00002104 if (CR.CaseBB == SwitchBB)
2105 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002106 else
2107 SwitchCases.push_back(CB);
2108
2109 return true;
2110}
2111
2112/// handleBitTestsSwitchCase - if current case range has few destination and
2113/// range span less, than machine word bitwidth, encode case range into series
2114/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002115bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2116 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002117 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002118 MachineBasicBlock* Default,
2119 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00002120 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002121 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002122
2123 Case& FrontCase = *CR.Range.first;
2124 Case& BackCase = *(CR.Range.second-1);
2125
2126 // Get the MachineFunction which holds the current MBB. This is used when
2127 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002128 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002130 // If target does not have legal shift left, do not emit bit tests at all.
2131 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
2132 return false;
2133
Anton Korobeynikov23218582008-12-23 22:25:27 +00002134 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002135 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2136 I!=E; ++I) {
2137 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002138 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002139 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141 // Count unique destinations
2142 SmallSet<MachineBasicBlock*, 4> Dests;
2143 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2144 Dests.insert(I->BB);
2145 if (Dests.size() > 3)
2146 // Don't bother the code below, if there are too much unique destinations
2147 return false;
2148 }
David Greene4b69d992010-01-05 01:24:57 +00002149 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002150 << Dests.size() << '\n'
2151 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002152
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002153 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002154 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2155 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002156 APInt cmpRange = maxValue - minValue;
2157
David Greene4b69d992010-01-05 01:24:57 +00002158 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002159 << "Low bound: " << minValue << '\n'
2160 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002161
Dan Gohmane0567812010-04-08 23:03:40 +00002162 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002163 (!(Dests.size() == 1 && numCmps >= 3) &&
2164 !(Dests.size() == 2 && numCmps >= 5) &&
2165 !(Dests.size() >= 3 && numCmps >= 6)))
2166 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002167
David Greene4b69d992010-01-05 01:24:57 +00002168 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002169 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2170
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002171 // Optimize the case where all the case values fit in a
2172 // word without having to subtract minValue. In this case,
2173 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00002174 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002175 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002176 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002177 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002178 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002180 CaseBitsVector CasesBits;
2181 unsigned i, count = 0;
2182
2183 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2184 MachineBasicBlock* Dest = I->BB;
2185 for (i = 0; i < count; ++i)
2186 if (Dest == CasesBits[i].BB)
2187 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002189 if (i == count) {
2190 assert((count < 3) && "Too much destinations to test!");
2191 CasesBits.push_back(CaseBits(0, Dest, 0));
2192 count++;
2193 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002194
2195 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2196 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2197
2198 uint64_t lo = (lowValue - lowBound).getZExtValue();
2199 uint64_t hi = (highValue - lowBound).getZExtValue();
2200
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002201 for (uint64_t j = lo; j <= hi; j++) {
2202 CasesBits[i].Mask |= 1ULL << j;
2203 CasesBits[i].Bits++;
2204 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002205
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002206 }
2207 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002208
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002209 BitTestInfo BTC;
2210
2211 // Figure out which block is immediately after the current one.
2212 MachineFunction::iterator BBI = CR.CaseBB;
2213 ++BBI;
2214
2215 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2216
David Greene4b69d992010-01-05 01:24:57 +00002217 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002218 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002219 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002220 << ", Bits: " << CasesBits[i].Bits
2221 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222
2223 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2224 CurMF->insert(BBI, CaseBB);
2225 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2226 CaseBB,
2227 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002228
2229 // Put SV in a virtual register to make it available from the new blocks.
2230 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002231 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002232
2233 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002234 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235 CR.CaseBB, Default, BTC);
2236
Dan Gohman99be8ae2010-04-19 22:41:47 +00002237 if (CR.CaseBB == SwitchBB)
2238 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240 BitTestCases.push_back(BTB);
2241
2242 return true;
2243}
2244
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002245/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002246size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2247 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002248 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002249
2250 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002251 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2253 Cases.push_back(Case(SI.getSuccessorValue(i),
2254 SI.getSuccessorValue(i),
2255 SMBB));
2256 }
2257 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2258
2259 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002260 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002261 // Must recompute end() each iteration because it may be
2262 // invalidated by erase if we hold on to it
Nick Lewyckyed4efd32011-01-28 04:00:15 +00002263 for (CaseItr I = Cases.begin(), J = llvm::next(Cases.begin());
2264 J != Cases.end(); ) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002265 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2266 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002267 MachineBasicBlock* nextBB = J->BB;
2268 MachineBasicBlock* currentBB = I->BB;
2269
2270 // If the two neighboring cases go to the same destination, merge them
2271 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002272 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273 I->High = J->High;
2274 J = Cases.erase(J);
2275 } else {
2276 I = J++;
2277 }
2278 }
2279
2280 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2281 if (I->Low != I->High)
2282 // A range counts double, since it requires two compares.
2283 ++numCmps;
2284 }
2285
2286 return numCmps;
2287}
2288
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002289void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2290 MachineBasicBlock *Last) {
2291 // Update JTCases.
2292 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2293 if (JTCases[i].first.HeaderBB == First)
2294 JTCases[i].first.HeaderBB = Last;
2295
2296 // Update BitTestCases.
2297 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2298 if (BitTestCases[i].Parent == First)
2299 BitTestCases[i].Parent = Last;
2300}
2301
Dan Gohman46510a72010-04-15 01:51:59 +00002302void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002303 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002304
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002305 // Figure out which block is immediately after the current one.
2306 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2308
2309 // If there is only the default destination, branch to it if it is not the
2310 // next basic block. Otherwise, just fall through.
2311 if (SI.getNumOperands() == 2) {
2312 // Update machine-CFG edges.
2313
2314 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002315 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002316 if (Default != NextBlock)
2317 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
2318 MVT::Other, getControlRoot(),
2319 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002320
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002321 return;
2322 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002323
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002324 // If there are any non-default case statements, create a vector of Cases
2325 // representing each one, and sort the vector so that we can efficiently
2326 // create a binary search tree from them.
2327 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002328 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002329 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002330 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002331 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002332
2333 // Get the Value to be switched on and default basic blocks, which will be
2334 // inserted into CaseBlock records, representing basic blocks in the binary
2335 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002336 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002337
2338 // Push the initial CaseRec onto the worklist
2339 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002340 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2341 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002342
2343 while (!WorkList.empty()) {
2344 // Grab a record representing a case range to process off the worklist
2345 CaseRec CR = WorkList.back();
2346 WorkList.pop_back();
2347
Dan Gohman99be8ae2010-04-19 22:41:47 +00002348 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002349 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002350
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002351 // If the range has few cases (two or less) emit a series of specific
2352 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002353 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002354 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002355
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002356 // If the switch has more than 5 blocks, and at least 40% dense, and the
2357 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002358 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002359 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002360 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002361
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002362 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2363 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002364 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002365 }
2366}
2367
Dan Gohman46510a72010-04-15 01:51:59 +00002368void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002369 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002370
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002371 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002372 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002373 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002374 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002375 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002376 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002377 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2378 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002379 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002380
Bill Wendling4533cac2010-01-28 21:51:40 +00002381 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2382 MVT::Other, getControlRoot(),
2383 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002384}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002385
Dan Gohman46510a72010-04-15 01:51:59 +00002386void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002387 // -0.0 - X --> fneg
2388 const Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002389 if (isa<Constant>(I.getOperand(0)) &&
2390 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2391 SDValue Op2 = getValue(I.getOperand(1));
2392 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2393 Op2.getValueType(), Op2));
2394 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002395 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002396
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002397 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002398}
2399
Dan Gohman46510a72010-04-15 01:51:59 +00002400void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002401 SDValue Op1 = getValue(I.getOperand(0));
2402 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002403 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2404 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002405}
2406
Dan Gohman46510a72010-04-15 01:51:59 +00002407void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002408 SDValue Op1 = getValue(I.getOperand(0));
2409 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002410
2411 MVT ShiftTy = TLI.getShiftAmountTy(Op2.getValueType());
2412
Chris Lattnerd3027732011-02-13 09:02:52 +00002413 // Coerce the shift amount to the right type if we can.
2414 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002415 unsigned ShiftSize = ShiftTy.getSizeInBits();
2416 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Chris Lattnerd3027732011-02-13 09:02:52 +00002417 DebugLoc DL = getCurDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002418
Dan Gohman57fc82d2009-04-09 03:51:29 +00002419 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002420 if (ShiftSize > Op2Size)
2421 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002422
Dan Gohman57fc82d2009-04-09 03:51:29 +00002423 // If the operand is larger than the shift count type but the shift
2424 // count type has enough bits to represent any shift value, truncate
2425 // it now. This is a common case and it exposes the truncate to
2426 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002427 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2428 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2429 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002430 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002431 else
Chris Lattnere0751182011-02-13 19:09:16 +00002432 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002433 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002434
Bill Wendling4533cac2010-01-28 21:51:40 +00002435 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2436 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002437}
2438
Dan Gohman46510a72010-04-15 01:51:59 +00002439void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002440 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002441 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002442 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002443 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002444 predicate = ICmpInst::Predicate(IC->getPredicate());
2445 SDValue Op1 = getValue(I.getOperand(0));
2446 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002447 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002448
Owen Andersone50ed302009-08-10 22:56:29 +00002449 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002450 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002451}
2452
Dan Gohman46510a72010-04-15 01:51:59 +00002453void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002454 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002455 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002456 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002457 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002458 predicate = FCmpInst::Predicate(FC->getPredicate());
2459 SDValue Op1 = getValue(I.getOperand(0));
2460 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002461 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002462 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002463 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002464}
2465
Dan Gohman46510a72010-04-15 01:51:59 +00002466void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002467 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002468 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2469 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002470 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002471
Bill Wendling49fcff82009-12-21 22:30:11 +00002472 SmallVector<SDValue, 4> Values(NumValues);
2473 SDValue Cond = getValue(I.getOperand(0));
2474 SDValue TrueVal = getValue(I.getOperand(1));
2475 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002476
Bill Wendling4533cac2010-01-28 21:51:40 +00002477 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002478 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002479 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2480 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002481 SDValue(TrueVal.getNode(),
2482 TrueVal.getResNo() + i),
2483 SDValue(FalseVal.getNode(),
2484 FalseVal.getResNo() + i));
2485
Bill Wendling4533cac2010-01-28 21:51:40 +00002486 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2487 DAG.getVTList(&ValueVTs[0], NumValues),
2488 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002489}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002490
Dan Gohman46510a72010-04-15 01:51:59 +00002491void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2493 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002494 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002495 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002496}
2497
Dan Gohman46510a72010-04-15 01:51:59 +00002498void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002499 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2500 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2501 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002502 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002503 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002504}
2505
Dan Gohman46510a72010-04-15 01:51:59 +00002506void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002507 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2508 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2509 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002510 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002511 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002512}
2513
Dan Gohman46510a72010-04-15 01:51:59 +00002514void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002515 // FPTrunc is never a no-op cast, no need to check
2516 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002517 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002518 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2519 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002520}
2521
Dan Gohman46510a72010-04-15 01:51:59 +00002522void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002523 // FPTrunc is never a no-op cast, no need to check
2524 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002525 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002526 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002527}
2528
Dan Gohman46510a72010-04-15 01:51:59 +00002529void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002530 // FPToUI is never a no-op cast, no need to check
2531 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002532 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002533 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002534}
2535
Dan Gohman46510a72010-04-15 01:51:59 +00002536void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002537 // FPToSI is never a no-op cast, no need to check
2538 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002539 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002540 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002541}
2542
Dan Gohman46510a72010-04-15 01:51:59 +00002543void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002544 // UIToFP is never a no-op cast, no need to check
2545 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002546 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002547 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002548}
2549
Dan Gohman46510a72010-04-15 01:51:59 +00002550void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002551 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002552 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002553 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002554 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002555}
2556
Dan Gohman46510a72010-04-15 01:51:59 +00002557void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002558 // What to do depends on the size of the integer and the size of the pointer.
2559 // We can either truncate, zero extend, or no-op, accordingly.
2560 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002561 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002562 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563}
2564
Dan Gohman46510a72010-04-15 01:51:59 +00002565void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002566 // What to do depends on the size of the integer and the size of the pointer.
2567 // We can either truncate, zero extend, or no-op, accordingly.
2568 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002569 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002570 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002571}
2572
Dan Gohman46510a72010-04-15 01:51:59 +00002573void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002574 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002575 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002576
Bill Wendling49fcff82009-12-21 22:30:11 +00002577 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002578 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002579 if (DestVT != N.getValueType())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002580 setValue(&I, DAG.getNode(ISD::BITCAST, getCurDebugLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002581 DestVT, N)); // convert types.
2582 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002583 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002584}
2585
Dan Gohman46510a72010-04-15 01:51:59 +00002586void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002587 SDValue InVec = getValue(I.getOperand(0));
2588 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002589 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002590 TLI.getPointerTy(),
2591 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002592 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2593 TLI.getValueType(I.getType()),
2594 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002595}
2596
Dan Gohman46510a72010-04-15 01:51:59 +00002597void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002598 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002599 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002600 TLI.getPointerTy(),
2601 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002602 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2603 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002604}
2605
Mon P Wangaeb06d22008-11-10 04:46:22 +00002606// Utility for visitShuffleVector - Returns true if the mask is mask starting
2607// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002608static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2609 unsigned MaskNumElts = Mask.size();
2610 for (unsigned i = 0; i != MaskNumElts; ++i)
2611 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002613 return true;
2614}
2615
Dan Gohman46510a72010-04-15 01:51:59 +00002616void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002617 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002618 SDValue Src1 = getValue(I.getOperand(0));
2619 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002620
Nate Begeman9008ca62009-04-27 18:41:29 +00002621 // Convert the ConstantVector mask operand into an array of ints, with -1
2622 // representing undef values.
2623 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002624 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002625 unsigned MaskNumElts = MaskElts.size();
2626 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002627 if (isa<UndefValue>(MaskElts[i]))
2628 Mask.push_back(-1);
2629 else
2630 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2631 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002632
Owen Andersone50ed302009-08-10 22:56:29 +00002633 EVT VT = TLI.getValueType(I.getType());
2634 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002635 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002636
Mon P Wangc7849c22008-11-16 05:06:27 +00002637 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002638 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2639 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002640 return;
2641 }
2642
2643 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002644 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2645 // Mask is longer than the source vectors and is a multiple of the source
2646 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002647 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002648 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2649 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002650 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2651 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002652 return;
2653 }
2654
Mon P Wangc7849c22008-11-16 05:06:27 +00002655 // Pad both vectors with undefs to make them the same length as the mask.
2656 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002657 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2658 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002659 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002660
Nate Begeman9008ca62009-04-27 18:41:29 +00002661 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2662 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002663 MOps1[0] = Src1;
2664 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002665
2666 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2667 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002668 &MOps1[0], NumConcat);
2669 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002670 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002671 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002672
Mon P Wangaeb06d22008-11-10 04:46:22 +00002673 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002674 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002675 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002676 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002677 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002678 MappedOps.push_back(Idx);
2679 else
2680 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002681 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002682
Bill Wendling4533cac2010-01-28 21:51:40 +00002683 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2684 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002685 return;
2686 }
2687
Mon P Wangc7849c22008-11-16 05:06:27 +00002688 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002689 // Analyze the access pattern of the vector to see if we can extract
2690 // two subvectors and do the shuffle. The analysis is done by calculating
2691 // the range of elements the mask access on both vectors.
2692 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2693 int MaxRange[2] = {-1, -1};
2694
Nate Begeman5a5ca152009-04-29 05:20:52 +00002695 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002696 int Idx = Mask[i];
2697 int Input = 0;
2698 if (Idx < 0)
2699 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002700
Nate Begeman5a5ca152009-04-29 05:20:52 +00002701 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002702 Input = 1;
2703 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002704 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002705 if (Idx > MaxRange[Input])
2706 MaxRange[Input] = Idx;
2707 if (Idx < MinRange[Input])
2708 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002709 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002710
Mon P Wangc7849c22008-11-16 05:06:27 +00002711 // Check if the access is smaller than the vector size and can we find
2712 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002713 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2714 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002715 int StartIdx[2]; // StartIdx to extract from
2716 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002717 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002718 RangeUse[Input] = 0; // Unused
2719 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002720 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002721 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002722 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002723 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002724 RangeUse[Input] = 1; // Extract from beginning of the vector
2725 StartIdx[Input] = 0;
2726 } else {
2727 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002728 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Bob Wilson5e8b8332011-01-07 04:59:04 +00002729 StartIdx[Input] + MaskNumElts <= SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002730 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002731 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002732 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002733 }
2734
Bill Wendling636e2582009-08-21 18:16:06 +00002735 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002736 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002737 return;
2738 }
2739 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2740 // Extract appropriate subvector and generate a vector shuffle
2741 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002742 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002743 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002744 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002745 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002746 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002747 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002748 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002749
Mon P Wangc7849c22008-11-16 05:06:27 +00002750 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002751 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002752 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002753 int Idx = Mask[i];
2754 if (Idx < 0)
2755 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002756 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002757 MappedOps.push_back(Idx - StartIdx[0]);
2758 else
2759 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002760 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002761
Bill Wendling4533cac2010-01-28 21:51:40 +00002762 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2763 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002764 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002765 }
2766 }
2767
Mon P Wangc7849c22008-11-16 05:06:27 +00002768 // We can't use either concat vectors or extract subvectors so fall back to
2769 // replacing the shuffle with extract and build vector.
2770 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002771 EVT EltVT = VT.getVectorElementType();
2772 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002773 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002774 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002775 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002776 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002777 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002778 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002779 SDValue Res;
2780
Nate Begeman5a5ca152009-04-29 05:20:52 +00002781 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002782 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2783 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002784 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002785 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2786 EltVT, Src2,
2787 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2788
2789 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002790 }
2791 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002792
Bill Wendling4533cac2010-01-28 21:51:40 +00002793 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2794 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002795}
2796
Dan Gohman46510a72010-04-15 01:51:59 +00002797void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002798 const Value *Op0 = I.getOperand(0);
2799 const Value *Op1 = I.getOperand(1);
2800 const Type *AggTy = I.getType();
2801 const Type *ValTy = Op1->getType();
2802 bool IntoUndef = isa<UndefValue>(Op0);
2803 bool FromUndef = isa<UndefValue>(Op1);
2804
Dan Gohman0dadb152010-10-06 16:18:29 +00002805 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.idx_begin(), I.idx_end());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002806
Owen Andersone50ed302009-08-10 22:56:29 +00002807 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002809 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002810 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2811
2812 unsigned NumAggValues = AggValueVTs.size();
2813 unsigned NumValValues = ValValueVTs.size();
2814 SmallVector<SDValue, 4> Values(NumAggValues);
2815
2816 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002817 unsigned i = 0;
2818 // Copy the beginning value(s) from the original aggregate.
2819 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002820 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002821 SDValue(Agg.getNode(), Agg.getResNo() + i);
2822 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00002823 if (NumValValues) {
2824 SDValue Val = getValue(Op1);
2825 for (; i != LinearIndex + NumValValues; ++i)
2826 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
2827 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2828 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002829 // Copy remaining value(s) from the original aggregate.
2830 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002831 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 SDValue(Agg.getNode(), Agg.getResNo() + i);
2833
Bill Wendling4533cac2010-01-28 21:51:40 +00002834 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2835 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2836 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002837}
2838
Dan Gohman46510a72010-04-15 01:51:59 +00002839void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002840 const Value *Op0 = I.getOperand(0);
2841 const Type *AggTy = Op0->getType();
2842 const Type *ValTy = I.getType();
2843 bool OutOfUndef = isa<UndefValue>(Op0);
2844
Dan Gohman0dadb152010-10-06 16:18:29 +00002845 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.idx_begin(), I.idx_end());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002846
Owen Andersone50ed302009-08-10 22:56:29 +00002847 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2849
2850 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00002851
2852 // Ignore a extractvalue that produces an empty object
2853 if (!NumValValues) {
2854 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
2855 return;
2856 }
2857
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002858 SmallVector<SDValue, 4> Values(NumValValues);
2859
2860 SDValue Agg = getValue(Op0);
2861 // Copy out the selected value(s).
2862 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2863 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002864 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002865 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002866 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002867
Bill Wendling4533cac2010-01-28 21:51:40 +00002868 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2869 DAG.getVTList(&ValValueVTs[0], NumValValues),
2870 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002871}
2872
Dan Gohman46510a72010-04-15 01:51:59 +00002873void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002874 SDValue N = getValue(I.getOperand(0));
2875 const Type *Ty = I.getOperand(0)->getType();
2876
Dan Gohman46510a72010-04-15 01:51:59 +00002877 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002878 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002879 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002880 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2881 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2882 if (Field) {
2883 // N = N + Offset
2884 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002885 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002886 DAG.getIntPtrConstant(Offset));
2887 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002888
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002889 Ty = StTy->getElementType(Field);
2890 } else {
2891 Ty = cast<SequentialType>(Ty)->getElementType();
2892
2893 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002894 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00002895 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002896 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002897 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002898 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002899 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002900 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002901 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002902 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2903 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002904 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002905 else
Evan Chengb1032a82009-02-09 20:54:38 +00002906 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002907
Dale Johannesen66978ee2009-01-31 02:22:37 +00002908 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002909 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002910 continue;
2911 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002912
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002913 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002914 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2915 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002916 SDValue IdxN = getValue(Idx);
2917
2918 // If the index is smaller or larger than intptr_t, truncate or extend
2919 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002920 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002921
2922 // If this is a multiply by a power of two, turn it into a shl
2923 // immediately. This is a very common case.
2924 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002925 if (ElementSize.isPowerOf2()) {
2926 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002927 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002928 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002929 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002930 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002931 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002932 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002933 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002934 }
2935 }
2936
Scott Michelfdc40a02009-02-17 22:15:04 +00002937 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002938 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002939 }
2940 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002941
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002942 setValue(&I, N);
2943}
2944
Dan Gohman46510a72010-04-15 01:51:59 +00002945void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002946 // If this is a fixed sized alloca in the entry block of the function,
2947 // allocate it statically on the stack.
2948 if (FuncInfo.StaticAllocaMap.count(&I))
2949 return; // getValue will auto-populate this.
2950
2951 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002952 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002953 unsigned Align =
2954 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2955 I.getAlignment());
2956
2957 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002958
Owen Andersone50ed302009-08-10 22:56:29 +00002959 EVT IntPtr = TLI.getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00002960 if (AllocSize.getValueType() != IntPtr)
2961 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
2962
2963 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr,
2964 AllocSize,
2965 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002966
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002967 // Handle alignment. If the requested alignment is less than or equal to
2968 // the stack alignment, ignore it. If the size is greater than or equal to
2969 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002970 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002971 if (Align <= StackAlign)
2972 Align = 0;
2973
2974 // Round the size of the allocation up to the stack alignment size
2975 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002976 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002977 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002978 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002980 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002981 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002982 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002983 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2984
2985 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002986 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002987 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002988 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002989 setValue(&I, DSA);
2990 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002992 // Inform the Frame Information that we have just allocated a variable-sized
2993 // object.
Eric Christopher2b8271e2010-07-17 00:28:22 +00002994 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002995}
2996
Dan Gohman46510a72010-04-15 01:51:59 +00002997void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002998 const Value *SV = I.getOperand(0);
2999 SDValue Ptr = getValue(SV);
3000
3001 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003003 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003004 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003005 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003006 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003007
Owen Andersone50ed302009-08-10 22:56:29 +00003008 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003009 SmallVector<uint64_t, 4> Offsets;
3010 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
3011 unsigned NumValues = ValueVTs.size();
3012 if (NumValues == 0)
3013 return;
3014
3015 SDValue Root;
3016 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003017 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003018 // Serialize volatile loads with other side effects.
3019 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003020 else if (AA->pointsToConstantMemory(
3021 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003022 // Do not serialize (non-volatile) loads of constant memory with anything.
3023 Root = DAG.getEntryNode();
3024 ConstantMemory = true;
3025 } else {
3026 // Do not serialize non-volatile loads against each other.
3027 Root = DAG.getRoot();
3028 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003030 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003031 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3032 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003033 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003034 unsigned ChainI = 0;
3035 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3036 // Serializing loads here may result in excessive register pressure, and
3037 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3038 // could recover a bit by hoisting nodes upward in the chain by recognizing
3039 // they are side-effect free or do not alias. The optimizer should really
3040 // avoid this case by converting large object/array copies to llvm.memcpy
3041 // (MaxParallelChains should always remain as failsafe).
3042 if (ChainI == MaxParallelChains) {
3043 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
3044 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3045 MVT::Other, &Chains[0], ChainI);
3046 Root = Chain;
3047 ChainI = 0;
3048 }
Bill Wendling856ff412009-12-22 00:12:37 +00003049 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
3050 PtrVT, Ptr,
3051 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003052 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003053 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003054 isNonTemporal, Alignment, TBAAInfo);
Bill Wendling856ff412009-12-22 00:12:37 +00003055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003056 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003057 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003058 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003059
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003060 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003061 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003062 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003063 if (isVolatile)
3064 DAG.setRoot(Chain);
3065 else
3066 PendingLoads.push_back(Chain);
3067 }
3068
Bill Wendling4533cac2010-01-28 21:51:40 +00003069 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
3070 DAG.getVTList(&ValueVTs[0], NumValues),
3071 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003072}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003073
Dan Gohman46510a72010-04-15 01:51:59 +00003074void SelectionDAGBuilder::visitStore(const StoreInst &I) {
3075 const Value *SrcV = I.getOperand(0);
3076 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003077
Owen Andersone50ed302009-08-10 22:56:29 +00003078 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003079 SmallVector<uint64_t, 4> Offsets;
3080 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3081 unsigned NumValues = ValueVTs.size();
3082 if (NumValues == 0)
3083 return;
3084
3085 // Get the lowered operands. Note that we do this after
3086 // checking if NumResults is zero, because with zero results
3087 // the operands won't have values in the map.
3088 SDValue Src = getValue(SrcV);
3089 SDValue Ptr = getValue(PtrV);
3090
3091 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003092 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3093 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003094 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003095 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003096 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003097 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003098 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003099
Andrew Trickde91f3c2010-11-12 17:50:46 +00003100 unsigned ChainI = 0;
3101 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3102 // See visitLoad comments.
3103 if (ChainI == MaxParallelChains) {
3104 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3105 MVT::Other, &Chains[0], ChainI);
3106 Root = Chain;
3107 ChainI = 0;
3108 }
Bill Wendling856ff412009-12-22 00:12:37 +00003109 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3110 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickde91f3c2010-11-12 17:50:46 +00003111 SDValue St = DAG.getStore(Root, getCurDebugLoc(),
3112 SDValue(Src.getNode(), Src.getResNo() + i),
3113 Add, MachinePointerInfo(PtrV, Offsets[i]),
3114 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3115 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003116 }
3117
Devang Patel7e13efa2010-10-26 22:14:52 +00003118 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003119 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003120 ++SDNodeOrder;
3121 AssignOrderingToNode(StoreNode.getNode());
3122 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003123}
3124
3125/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3126/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003127void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003128 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003129 bool HasChain = !I.doesNotAccessMemory();
3130 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3131
3132 // Build the operand list.
3133 SmallVector<SDValue, 8> Ops;
3134 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3135 if (OnlyLoad) {
3136 // We don't need to serialize loads against other loads.
3137 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003138 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003139 Ops.push_back(getRoot());
3140 }
3141 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003142
3143 // Info is set by getTgtMemInstrinsic
3144 TargetLowering::IntrinsicInfo Info;
3145 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3146
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003147 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003148 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3149 Info.opc == ISD::INTRINSIC_W_CHAIN)
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003150 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003151
3152 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003153 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3154 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003155 assert(TLI.isTypeLegal(Op.getValueType()) &&
3156 "Intrinsic uses a non-legal type?");
3157 Ops.push_back(Op);
3158 }
3159
Owen Andersone50ed302009-08-10 22:56:29 +00003160 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003161 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3162#ifndef NDEBUG
3163 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3164 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3165 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003166 }
Bob Wilson8d919552009-07-31 22:41:21 +00003167#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003169 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003171
Bob Wilson8d919552009-07-31 22:41:21 +00003172 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003173
3174 // Create the node.
3175 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003176 if (IsTgtIntrinsic) {
3177 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003178 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003179 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003180 Info.memVT,
3181 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003182 Info.align, Info.vol,
3183 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003184 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003185 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003186 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003187 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003188 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003189 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003190 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003191 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003192 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003193 }
3194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003195 if (HasChain) {
3196 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3197 if (OnlyLoad)
3198 PendingLoads.push_back(Chain);
3199 else
3200 DAG.setRoot(Chain);
3201 }
Bill Wendling856ff412009-12-22 00:12:37 +00003202
Benjamin Kramerf0127052010-01-05 13:12:22 +00003203 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003204 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003205 EVT VT = TLI.getValueType(PTy);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003206 Result = DAG.getNode(ISD::BITCAST, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003207 }
Bill Wendling856ff412009-12-22 00:12:37 +00003208
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003209 setValue(&I, Result);
3210 }
3211}
3212
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003213/// GetSignificand - Get the significand and build it into a floating-point
3214/// number with exponent of 1:
3215///
3216/// Op = (Op & 0x007fffff) | 0x3f800000;
3217///
3218/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003219static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00003220GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003221 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3222 DAG.getConstant(0x007fffff, MVT::i32));
3223 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3224 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003225 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003226}
3227
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003228/// GetExponent - Get the exponent:
3229///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003230/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003231///
3232/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003233static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003234GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00003235 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003236 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3237 DAG.getConstant(0x7f800000, MVT::i32));
3238 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003239 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003240 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3241 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003242 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003243}
3244
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245/// getF32Constant - Get 32-bit floating point constant.
3246static SDValue
3247getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003249}
3250
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003251/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003252/// visitIntrinsicCall: I is a call instruction
3253/// Op is the associated NodeType for I
3254const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003255SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
3256 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003257 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003258 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003259 DAG.getAtomic(Op, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00003260 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003261 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00003262 getValue(I.getArgOperand(0)),
3263 getValue(I.getArgOperand(1)),
3264 I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003265 setValue(&I, L);
3266 DAG.setRoot(L.getValue(1));
3267 return 0;
3268}
3269
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003270// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003271const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003272SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Gabor Greif0635f352010-06-25 09:38:13 +00003273 SDValue Op1 = getValue(I.getArgOperand(0));
3274 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling74c37652008-12-09 22:08:41 +00003275
Owen Anderson825b72b2009-08-11 20:47:22 +00003276 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00003277 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003278 return 0;
3279}
Bill Wendling74c37652008-12-09 22:08:41 +00003280
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003281/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3282/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003283void
Dan Gohman46510a72010-04-15 01:51:59 +00003284SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003285 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003286 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003287
Gabor Greif0635f352010-06-25 09:38:13 +00003288 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003289 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003290 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003291
3292 // Put the exponent in the right bit position for later addition to the
3293 // final result:
3294 //
3295 // #define LOG2OFe 1.4426950f
3296 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003298 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003299 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003300
3301 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3303 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003304
3305 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003306 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003307 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003308
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003309 if (LimitFloatPrecision <= 6) {
3310 // For floating-point precision of 6:
3311 //
3312 // TwoToFractionalPartOfX =
3313 // 0.997535578f +
3314 // (0.735607626f + 0.252464424f * x) * x;
3315 //
3316 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003317 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003318 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003320 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003321 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3322 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003323 getF32Constant(DAG, 0x3f7f5e7e));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003324 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BITCAST, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003325
3326 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003328 TwoToFracPartOfX, IntegerPartOfX);
3329
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003330 result = DAG.getNode(ISD::BITCAST, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003331 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3332 // For floating-point precision of 12:
3333 //
3334 // TwoToFractionalPartOfX =
3335 // 0.999892986f +
3336 // (0.696457318f +
3337 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3338 //
3339 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003341 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003343 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3345 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003346 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3348 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x3f7ff8fd));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003350 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BITCAST, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003351
3352 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003354 TwoToFracPartOfX, IntegerPartOfX);
3355
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003356 result = DAG.getNode(ISD::BITCAST, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003357 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3358 // For floating-point precision of 18:
3359 //
3360 // TwoToFractionalPartOfX =
3361 // 0.999999982f +
3362 // (0.693148872f +
3363 // (0.240227044f +
3364 // (0.554906021e-1f +
3365 // (0.961591928e-2f +
3366 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3367 //
3368 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003370 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003371 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003372 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3374 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003375 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3377 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003378 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3380 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003381 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003382 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3383 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003384 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003385 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3386 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003387 getF32Constant(DAG, 0x3f800000));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003388 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003389 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003390
3391 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003393 TwoToFracPartOfX, IntegerPartOfX);
3394
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003395 result = DAG.getNode(ISD::BITCAST, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003396 }
3397 } else {
3398 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003399 result = DAG.getNode(ISD::FEXP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003400 getValue(I.getArgOperand(0)).getValueType(),
3401 getValue(I.getArgOperand(0)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003402 }
3403
Dale Johannesen59e577f2008-09-05 18:38:42 +00003404 setValue(&I, result);
3405}
3406
Bill Wendling39150252008-09-09 20:39:27 +00003407/// visitLog - Lower a log intrinsic. Handles the special sequences for
3408/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003409void
Dan Gohman46510a72010-04-15 01:51:59 +00003410SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003411 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003412 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003413
Gabor Greif0635f352010-06-25 09:38:13 +00003414 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003415 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003416 SDValue Op = getValue(I.getArgOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003417 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003418
3419 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003420 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003422 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003423
3424 // Get the significand and build it into a floating-point number with
3425 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003426 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003427
3428 if (LimitFloatPrecision <= 6) {
3429 // For floating-point precision of 6:
3430 //
3431 // LogofMantissa =
3432 // -1.1609546f +
3433 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003434 //
Bill Wendling39150252008-09-09 20:39:27 +00003435 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003436 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003437 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003438 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003439 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3441 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003442 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003443
Scott Michelfdc40a02009-02-17 22:15:04 +00003444 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003446 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3447 // For floating-point precision of 12:
3448 //
3449 // LogOfMantissa =
3450 // -1.7417939f +
3451 // (2.8212026f +
3452 // (-1.4699568f +
3453 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3454 //
3455 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003457 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003459 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003460 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3461 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003462 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3464 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003465 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3467 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003468 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003469
Scott Michelfdc40a02009-02-17 22:15:04 +00003470 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003472 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3473 // For floating-point precision of 18:
3474 //
3475 // LogOfMantissa =
3476 // -2.1072184f +
3477 // (4.2372794f +
3478 // (-3.7029485f +
3479 // (2.2781945f +
3480 // (-0.87823314f +
3481 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3482 //
3483 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003484 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003485 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003487 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003488 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3489 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003490 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3492 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003493 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003494 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3495 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003496 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3498 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003499 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003500 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3501 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003502 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003503
Scott Michelfdc40a02009-02-17 22:15:04 +00003504 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003505 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003506 }
3507 } else {
3508 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003509 result = DAG.getNode(ISD::FLOG, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003510 getValue(I.getArgOperand(0)).getValueType(),
3511 getValue(I.getArgOperand(0)));
Bill Wendling39150252008-09-09 20:39:27 +00003512 }
3513
Dale Johannesen59e577f2008-09-05 18:38:42 +00003514 setValue(&I, result);
3515}
3516
Bill Wendling3eb59402008-09-09 00:28:24 +00003517/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3518/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003519void
Dan Gohman46510a72010-04-15 01:51:59 +00003520SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003521 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003522 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003523
Gabor Greif0635f352010-06-25 09:38:13 +00003524 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003525 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003526 SDValue Op = getValue(I.getArgOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003527 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003528
Bill Wendling39150252008-09-09 20:39:27 +00003529 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003530 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003531
Bill Wendling3eb59402008-09-09 00:28:24 +00003532 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003533 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003534 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003535
Bill Wendling3eb59402008-09-09 00:28:24 +00003536 // Different possible minimax approximations of significand in
3537 // floating-point for various degrees of accuracy over [1,2].
3538 if (LimitFloatPrecision <= 6) {
3539 // For floating-point precision of 6:
3540 //
3541 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3542 //
3543 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003545 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003547 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3549 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003550 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003551
Scott Michelfdc40a02009-02-17 22:15:04 +00003552 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003554 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3555 // For floating-point precision of 12:
3556 //
3557 // Log2ofMantissa =
3558 // -2.51285454f +
3559 // (4.07009056f +
3560 // (-2.12067489f +
3561 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003562 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003563 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003565 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003566 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003567 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003568 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3569 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003570 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3572 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003573 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3575 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003576 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003577
Scott Michelfdc40a02009-02-17 22:15:04 +00003578 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003580 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3581 // For floating-point precision of 18:
3582 //
3583 // Log2ofMantissa =
3584 // -3.0400495f +
3585 // (6.1129976f +
3586 // (-5.3420409f +
3587 // (3.2865683f +
3588 // (-1.2669343f +
3589 // (0.27515199f -
3590 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3591 //
3592 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003593 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003594 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003596 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3598 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003599 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3601 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003602 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003603 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3604 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003605 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003606 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3607 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003608 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3610 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003611 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003612
Scott Michelfdc40a02009-02-17 22:15:04 +00003613 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003615 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003616 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003617 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003618 result = DAG.getNode(ISD::FLOG2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003619 getValue(I.getArgOperand(0)).getValueType(),
3620 getValue(I.getArgOperand(0)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003621 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003622
Dale Johannesen59e577f2008-09-05 18:38:42 +00003623 setValue(&I, result);
3624}
3625
Bill Wendling3eb59402008-09-09 00:28:24 +00003626/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3627/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003628void
Dan Gohman46510a72010-04-15 01:51:59 +00003629SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003630 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003631 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003632
Gabor Greif0635f352010-06-25 09:38:13 +00003633 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003634 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003635 SDValue Op = getValue(I.getArgOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003636 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003637
Bill Wendling39150252008-09-09 20:39:27 +00003638 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003639 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003640 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003641 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003642
3643 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003644 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003645 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003646
3647 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003648 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003649 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003650 // Log10ofMantissa =
3651 // -0.50419619f +
3652 // (0.60948995f - 0.10380950f * x) * x;
3653 //
3654 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003656 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003657 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003658 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003659 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3660 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003661 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003662
Scott Michelfdc40a02009-02-17 22:15:04 +00003663 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003664 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003665 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3666 // For floating-point precision of 12:
3667 //
3668 // Log10ofMantissa =
3669 // -0.64831180f +
3670 // (0.91751397f +
3671 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3672 //
3673 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003674 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003675 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003676 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003677 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003678 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3679 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003680 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003681 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3682 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003683 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003684
Scott Michelfdc40a02009-02-17 22:15:04 +00003685 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003686 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003687 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003688 // For floating-point precision of 18:
3689 //
3690 // Log10ofMantissa =
3691 // -0.84299375f +
3692 // (1.5327582f +
3693 // (-1.0688956f +
3694 // (0.49102474f +
3695 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3696 //
3697 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003698 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003699 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003700 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003701 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003702 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3703 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003704 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003705 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3706 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003707 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003708 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3709 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003710 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003711 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3712 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003713 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003714
Scott Michelfdc40a02009-02-17 22:15:04 +00003715 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003717 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003718 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003719 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003720 result = DAG.getNode(ISD::FLOG10, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003721 getValue(I.getArgOperand(0)).getValueType(),
3722 getValue(I.getArgOperand(0)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003723 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003724
Dale Johannesen59e577f2008-09-05 18:38:42 +00003725 setValue(&I, result);
3726}
3727
Bill Wendlinge10c8142008-09-09 22:39:21 +00003728/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3729/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003730void
Dan Gohman46510a72010-04-15 01:51:59 +00003731SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003732 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003733 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003734
Gabor Greif0635f352010-06-25 09:38:13 +00003735 if (getValue(I.getArgOperand(0)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003736 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003737 SDValue Op = getValue(I.getArgOperand(0));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003738
Owen Anderson825b72b2009-08-11 20:47:22 +00003739 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003740
3741 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003742 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3743 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003744
3745 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003746 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003747 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003748
3749 if (LimitFloatPrecision <= 6) {
3750 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003751 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003752 // TwoToFractionalPartOfX =
3753 // 0.997535578f +
3754 // (0.735607626f + 0.252464424f * x) * x;
3755 //
3756 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003757 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003758 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003759 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003760 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003761 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3762 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003763 getF32Constant(DAG, 0x3f7f5e7e));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003764 SDValue t6 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003765 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003766 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003767
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003768 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003769 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003770 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3771 // For floating-point precision of 12:
3772 //
3773 // TwoToFractionalPartOfX =
3774 // 0.999892986f +
3775 // (0.696457318f +
3776 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3777 //
3778 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003779 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003780 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003781 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003782 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003783 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3784 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003785 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003786 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3787 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003788 getF32Constant(DAG, 0x3f7ff8fd));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003789 SDValue t8 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003790 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003791 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003792
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003793 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003795 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3796 // For floating-point precision of 18:
3797 //
3798 // TwoToFractionalPartOfX =
3799 // 0.999999982f +
3800 // (0.693148872f +
3801 // (0.240227044f +
3802 // (0.554906021e-1f +
3803 // (0.961591928e-2f +
3804 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3805 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003806 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003807 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003808 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003809 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003810 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3811 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003812 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3814 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003815 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3817 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003818 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003819 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3820 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003821 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3823 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003824 getF32Constant(DAG, 0x3f800000));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003825 SDValue t14 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003826 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003827 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003828
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003829 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003830 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003831 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003832 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003833 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003834 result = DAG.getNode(ISD::FEXP2, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003835 getValue(I.getArgOperand(0)).getValueType(),
3836 getValue(I.getArgOperand(0)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003837 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003838
Dale Johannesen601d3c02008-09-05 01:48:15 +00003839 setValue(&I, result);
3840}
3841
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003842/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3843/// limited-precision mode with x == 10.0f.
3844void
Dan Gohman46510a72010-04-15 01:51:59 +00003845SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003846 SDValue result;
Gabor Greif0635f352010-06-25 09:38:13 +00003847 const Value *Val = I.getArgOperand(0);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003848 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003849 bool IsExp10 = false;
3850
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 if (getValue(Val).getValueType() == MVT::f32 &&
Gabor Greif0635f352010-06-25 09:38:13 +00003852 getValue(I.getArgOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003853 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3854 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3855 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3856 APFloat Ten(10.0f);
3857 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3858 }
3859 }
3860 }
3861
3862 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Gabor Greif0635f352010-06-25 09:38:13 +00003863 SDValue Op = getValue(I.getArgOperand(1));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003864
3865 // Put the exponent in the right bit position for later addition to the
3866 // final result:
3867 //
3868 // #define LOG2OF10 3.3219281f
3869 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003870 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003871 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003872 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003873
3874 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003875 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3876 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003877
3878 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003879 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003880 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003881
3882 if (LimitFloatPrecision <= 6) {
3883 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003884 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003885 // twoToFractionalPartOfX =
3886 // 0.997535578f +
3887 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003888 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003889 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003890 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003891 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003892 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003893 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003894 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3895 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003896 getF32Constant(DAG, 0x3f7f5e7e));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003897 SDValue t6 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003898 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003899 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003900
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003901 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003902 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003903 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3904 // For floating-point precision of 12:
3905 //
3906 // TwoToFractionalPartOfX =
3907 // 0.999892986f +
3908 // (0.696457318f +
3909 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3910 //
3911 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003912 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003913 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003915 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003916 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3917 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003918 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003919 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3920 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003921 getF32Constant(DAG, 0x3f7ff8fd));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003922 SDValue t8 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003923 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003924 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003925
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003926 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003927 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003928 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3929 // For floating-point precision of 18:
3930 //
3931 // TwoToFractionalPartOfX =
3932 // 0.999999982f +
3933 // (0.693148872f +
3934 // (0.240227044f +
3935 // (0.554906021e-1f +
3936 // (0.961591928e-2f +
3937 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3938 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003939 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003940 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003941 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003942 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003943 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3944 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003945 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003946 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3947 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003948 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3950 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003951 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003952 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3953 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003954 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003955 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3956 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003957 getF32Constant(DAG, 0x3f800000));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003958 SDValue t14 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003959 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003960 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003961
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003962 result = DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003963 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003964 }
3965 } else {
3966 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003967 result = DAG.getNode(ISD::FPOW, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00003968 getValue(I.getArgOperand(0)).getValueType(),
3969 getValue(I.getArgOperand(0)),
3970 getValue(I.getArgOperand(1)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003971 }
3972
3973 setValue(&I, result);
3974}
3975
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003976
3977/// ExpandPowI - Expand a llvm.powi intrinsic.
3978static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3979 SelectionDAG &DAG) {
3980 // If RHS is a constant, we can expand this out to a multiplication tree,
3981 // otherwise we end up lowering to a call to __powidf2 (for example). When
3982 // optimizing for size, we only want to do this if the expansion would produce
3983 // a small number of multiplies, otherwise we do the full expansion.
3984 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3985 // Get the exponent as a positive value.
3986 unsigned Val = RHSC->getSExtValue();
3987 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003988
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003989 // powi(x, 0) -> 1.0
3990 if (Val == 0)
3991 return DAG.getConstantFP(1.0, LHS.getValueType());
3992
Dan Gohmanae541aa2010-04-15 04:33:49 +00003993 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003994 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3995 // If optimizing for size, don't insert too many multiplies. This
3996 // inserts up to 5 multiplies.
3997 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3998 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003999 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004000 // powi(x,15) generates one more multiply than it should), but this has
4001 // the benefit of being both really simple and much better than a libcall.
4002 SDValue Res; // Logically starts equal to 1.0
4003 SDValue CurSquare = LHS;
4004 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004005 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004006 if (Res.getNode())
4007 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4008 else
4009 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004010 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004011
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004012 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4013 CurSquare, CurSquare);
4014 Val >>= 1;
4015 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004016
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004017 // If the original was negative, invert the result, producing 1/(x*x*x).
4018 if (RHSC->getSExtValue() < 0)
4019 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4020 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4021 return Res;
4022 }
4023 }
4024
4025 // Otherwise, expand to a libcall.
4026 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4027}
4028
Devang Patel227dfdb2011-05-16 21:24:05 +00004029// getTruncatedArgReg - Find underlying register used for an truncated
4030// argument.
4031static unsigned getTruncatedArgReg(const SDValue &N) {
4032 if (N.getOpcode() != ISD::TRUNCATE)
4033 return 0;
4034
4035 const SDValue &Ext = N.getOperand(0);
4036 if (Ext.getOpcode() == ISD::AssertZext || Ext.getOpcode() == ISD::AssertSext){
4037 const SDValue &CFR = Ext.getOperand(0);
4038 if (CFR.getOpcode() == ISD::CopyFromReg)
4039 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
4040 else
4041 if (CFR.getOpcode() == ISD::TRUNCATE)
4042 return getTruncatedArgReg(CFR);
4043 }
4044 return 0;
4045}
4046
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004047/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4048/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4049/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004050bool
Devang Patel78a06e52010-08-25 20:39:26 +00004051SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004052 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004053 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004054 const Argument *Arg = dyn_cast<Argument>(V);
4055 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004056 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004057
Devang Patel719f6a92010-04-29 20:40:36 +00004058 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004059 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
4060 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4061
Devang Patela83ce982010-04-29 18:50:36 +00004062 // Ignore inlined function arguments here.
4063 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004064 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004065 return false;
4066
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004067 unsigned Reg = 0;
Devang Patel0b48ead2010-08-31 22:22:42 +00004068 if (Arg->hasByValAttr()) {
4069 // Byval arguments' frame index is recorded during argument lowering.
4070 // Use this info directly.
Devang Patel0b48ead2010-08-31 22:22:42 +00004071 Reg = TRI->getFrameRegister(MF);
4072 Offset = FuncInfo.getByValArgumentFrameIndex(Arg);
Devang Patel27f46cd2010-10-01 19:00:44 +00004073 // If byval argument ofset is not recorded then ignore this.
4074 if (!Offset)
4075 Reg = 0;
Devang Patel0b48ead2010-08-31 22:22:42 +00004076 }
4077
Devang Patel227dfdb2011-05-16 21:24:05 +00004078 if (N.getNode()) {
4079 if (N.getOpcode() == ISD::CopyFromReg)
4080 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4081 else
4082 Reg = getTruncatedArgReg(N);
4083 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004084 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4085 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4086 if (PR)
4087 Reg = PR;
4088 }
4089 }
4090
Evan Chenga36acad2010-04-29 06:33:38 +00004091 if (!Reg) {
Devang Patela90b3052010-11-02 17:01:30 +00004092 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004093 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004094 if (VMI != FuncInfo.ValueMap.end())
4095 Reg = VMI->second;
Evan Chenga36acad2010-04-29 06:33:38 +00004096 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004097
Devang Patel8bc9ef72010-11-02 17:19:03 +00004098 if (!Reg && N.getNode()) {
Devang Patela90b3052010-11-02 17:01:30 +00004099 // Check if frame index is available.
4100 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004101 if (FrameIndexSDNode *FINode =
Devang Patela90b3052010-11-02 17:01:30 +00004102 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) {
4103 Reg = TRI->getFrameRegister(MF);
4104 Offset = FINode->getIndex();
4105 }
Devang Patel8bc9ef72010-11-02 17:19:03 +00004106 }
4107
4108 if (!Reg)
4109 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004110
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004111 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
4112 TII->get(TargetOpcode::DBG_VALUE))
Evan Chenga36acad2010-04-29 06:33:38 +00004113 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004114 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004115 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004116}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004117
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004118// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004119#if defined(_MSC_VER) && defined(setjmp) && \
4120 !defined(setjmp_undefined_for_msvc)
4121# pragma push_macro("setjmp")
4122# undef setjmp
4123# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004124#endif
4125
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004126/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4127/// we want to emit this as a call to a named external function, return the name
4128/// otherwise lower it and return null.
4129const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004130SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004131 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004132 SDValue Res;
4133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004134 switch (Intrinsic) {
4135 default:
4136 // By default, turn this into a target intrinsic node.
4137 visitTargetIntrinsic(I, Intrinsic);
4138 return 0;
4139 case Intrinsic::vastart: visitVAStart(I); return 0;
4140 case Intrinsic::vaend: visitVAEnd(I); return 0;
4141 case Intrinsic::vacopy: visitVACopy(I); return 0;
4142 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00004143 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004144 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004145 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004146 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00004147 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004148 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004149 return 0;
4150 case Intrinsic::setjmp:
4151 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004152 case Intrinsic::longjmp:
4153 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004154 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004155 // Assert for address < 256 since we support only user defined address
4156 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004157 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004158 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004159 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004160 < 256 &&
4161 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004162 SDValue Op1 = getValue(I.getArgOperand(0));
4163 SDValue Op2 = getValue(I.getArgOperand(1));
4164 SDValue Op3 = getValue(I.getArgOperand(2));
4165 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4166 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00004167 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004168 MachinePointerInfo(I.getArgOperand(0)),
4169 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004170 return 0;
4171 }
Chris Lattner824b9582008-11-21 16:42:48 +00004172 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004173 // Assert for address < 256 since we support only user defined address
4174 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004175 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004176 < 256 &&
4177 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004178 SDValue Op1 = getValue(I.getArgOperand(0));
4179 SDValue Op2 = getValue(I.getArgOperand(1));
4180 SDValue Op3 = getValue(I.getArgOperand(2));
4181 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4182 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00004183 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004184 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004185 return 0;
4186 }
Chris Lattner824b9582008-11-21 16:42:48 +00004187 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004188 // Assert for address < 256 since we support only user defined address
4189 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004190 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004191 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004192 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004193 < 256 &&
4194 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004195 SDValue Op1 = getValue(I.getArgOperand(0));
4196 SDValue Op2 = getValue(I.getArgOperand(1));
4197 SDValue Op3 = getValue(I.getArgOperand(2));
4198 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
4199 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00004200 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004201 MachinePointerInfo(I.getArgOperand(0)),
4202 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004203 return 0;
4204 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004205 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004206 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004207 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004208 const Value *Address = DI.getAddress();
Devang Patel8e741ed2010-09-02 21:02:27 +00004209 if (!Address || !DIVariable(DI.getVariable()).Verify())
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004210 return 0;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004211
4212 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4213 // but do not always have a corresponding SDNode built. The SDNodeOrder
4214 // absolute, but not relative, values are different depending on whether
4215 // debug info exists.
4216 ++SDNodeOrder;
Devang Patel3f74a112010-09-02 21:29:42 +00004217
4218 // Check if address has undef value.
4219 if (isa<UndefValue>(Address) ||
4220 (Address->use_empty() && !isa<Argument>(Address))) {
Devang Patelafeaae72010-12-06 22:39:26 +00004221 DEBUG(dbgs() << "Dropping debug info for " << DI);
Devang Patel3f74a112010-09-02 21:29:42 +00004222 return 0;
4223 }
4224
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004225 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004226 if (!N.getNode() && isa<Argument>(Address))
4227 // Check unused arguments map.
4228 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004229 SDDbgValue *SDV;
4230 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004231 // Parameters are handled specially.
Michael J. Spencere70c5262010-10-16 08:25:21 +00004232 bool isParameter =
Devang Patel8e741ed2010-09-02 21:02:27 +00004233 DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable;
4234 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4235 Address = BCI->getOperand(0);
4236 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4237
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004238 if (isParameter && !AI) {
4239 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4240 if (FINode)
4241 // Byval parameter. We have a frame index at this point.
4242 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4243 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004244 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004245 // Address is an argument, so try to emit its dbg value using
4246 // virtual register info from the FuncInfo.ValueMap.
4247 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004248 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004249 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004250 } else if (AI)
4251 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4252 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004253 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004254 // Can't do anything with other non-AI cases yet.
Devang Patelafeaae72010-12-06 22:39:26 +00004255 DEBUG(dbgs() << "Dropping debug info for " << DI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004256 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004257 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004258 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4259 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004260 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004261 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004262 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004263 // If variable is pinned by a alloca in dominating bb then
4264 // use StaticAllocaMap.
4265 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004266 if (AI->getParent() != DI.getParent()) {
4267 DenseMap<const AllocaInst*, int>::iterator SI =
4268 FuncInfo.StaticAllocaMap.find(AI);
4269 if (SI != FuncInfo.StaticAllocaMap.end()) {
4270 SDV = DAG.getDbgValue(Variable, SI->second,
4271 0, dl, SDNodeOrder);
4272 DAG.AddDbgValue(SDV, 0, false);
4273 return 0;
4274 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004275 }
4276 }
Devang Patelafeaae72010-12-06 22:39:26 +00004277 DEBUG(dbgs() << "Dropping debug info for " << DI);
Devang Patel6cd467b2010-08-26 22:53:27 +00004278 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004279 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004280 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004281 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004282 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004283 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004284 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004285 return 0;
4286
4287 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004288 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004289 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004290 if (!V)
4291 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004292
4293 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
4294 // but do not always have a corresponding SDNode built. The SDNodeOrder
4295 // absolute, but not relative, values are different depending on whether
4296 // debug info exists.
4297 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004298 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00004299 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004300 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4301 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004302 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004303 // Do not use getValue() in here; we don't want to generate code at
4304 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004305 SDValue N = NodeMap[V];
4306 if (!N.getNode() && isa<Argument>(V))
4307 // Check unused arguments map.
4308 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004309 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004310 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004311 SDV = DAG.getDbgValue(Variable, N.getNode(),
4312 N.getResNo(), Offset, dl, SDNodeOrder);
4313 DAG.AddDbgValue(SDV, N.getNode(), false);
4314 }
Devang Patela778f5c2011-02-18 22:43:42 +00004315 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004316 // Do not call getValue(V) yet, as we don't want to generate code.
4317 // Remember it for later.
4318 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4319 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004320 } else {
Devang Patel00190342010-03-15 19:15:44 +00004321 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004322 // data available is an unreferenced parameter.
4323 DEBUG(dbgs() << "Dropping debug info for " << DI);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004324 }
Devang Patel00190342010-03-15 19:15:44 +00004325 }
4326
4327 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004328 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004329 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004330 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004331 // Don't handle byval struct arguments or VLAs, for example.
4332 if (!AI)
4333 return 0;
4334 DenseMap<const AllocaInst*, int>::iterator SI =
4335 FuncInfo.StaticAllocaMap.find(AI);
4336 if (SI == FuncInfo.StaticAllocaMap.end())
4337 return 0; // VLAs.
4338 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004339
Chris Lattner512063d2010-04-05 06:19:28 +00004340 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4341 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4342 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004343 return 0;
4344 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004345 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004346 // Insert the EXCEPTIONADDR instruction.
Dan Gohman84023e02010-07-10 09:00:22 +00004347 assert(FuncInfo.MBB->isLandingPad() &&
Dan Gohman99be8ae2010-04-19 22:41:47 +00004348 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004350 SDValue Ops[1];
4351 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004352 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004353 setValue(&I, Op);
4354 DAG.setRoot(Op.getValue(1));
4355 return 0;
4356 }
4357
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004358 case Intrinsic::eh_selector: {
Dan Gohman84023e02010-07-10 09:00:22 +00004359 MachineBasicBlock *CallMBB = FuncInfo.MBB;
Chris Lattner512063d2010-04-05 06:19:28 +00004360 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00004361 if (CallMBB->isLandingPad())
4362 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004363 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004364#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004365 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004366#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004367 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4368 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman84023e02010-07-10 09:00:22 +00004369 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004370 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004371
Chris Lattner3a5815f2009-09-17 23:54:54 +00004372 // Insert the EHSELECTION instruction.
4373 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4374 SDValue Ops[2];
Gabor Greif0635f352010-06-25 09:38:13 +00004375 Ops[0] = getValue(I.getArgOperand(0));
Chris Lattner3a5815f2009-09-17 23:54:54 +00004376 Ops[1] = getRoot();
4377 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00004378 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004379 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004380 return 0;
4381 }
4382
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004383 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004384 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004385 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004386 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4387 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004388 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004389 return 0;
4390 }
4391
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004392 case Intrinsic::eh_return_i32:
4393 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004394 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
4395 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
4396 MVT::Other,
4397 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004398 getValue(I.getArgOperand(0)),
4399 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004400 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004401 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004402 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004403 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004404 case Intrinsic::eh_dwarf_cfa: {
Gabor Greif0635f352010-06-25 09:38:13 +00004405 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004406 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004407 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004408 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004409 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004410 TLI.getPointerTy()),
4411 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004412 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004413 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004414 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00004415 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4416 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004417 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004418 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004419 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004420 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004421 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004422 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004423 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004424
Chris Lattner512063d2010-04-05 06:19:28 +00004425 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004426 return 0;
4427 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004428 case Intrinsic::eh_sjlj_setjmp: {
4429 setValue(&I, DAG.getNode(ISD::EH_SJLJ_SETJMP, dl, MVT::i32, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004430 getValue(I.getArgOperand(0))));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004431 return 0;
4432 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004433 case Intrinsic::eh_sjlj_longjmp: {
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004434 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, dl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004435 getRoot(), getValue(I.getArgOperand(0))));
4436 return 0;
4437 }
4438 case Intrinsic::eh_sjlj_dispatch_setup: {
4439 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00004440 getRoot(), getValue(I.getArgOperand(0))));
Jim Grosbach5eb19512010-05-22 01:06:18 +00004441 return 0;
4442 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004443
Dale Johannesen0488fb62010-09-30 23:57:10 +00004444 case Intrinsic::x86_mmx_pslli_w:
4445 case Intrinsic::x86_mmx_pslli_d:
4446 case Intrinsic::x86_mmx_pslli_q:
4447 case Intrinsic::x86_mmx_psrli_w:
4448 case Intrinsic::x86_mmx_psrli_d:
4449 case Intrinsic::x86_mmx_psrli_q:
4450 case Intrinsic::x86_mmx_psrai_w:
4451 case Intrinsic::x86_mmx_psrai_d: {
4452 SDValue ShAmt = getValue(I.getArgOperand(1));
4453 if (isa<ConstantSDNode>(ShAmt)) {
4454 visitTargetIntrinsic(I, Intrinsic);
4455 return 0;
4456 }
4457 unsigned NewIntrinsic = 0;
4458 EVT ShAmtVT = MVT::v2i32;
4459 switch (Intrinsic) {
4460 case Intrinsic::x86_mmx_pslli_w:
4461 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4462 break;
4463 case Intrinsic::x86_mmx_pslli_d:
4464 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4465 break;
4466 case Intrinsic::x86_mmx_pslli_q:
4467 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4468 break;
4469 case Intrinsic::x86_mmx_psrli_w:
4470 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4471 break;
4472 case Intrinsic::x86_mmx_psrli_d:
4473 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4474 break;
4475 case Intrinsic::x86_mmx_psrli_q:
4476 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4477 break;
4478 case Intrinsic::x86_mmx_psrai_w:
4479 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4480 break;
4481 case Intrinsic::x86_mmx_psrai_d:
4482 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4483 break;
4484 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4485 }
4486
4487 // The vector shift intrinsics with scalars uses 32b shift amounts but
4488 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4489 // to be zero.
4490 // We must do this early because v2i32 is not a legal type.
4491 DebugLoc dl = getCurDebugLoc();
4492 SDValue ShOps[2];
4493 ShOps[0] = ShAmt;
4494 ShOps[1] = DAG.getConstant(0, MVT::i32);
4495 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
4496 EVT DestVT = TLI.getValueType(I.getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004497 ShAmt = DAG.getNode(ISD::BITCAST, dl, DestVT, ShAmt);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004498 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
4499 DAG.getConstant(NewIntrinsic, MVT::i32),
4500 getValue(I.getArgOperand(0)), ShAmt);
4501 setValue(&I, Res);
4502 return 0;
4503 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004504 case Intrinsic::convertff:
4505 case Intrinsic::convertfsi:
4506 case Intrinsic::convertfui:
4507 case Intrinsic::convertsif:
4508 case Intrinsic::convertuif:
4509 case Intrinsic::convertss:
4510 case Intrinsic::convertsu:
4511 case Intrinsic::convertus:
4512 case Intrinsic::convertuu: {
4513 ISD::CvtCode Code = ISD::CVT_INVALID;
4514 switch (Intrinsic) {
4515 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4516 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4517 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4518 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4519 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4520 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4521 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4522 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4523 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4524 }
Owen Andersone50ed302009-08-10 22:56:29 +00004525 EVT DestVT = TLI.getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004526 const Value *Op1 = I.getArgOperand(0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004527 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4528 DAG.getValueType(DestVT),
4529 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004530 getValue(I.getArgOperand(1)),
4531 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004532 Code);
4533 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004534 return 0;
4535 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004536 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004537 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004538 getValue(I.getArgOperand(0)).getValueType(),
4539 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004540 return 0;
4541 case Intrinsic::powi:
Gabor Greif0635f352010-06-25 09:38:13 +00004542 setValue(&I, ExpandPowI(dl, getValue(I.getArgOperand(0)),
4543 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004544 return 0;
4545 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004546 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004547 getValue(I.getArgOperand(0)).getValueType(),
4548 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004549 return 0;
4550 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004551 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004552 getValue(I.getArgOperand(0)).getValueType(),
4553 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004554 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004555 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004556 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004557 return 0;
4558 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004559 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004560 return 0;
4561 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004562 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004563 return 0;
4564 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004565 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004566 return 0;
4567 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004568 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004569 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004570 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004571 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004572 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004573 case Intrinsic::convert_to_fp16:
4574 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004575 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004576 return 0;
4577 case Intrinsic::convert_from_fp16:
4578 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004579 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004580 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004581 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004582 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004583 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004584 return 0;
4585 }
4586 case Intrinsic::readcyclecounter: {
4587 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004588 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4589 DAG.getVTList(MVT::i64, MVT::Other),
4590 &Op, 1);
4591 setValue(&I, Res);
4592 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004593 return 0;
4594 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004595 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004596 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Gabor Greif0635f352010-06-25 09:38:13 +00004597 getValue(I.getArgOperand(0)).getValueType(),
4598 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004599 return 0;
4600 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004601 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004602 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004603 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004604 return 0;
4605 }
4606 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004607 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004608 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004609 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004610 return 0;
4611 }
4612 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00004613 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004614 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004615 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004616 return 0;
4617 }
4618 case Intrinsic::stacksave: {
4619 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004620 Res = DAG.getNode(ISD::STACKSAVE, dl,
4621 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4622 setValue(&I, Res);
4623 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624 return 0;
4625 }
4626 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00004627 Res = getValue(I.getArgOperand(0));
Bill Wendling4533cac2010-01-28 21:51:40 +00004628 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004629 return 0;
4630 }
Bill Wendling57344502008-11-18 11:01:33 +00004631 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004632 // Emit code into the DAG to store the stack guard onto the stack.
4633 MachineFunction &MF = DAG.getMachineFunction();
4634 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004635 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004636
Gabor Greif0635f352010-06-25 09:38:13 +00004637 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
4638 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004639
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004640 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004641 MFI->setStackProtectorIndex(FI);
4642
4643 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4644
4645 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004646 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00004647 MachinePointerInfo::getFixedStack(FI),
4648 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004649 setValue(&I, Res);
4650 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004651 return 0;
4652 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004653 case Intrinsic::objectsize: {
4654 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00004655 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004656
4657 assert(CI && "Non-constant type in __builtin_object_size?");
4658
Gabor Greif0635f352010-06-25 09:38:13 +00004659 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004660 EVT Ty = Arg.getValueType();
4661
Dan Gohmane368b462010-06-18 14:22:04 +00004662 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004663 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004664 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004665 Res = DAG.getConstant(0, Ty);
4666
4667 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004668 return 0;
4669 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004670 case Intrinsic::var_annotation:
4671 // Discard annotate attributes
4672 return 0;
4673
4674 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00004675 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676
4677 SDValue Ops[6];
4678 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004679 Ops[1] = getValue(I.getArgOperand(0));
4680 Ops[2] = getValue(I.getArgOperand(1));
4681 Ops[3] = getValue(I.getArgOperand(2));
4682 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004683 Ops[5] = DAG.getSrcValue(F);
4684
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004685 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4686 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4687 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004688
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004689 setValue(&I, Res);
4690 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004691 return 0;
4692 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004693 case Intrinsic::gcroot:
4694 if (GFI) {
Gabor Greif0635f352010-06-25 09:38:13 +00004695 const Value *Alloca = I.getArgOperand(0);
4696 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004697
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004698 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4699 GFI->addStackRoot(FI->getIndex(), TypeMap);
4700 }
4701 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004702 case Intrinsic::gcread:
4703 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004704 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004705 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004706 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004707 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004708 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00004709 case Intrinsic::trap: {
4710 StringRef TrapFuncName = getTrapFunctionName();
4711 if (TrapFuncName.empty()) {
4712 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
4713 return 0;
4714 }
4715 TargetLowering::ArgListTy Args;
4716 std::pair<SDValue, SDValue> Result =
4717 TLI.LowerCallTo(getRoot(), I.getType(),
4718 false, false, false, false, 0, CallingConv::C,
4719 /*isTailCall=*/false, /*isReturnValueUsed=*/true,
4720 DAG.getExternalSymbol(TrapFuncName.data(), TLI.getPointerTy()),
4721 Args, DAG, getCurDebugLoc());
4722 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004723 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00004724 }
Bill Wendlingef375462008-11-21 02:38:44 +00004725 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004726 return implVisitAluOverflow(I, ISD::UADDO);
4727 case Intrinsic::sadd_with_overflow:
4728 return implVisitAluOverflow(I, ISD::SADDO);
4729 case Intrinsic::usub_with_overflow:
4730 return implVisitAluOverflow(I, ISD::USUBO);
4731 case Intrinsic::ssub_with_overflow:
4732 return implVisitAluOverflow(I, ISD::SSUBO);
4733 case Intrinsic::umul_with_overflow:
4734 return implVisitAluOverflow(I, ISD::UMULO);
4735 case Intrinsic::smul_with_overflow:
4736 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004737
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004738 case Intrinsic::prefetch: {
4739 SDValue Ops[4];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004740 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004741 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00004742 Ops[1] = getValue(I.getArgOperand(0));
4743 Ops[2] = getValue(I.getArgOperand(1));
4744 Ops[3] = getValue(I.getArgOperand(2));
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004745 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, dl,
4746 DAG.getVTList(MVT::Other),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004747 &Ops[0], 4,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004748 EVT::getIntegerVT(*Context, 8),
4749 MachinePointerInfo(I.getArgOperand(0)),
4750 0, /* align */
4751 false, /* volatile */
4752 rw==0, /* read */
4753 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004754 return 0;
4755 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004756 case Intrinsic::memory_barrier: {
4757 SDValue Ops[6];
4758 Ops[0] = getRoot();
4759 for (int x = 1; x < 6; ++x)
Gabor Greif0635f352010-06-25 09:38:13 +00004760 Ops[x] = getValue(I.getArgOperand(x - 1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004761
Bill Wendling4533cac2010-01-28 21:51:40 +00004762 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004763 return 0;
4764 }
4765 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004766 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004767 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004768 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Gabor Greif0635f352010-06-25 09:38:13 +00004769 getValue(I.getArgOperand(1)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004770 Root,
Gabor Greif0635f352010-06-25 09:38:13 +00004771 getValue(I.getArgOperand(0)),
4772 getValue(I.getArgOperand(1)),
4773 getValue(I.getArgOperand(2)),
Chris Lattner60bddc82010-09-21 04:53:42 +00004774 MachinePointerInfo(I.getArgOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004775 setValue(&I, L);
4776 DAG.setRoot(L.getValue(1));
4777 return 0;
4778 }
4779 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004780 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004781 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004782 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004783 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004784 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004785 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004786 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004787 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004788 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004789 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004790 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004791 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004792 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004793 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004794 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004795 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004796 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004798 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004799 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004800 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004801
4802 case Intrinsic::invariant_start:
4803 case Intrinsic::lifetime_start:
4804 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004805 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004806 return 0;
4807 case Intrinsic::invariant_end:
4808 case Intrinsic::lifetime_end:
4809 // Discard region information.
4810 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004811 }
4812}
4813
Dan Gohman46510a72010-04-15 01:51:59 +00004814void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004815 bool isTailCall,
4816 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004817 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4818 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004819 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004820 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004821 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004822
4823 TargetLowering::ArgListTy Args;
4824 TargetLowering::ArgListEntry Entry;
4825 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004826
4827 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00004828 SmallVector<ISD::OutputArg, 4> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004829 SmallVector<uint64_t, 4> Offsets;
Dan Gohman84023e02010-07-10 09:00:22 +00004830 GetReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4831 Outs, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004832
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004833 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Dan Gohman84023e02010-07-10 09:00:22 +00004834 FTy->isVarArg(), Outs, FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004835
4836 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00004837 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004838
4839 if (!CanLowerReturn) {
4840 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4841 FTy->getReturnType());
4842 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4843 FTy->getReturnType());
4844 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00004845 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004846 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4847
Chris Lattnerecf42c42010-09-21 16:36:31 +00004848 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI.getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004849 Entry.Node = DemoteStackSlot;
4850 Entry.Ty = StackSlotPtrType;
4851 Entry.isSExt = false;
4852 Entry.isZExt = false;
4853 Entry.isInReg = false;
4854 Entry.isSRet = true;
4855 Entry.isNest = false;
4856 Entry.isByVal = false;
4857 Entry.Alignment = Align;
4858 Args.push_back(Entry);
4859 RetTy = Type::getVoidTy(FTy->getContext());
4860 }
4861
Dan Gohman46510a72010-04-15 01:51:59 +00004862 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004863 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00004864 const Value *V = *i;
4865
4866 // Skip empty types
4867 if (V->getType()->isEmptyTy())
4868 continue;
4869
4870 SDValue ArgNode = getValue(V);
4871 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004872
4873 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004874 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4875 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4876 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4877 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4878 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4879 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004880 Entry.Alignment = CS.getParamAlignment(attrInd);
4881 Args.push_back(Entry);
4882 }
4883
Chris Lattner512063d2010-04-05 06:19:28 +00004884 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004885 // Insert a label before the invoke call to mark the try range. This can be
4886 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004887 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004888
Jim Grosbachca752c92010-01-28 01:45:32 +00004889 // For SjLj, keep track of which landing pads go with which invokes
4890 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004891 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004892 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004893 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004894 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004895 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004896 }
4897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004898 // Both PendingLoads and PendingExports must be flushed here;
4899 // this call might not return.
4900 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004901 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004902 }
4903
Dan Gohman98ca4f22009-08-05 01:29:28 +00004904 // Check if target-independent constraints permit a tail call here.
4905 // Target-dependent constraints are checked within TLI.LowerCallTo.
4906 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004907 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004908 isTailCall = false;
4909
Dan Gohmanbadcda42010-08-28 00:51:03 +00004910 // If there's a possibility that fast-isel has already selected some amount
4911 // of the current basic block, don't emit a tail call.
4912 if (isTailCall && EnableFastISel)
4913 isTailCall = false;
4914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004915 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004916 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004917 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004918 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004919 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004920 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004921 isTailCall,
4922 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004923 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004924 assert((isTailCall || Result.second.getNode()) &&
4925 "Non-null chain expected with non-tail call!");
4926 assert((Result.second.getNode() || !Result.first.getNode()) &&
4927 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004928 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004929 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004930 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004931 // The instruction result is the result of loading from the
4932 // hidden sret parameter.
4933 SmallVector<EVT, 1> PVTs;
4934 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4935
4936 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4937 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4938 EVT PtrVT = PVTs[0];
Dan Gohman84023e02010-07-10 09:00:22 +00004939 unsigned NumValues = Outs.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004940 SmallVector<SDValue, 4> Values(NumValues);
4941 SmallVector<SDValue, 4> Chains(NumValues);
4942
4943 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004944 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4945 DemoteStackSlot,
4946 DAG.getConstant(Offsets[i], PtrVT));
Dan Gohman84023e02010-07-10 09:00:22 +00004947 SDValue L = DAG.getLoad(Outs[i].VT, getCurDebugLoc(), Result.second,
Chris Lattnerecf42c42010-09-21 16:36:31 +00004948 Add,
4949 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
4950 false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004951 Values[i] = L;
4952 Chains[i] = L.getValue(1);
4953 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004954
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004955 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4956 MVT::Other, &Chains[0], NumValues);
4957 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00004958
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004959 // Collect the legal value parts into potentially illegal values
4960 // that correspond to the original function's return values.
4961 SmallVector<EVT, 4> RetTys;
4962 RetTy = FTy->getReturnType();
4963 ComputeValueVTs(TLI, RetTy, RetTys);
4964 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4965 SmallVector<SDValue, 4> ReturnValues;
4966 unsigned CurReg = 0;
4967 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4968 EVT VT = RetTys[I];
4969 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4970 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
Michael J. Spencere70c5262010-10-16 08:25:21 +00004971
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004972 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004973 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004974 RegisterVT, VT, AssertOp);
4975 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004976 CurReg += NumRegs;
4977 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004978
Bill Wendling4533cac2010-01-28 21:51:40 +00004979 setValue(CS.getInstruction(),
4980 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4981 DAG.getVTList(&RetTys[0], RetTys.size()),
4982 &ReturnValues[0], ReturnValues.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004983 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004984
Evan Chengc249e482011-04-01 19:57:01 +00004985 // Assign order to nodes here. If the call does not produce a result, it won't
4986 // be mapped to a SDNode and visit() will not assign it an order number.
Evan Cheng8380c032011-04-01 19:42:22 +00004987 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00004988 // As a special case, a null chain means that a tail call has been emitted and
4989 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00004990 HasTailCall = true;
Evan Cheng8380c032011-04-01 19:42:22 +00004991 ++SDNodeOrder;
4992 AssignOrderingToNode(DAG.getRoot().getNode());
4993 } else {
4994 DAG.setRoot(Result.second);
4995 ++SDNodeOrder;
4996 AssignOrderingToNode(Result.second.getNode());
4997 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004998
Chris Lattner512063d2010-04-05 06:19:28 +00004999 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005000 // Insert a label at the end of the invoke call to mark the try range. This
5001 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005002 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00005003 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005004
5005 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005006 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005007 }
5008}
5009
Chris Lattner8047d9a2009-12-24 00:37:38 +00005010/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5011/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005012static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5013 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005014 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005015 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005016 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005017 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005018 if (C->isNullValue())
5019 continue;
5020 // Unknown instruction.
5021 return false;
5022 }
5023 return true;
5024}
5025
Dan Gohman46510a72010-04-15 01:51:59 +00005026static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
5027 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005028 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005029
Chris Lattner8047d9a2009-12-24 00:37:38 +00005030 // Check to see if this load can be trivially constant folded, e.g. if the
5031 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005032 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005033 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005034 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005035 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005036
Dan Gohman46510a72010-04-15 01:51:59 +00005037 if (const Constant *LoadCst =
5038 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5039 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005040 return Builder.getValue(LoadCst);
5041 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005042
Chris Lattner8047d9a2009-12-24 00:37:38 +00005043 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5044 // still constant memory, the input chain can be the entry node.
5045 SDValue Root;
5046 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005047
Chris Lattner8047d9a2009-12-24 00:37:38 +00005048 // Do not serialize (non-volatile) loads of constant memory with anything.
5049 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5050 Root = Builder.DAG.getEntryNode();
5051 ConstantMemory = true;
5052 } else {
5053 // Do not serialize non-volatile loads against each other.
5054 Root = Builder.DAG.getRoot();
5055 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005056
Chris Lattner8047d9a2009-12-24 00:37:38 +00005057 SDValue Ptr = Builder.getValue(PtrVal);
5058 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005059 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005060 false /*volatile*/,
5061 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005062
Chris Lattner8047d9a2009-12-24 00:37:38 +00005063 if (!ConstantMemory)
5064 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5065 return LoadVal;
5066}
5067
5068
5069/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5070/// If so, return true and lower it, otherwise return false and it will be
5071/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005072bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005073 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005074 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005075 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005076
Gabor Greif0635f352010-06-25 09:38:13 +00005077 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005078 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005079 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005080 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005081 return false;
5082
Gabor Greif0635f352010-06-25 09:38:13 +00005083 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005084
Chris Lattner8047d9a2009-12-24 00:37:38 +00005085 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5086 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005087 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5088 bool ActuallyDoIt = true;
5089 MVT LoadVT;
5090 const Type *LoadTy;
5091 switch (Size->getZExtValue()) {
5092 default:
5093 LoadVT = MVT::Other;
5094 LoadTy = 0;
5095 ActuallyDoIt = false;
5096 break;
5097 case 2:
5098 LoadVT = MVT::i16;
5099 LoadTy = Type::getInt16Ty(Size->getContext());
5100 break;
5101 case 4:
5102 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005103 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005104 break;
5105 case 8:
5106 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005107 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005108 break;
5109 /*
5110 case 16:
5111 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005112 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005113 LoadTy = VectorType::get(LoadTy, 4);
5114 break;
5115 */
5116 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005117
Chris Lattner04b091a2009-12-24 01:07:17 +00005118 // This turns into unaligned loads. We only do this if the target natively
5119 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5120 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005121
Chris Lattner04b091a2009-12-24 01:07:17 +00005122 // Require that we can find a legal MVT, and only do this if the target
5123 // supports unaligned loads of that type. Expanding into byte loads would
5124 // bloat the code.
5125 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5126 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5127 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5128 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5129 ActuallyDoIt = false;
5130 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005131
Chris Lattner04b091a2009-12-24 01:07:17 +00005132 if (ActuallyDoIt) {
5133 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5134 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005135
Chris Lattner04b091a2009-12-24 01:07:17 +00005136 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
5137 ISD::SETNE);
5138 EVT CallVT = TLI.getValueType(I.getType(), true);
5139 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
5140 return true;
5141 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005142 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005143
5144
Chris Lattner8047d9a2009-12-24 00:37:38 +00005145 return false;
5146}
5147
5148
Dan Gohman46510a72010-04-15 01:51:59 +00005149void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005150 // Handle inline assembly differently.
5151 if (isa<InlineAsm>(I.getCalledValue())) {
5152 visitInlineAsm(&I);
5153 return;
5154 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005155
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005156 // See if any floating point values are being passed to this function. This is
5157 // used to emit an undefined reference to fltused on Windows.
5158 const FunctionType *FT =
5159 cast<FunctionType>(I.getCalledValue()->getType()->getContainedType(0));
5160 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
5161 if (FT->isVarArg() &&
5162 !MMI.callsExternalVAFunctionWithFloatingPointArguments()) {
5163 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
5164 const Type* T = I.getArgOperand(i)->getType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005165 for (po_iterator<const Type*> i = po_begin(T), e = po_end(T);
Chris Lattnera29aae72010-11-12 17:24:29 +00005166 i != e; ++i) {
5167 if (!i->isFloatingPointTy()) continue;
5168 MMI.setCallsExternalVAFunctionWithFloatingPointArguments(true);
5169 break;
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005170 }
5171 }
5172 }
5173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005174 const char *RenameFn = 0;
5175 if (Function *F = I.getCalledFunction()) {
5176 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005177 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005178 if (unsigned IID = II->getIntrinsicID(F)) {
5179 RenameFn = visitIntrinsicCall(I, IID);
5180 if (!RenameFn)
5181 return;
5182 }
5183 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005184 if (unsigned IID = F->getIntrinsicID()) {
5185 RenameFn = visitIntrinsicCall(I, IID);
5186 if (!RenameFn)
5187 return;
5188 }
5189 }
5190
5191 // Check for well-known libc/libm calls. If the function is internal, it
5192 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005193 if (!F->hasLocalLinkage() && F->hasName()) {
5194 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00005195 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005196 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005197 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5198 I.getType() == I.getArgOperand(0)->getType() &&
5199 I.getType() == I.getArgOperand(1)->getType()) {
5200 SDValue LHS = getValue(I.getArgOperand(0));
5201 SDValue RHS = getValue(I.getArgOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005202 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5203 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005204 return;
5205 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005206 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005207 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005208 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5209 I.getType() == I.getArgOperand(0)->getType()) {
5210 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00005211 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
5212 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005213 return;
5214 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005215 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005216 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005217 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5218 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005219 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005220 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00005221 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5222 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005223 return;
5224 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005225 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005226 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005227 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5228 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005229 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005230 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00005231 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5232 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005233 return;
5234 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005235 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
Gabor Greif37387d52010-06-30 12:55:46 +00005236 if (I.getNumArgOperands() == 1 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005237 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5238 I.getType() == I.getArgOperand(0)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005239 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005240 SDValue Tmp = getValue(I.getArgOperand(0));
Bill Wendling0d580132009-12-23 01:28:19 +00005241 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5242 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005243 return;
5244 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005245 } else if (Name == "memcmp") {
5246 if (visitMemCmpCall(I))
5247 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005248 }
5249 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005250 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005251
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005252 SDValue Callee;
5253 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005254 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005255 else
Bill Wendling056292f2008-09-16 21:48:12 +00005256 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005257
Bill Wendling0d580132009-12-23 01:28:19 +00005258 // Check if we can potentially perform a tail call. More detailed checking is
5259 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005260 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261}
5262
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005263namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005264
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005265/// AsmOperandInfo - This contains information for each constraint that we are
5266/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005267class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005268public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005269 /// CallOperand - If this is the result output operand or a clobber
5270 /// this is null, otherwise it is the incoming operand to the CallInst.
5271 /// This gets modified as the asm is processed.
5272 SDValue CallOperand;
5273
5274 /// AssignedRegs - If this is a register or register class operand, this
5275 /// contains the set of register corresponding to the operand.
5276 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005277
John Thompsoneac6e1d2010-09-13 18:15:37 +00005278 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005279 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5280 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005282 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5283 /// busy in OutputRegs/InputRegs.
5284 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005285 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005286 std::set<unsigned> &InputRegs,
5287 const TargetRegisterInfo &TRI) const {
5288 if (isOutReg) {
5289 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5290 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5291 }
5292 if (isInReg) {
5293 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5294 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5295 }
5296 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005297
Owen Andersone50ed302009-08-10 22:56:29 +00005298 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005299 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005300 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005301 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005302 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005303 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005304 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005305
Chris Lattner81249c92008-10-17 17:05:25 +00005306 if (isa<BasicBlock>(CallOperandVal))
5307 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005308
Chris Lattner81249c92008-10-17 17:05:25 +00005309 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005310
Eric Christophercef81b72011-05-09 20:04:43 +00005311 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00005312 // If this is an indirect operand, the operand is a pointer to the
5313 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005314 if (isIndirect) {
5315 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5316 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005317 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005318 OpTy = PtrTy->getElementType();
5319 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005320
Eric Christophercef81b72011-05-09 20:04:43 +00005321 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
5322 if (const StructType *STy = dyn_cast<StructType>(OpTy))
5323 if (STy->getNumElements() == 1)
5324 OpTy = STy->getElementType(0);
5325
Chris Lattner81249c92008-10-17 17:05:25 +00005326 // If OpTy is not a single value, it may be a struct/union that we
5327 // can tile with integers.
5328 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5329 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5330 switch (BitSize) {
5331 default: break;
5332 case 1:
5333 case 8:
5334 case 16:
5335 case 32:
5336 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005337 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005338 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005339 break;
5340 }
5341 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005342
Chris Lattner81249c92008-10-17 17:05:25 +00005343 return TLI.getValueType(OpTy, true);
5344 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005345
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005346private:
5347 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5348 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005349 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005350 const TargetRegisterInfo &TRI) {
5351 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5352 Regs.insert(Reg);
5353 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5354 for (; *Aliases; ++Aliases)
5355 Regs.insert(*Aliases);
5356 }
5357};
Dan Gohman462f6b52010-05-29 17:53:24 +00005358
John Thompson44ab89e2010-10-29 17:29:13 +00005359typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
5360
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005361} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005362
Dan Gohman462f6b52010-05-29 17:53:24 +00005363/// isAllocatableRegister - If the specified register is safe to allocate,
5364/// i.e. it isn't a stack pointer or some other special register, return the
5365/// register class for the register. Otherwise, return null.
5366static const TargetRegisterClass *
5367isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5368 const TargetLowering &TLI,
5369 const TargetRegisterInfo *TRI) {
5370 EVT FoundVT = MVT::Other;
5371 const TargetRegisterClass *FoundRC = 0;
5372 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5373 E = TRI->regclass_end(); RCI != E; ++RCI) {
5374 EVT ThisVT = MVT::Other;
5375
5376 const TargetRegisterClass *RC = *RCI;
5377 // If none of the value types for this register class are valid, we
5378 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5379 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5380 I != E; ++I) {
5381 if (TLI.isTypeLegal(*I)) {
5382 // If we have already found this register in a different register class,
5383 // choose the one with the largest VT specified. For example, on
5384 // PowerPC, we favor f64 register classes over f32.
5385 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
5386 ThisVT = *I;
5387 break;
5388 }
5389 }
5390 }
5391
5392 if (ThisVT == MVT::Other) continue;
5393
5394 // NOTE: This isn't ideal. In particular, this might allocate the
5395 // frame pointer in functions that need it (due to them not being taken
5396 // out of allocation, because a variable sized allocation hasn't been seen
5397 // yet). This is a slight code pessimization, but should still work.
5398 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5399 E = RC->allocation_order_end(MF); I != E; ++I)
5400 if (*I == Reg) {
5401 // We found a matching register class. Keep looking at others in case
5402 // we find one with larger registers that this physreg is also in.
5403 FoundRC = RC;
5404 FoundVT = ThisVT;
5405 break;
5406 }
5407 }
5408 return FoundRC;
5409}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005410
5411/// GetRegistersForValue - Assign registers (virtual or physical) for the
5412/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005413/// register allocator to handle the assignment process. However, if the asm
5414/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005415/// allocation. This produces generally horrible, but correct, code.
5416///
5417/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005418/// Input and OutputRegs are the set of already allocated physical registers.
5419///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005420static void GetRegistersForValue(SelectionDAG &DAG,
5421 const TargetLowering &TLI,
5422 DebugLoc DL,
5423 SDISelAsmOperandInfo &OpInfo,
5424 std::set<unsigned> &OutputRegs,
5425 std::set<unsigned> &InputRegs) {
5426 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005427
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005428 // Compute whether this value requires an input register, an output register,
5429 // or both.
5430 bool isOutReg = false;
5431 bool isInReg = false;
5432 switch (OpInfo.Type) {
5433 case InlineAsm::isOutput:
5434 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005435
5436 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005437 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005438 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005439 break;
5440 case InlineAsm::isInput:
5441 isInReg = true;
5442 isOutReg = false;
5443 break;
5444 case InlineAsm::isClobber:
5445 isOutReg = true;
5446 isInReg = true;
5447 break;
5448 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005449
5450
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005451 MachineFunction &MF = DAG.getMachineFunction();
5452 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005453
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005454 // If this is a constraint for a single physreg, or a constraint for a
5455 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005456 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005457 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5458 OpInfo.ConstraintVT);
5459
5460 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005461 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005462 // If this is a FP input in an integer register (or visa versa) insert a bit
5463 // cast of the input value. More generally, handle any case where the input
5464 // value disagrees with the register class we plan to stick this in.
5465 if (OpInfo.Type == InlineAsm::isInput &&
5466 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005467 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005468 // types are identical size, use a bitcast to convert (e.g. two differing
5469 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005470 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005471 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005472 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005473 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005474 OpInfo.ConstraintVT = RegVT;
5475 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5476 // If the input is a FP value and we want it in FP registers, do a
5477 // bitcast to the corresponding integer type. This turns an f64 value
5478 // into i64, which can be passed with two i32 values on a 32-bit
5479 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005480 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005481 OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005482 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005483 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005484 OpInfo.ConstraintVT = RegVT;
5485 }
5486 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005487
Owen Anderson23b9b192009-08-12 00:36:31 +00005488 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005489 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005490
Owen Andersone50ed302009-08-10 22:56:29 +00005491 EVT RegVT;
5492 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005493
5494 // If this is a constraint for a specific physical register, like {r17},
5495 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005496 if (unsigned AssignedReg = PhysReg.first) {
5497 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005498 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005499 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005500
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005501 // Get the actual register value type. This is important, because the user
5502 // may have asked for (e.g.) the AX register in i32 type. We need to
5503 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005504 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005506 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005507 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005508
5509 // If this is an expanded reference, add the rest of the regs to Regs.
5510 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005511 TargetRegisterClass::iterator I = RC->begin();
5512 for (; *I != AssignedReg; ++I)
5513 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005514
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005515 // Already added the first reg.
5516 --NumRegs; ++I;
5517 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005518 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005519 Regs.push_back(*I);
5520 }
5521 }
Bill Wendling651ad132009-12-22 01:25:10 +00005522
Dan Gohman7451d3e2010-05-29 17:03:36 +00005523 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005524 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5525 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5526 return;
5527 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005528
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005529 // Otherwise, if this was a reference to an LLVM register class, create vregs
5530 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005531 if (const TargetRegisterClass *RC = PhysReg.second) {
5532 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005533 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005534 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005535
Evan Chengfb112882009-03-23 08:01:15 +00005536 // Create the appropriate number of virtual registers.
5537 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5538 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005539 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005540
Dan Gohman7451d3e2010-05-29 17:03:36 +00005541 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005542 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005543 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005544
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005545 // This is a reference to a register class that doesn't directly correspond
5546 // to an LLVM register class. Allocate NumRegs consecutive, available,
5547 // registers from the class.
5548 std::vector<unsigned> RegClassRegs
5549 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5550 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005551
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005552 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5553 unsigned NumAllocated = 0;
5554 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5555 unsigned Reg = RegClassRegs[i];
5556 // See if this register is available.
5557 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5558 (isInReg && InputRegs.count(Reg))) { // Already used.
5559 // Make sure we find consecutive registers.
5560 NumAllocated = 0;
5561 continue;
5562 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005563
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005564 // Check to see if this register is allocatable (i.e. don't give out the
5565 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005566 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5567 if (!RC) { // Couldn't allocate this register.
5568 // Reset NumAllocated to make sure we return consecutive registers.
5569 NumAllocated = 0;
5570 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005571 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005572
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005573 // Okay, this register is good, we can use it.
5574 ++NumAllocated;
5575
5576 // If we allocated enough consecutive registers, succeed.
5577 if (NumAllocated == NumRegs) {
5578 unsigned RegStart = (i-NumAllocated)+1;
5579 unsigned RegEnd = i+1;
5580 // Mark all of the allocated registers used.
5581 for (unsigned i = RegStart; i != RegEnd; ++i)
5582 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005583
Dan Gohman7451d3e2010-05-29 17:03:36 +00005584 OpInfo.AssignedRegs = RegsForValue(Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005585 OpInfo.ConstraintVT);
5586 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5587 return;
5588 }
5589 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005590
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005591 // Otherwise, we couldn't allocate enough registers for this.
5592}
5593
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005594/// visitInlineAsm - Handle a call to an InlineAsm object.
5595///
Dan Gohman46510a72010-04-15 01:51:59 +00005596void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5597 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005598
5599 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00005600 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005601
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005602 std::set<unsigned> OutputRegs, InputRegs;
5603
Evan Chengce1cdac2011-05-06 20:52:23 +00005604 TargetLowering::AsmOperandInfoVector
5605 TargetConstraints = TLI.ParseConstraints(CS);
5606
John Thompsoneac6e1d2010-09-13 18:15:37 +00005607 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005608
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005609 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5610 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005611 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
5612 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005613 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00005614
Owen Anderson825b72b2009-08-11 20:47:22 +00005615 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005616
5617 // Compute the value type for each operand.
5618 switch (OpInfo.Type) {
5619 case InlineAsm::isOutput:
5620 // Indirect outputs just consume an argument.
5621 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005622 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005623 break;
5624 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005625
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626 // The return value of the call is this value. As such, there is no
5627 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005628 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005629 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005630 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5631 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5632 } else {
5633 assert(ResNo == 0 && "Asm only has one result!");
5634 OpVT = TLI.getValueType(CS.getType());
5635 }
5636 ++ResNo;
5637 break;
5638 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005639 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005640 break;
5641 case InlineAsm::isClobber:
5642 // Nothing to do.
5643 break;
5644 }
5645
5646 // If this is an input or an indirect output, process the call argument.
5647 // BasicBlocks are labels, currently appearing only in asm's.
5648 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00005649 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005650 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005651 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005653 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005654
Owen Anderson1d0be152009-08-13 21:58:54 +00005655 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005656 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005657
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005658 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005659
John Thompsoneac6e1d2010-09-13 18:15:37 +00005660 // Indirect operand accesses access memory.
5661 if (OpInfo.isIndirect)
5662 hasMemory = true;
5663 else {
5664 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005665 TargetLowering::ConstraintType
5666 CType = TLI.getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00005667 if (CType == TargetLowering::C_Memory) {
5668 hasMemory = true;
5669 break;
5670 }
5671 }
5672 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005673 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005674
John Thompsoneac6e1d2010-09-13 18:15:37 +00005675 SDValue Chain, Flag;
5676
5677 // We won't need to flush pending loads if this asm doesn't touch
5678 // memory and is nonvolatile.
5679 if (hasMemory || IA->hasSideEffects())
5680 Chain = getRoot();
5681 else
5682 Chain = DAG.getRoot();
5683
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005684 // Second pass over the constraints: compute which constraint option to use
5685 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005686 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005687 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005688
John Thompson54584742010-09-24 22:24:05 +00005689 // If this is an output operand with a matching input operand, look up the
5690 // matching input. If their types mismatch, e.g. one is an integer, the
5691 // other is floating point, or their sizes are different, flag it as an
5692 // error.
5693 if (OpInfo.hasMatchingInput()) {
5694 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00005695
John Thompson54584742010-09-24 22:24:05 +00005696 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
5697 if ((OpInfo.ConstraintVT.isInteger() !=
5698 Input.ConstraintVT.isInteger()) ||
5699 (OpInfo.ConstraintVT.getSizeInBits() !=
5700 Input.ConstraintVT.getSizeInBits())) {
5701 report_fatal_error("Unsupported asm: input constraint"
5702 " with a matching output constraint of"
5703 " incompatible type!");
5704 }
5705 Input.ConstraintVT = OpInfo.ConstraintVT;
5706 }
5707 }
5708
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709 // Compute the constraint code and ConstraintType to use.
Dale Johannesen1784d162010-06-25 21:55:36 +00005710 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005711
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005712 // If this is a memory input, and if the operand is not indirect, do what we
5713 // need to to provide an address for the memory input.
5714 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5715 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005716 assert((OpInfo.isMultipleAlternative ||
5717 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005718 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005719
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005720 // Memory operands really want the address of the value. If we don't have
5721 // an indirect input, put it in the constpool if we can, otherwise spill
5722 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005723
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005724 // If the operand is a float, integer, or vector constant, spill to a
5725 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005726 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005727 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5728 isa<ConstantVector>(OpVal)) {
5729 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5730 TLI.getPointerTy());
5731 } else {
5732 // Otherwise, create a stack slot and emit a store to it before the
5733 // asm.
5734 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005735 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005736 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5737 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005738 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005739 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005740 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00005741 OpInfo.CallOperand, StackSlot,
5742 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00005743 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005744 OpInfo.CallOperand = StackSlot;
5745 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005746
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005747 // There is no longer a Value* corresponding to this operand.
5748 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005749
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005750 // It is now an indirect operand.
5751 OpInfo.isIndirect = true;
5752 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005753
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005754 // If this constraint is for a specific register, allocate it before
5755 // anything else.
5756 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005757 GetRegistersForValue(DAG, TLI, getCurDebugLoc(), OpInfo, OutputRegs,
5758 InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005759 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005760
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005761 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005762 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005763 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5764 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005766 // C_Register operands have already been allocated, Other/Memory don't need
5767 // to be.
5768 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005769 GetRegistersForValue(DAG, TLI, getCurDebugLoc(), OpInfo, OutputRegs,
5770 InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005771 }
5772
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005773 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5774 std::vector<SDValue> AsmNodeOperands;
5775 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5776 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005777 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5778 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005779
Chris Lattnerdecc2672010-04-07 05:20:54 +00005780 // If we have a !srcloc metadata node associated with it, we want to attach
5781 // this to the ultimately generated inline asm machineinstr. To do this, we
5782 // pass in the third operand as this (potentially null) inline asm MDNode.
5783 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5784 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005785
Evan Chengc36b7062011-01-07 23:50:32 +00005786 // Remember the HasSideEffect and AlignStack bits as operand 3.
5787 unsigned ExtraInfo = 0;
5788 if (IA->hasSideEffects())
5789 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
5790 if (IA->isAlignStack())
5791 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
5792 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
5793 TLI.getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005794
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005795 // Loop over all of the inputs, copying the operand values into the
5796 // appropriate registers and processing the output regs.
5797 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005798
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005799 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5800 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5803 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5804
5805 switch (OpInfo.Type) {
5806 case InlineAsm::isOutput: {
5807 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5808 OpInfo.ConstraintType != TargetLowering::C_Register) {
5809 // Memory output, or 'other' output (e.g. 'X' constraint).
5810 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5811
5812 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005813 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5814 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005815 TLI.getPointerTy()));
5816 AsmNodeOperands.push_back(OpInfo.CallOperand);
5817 break;
5818 }
5819
5820 // Otherwise, this is a register or register class output.
5821
5822 // Copy the output from the appropriate register. Find a register that
5823 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005824 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005825 report_fatal_error("Couldn't allocate output reg for constraint '" +
5826 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005827
5828 // If this is an indirect operand, store through the pointer after the
5829 // asm.
5830 if (OpInfo.isIndirect) {
5831 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5832 OpInfo.CallOperandVal));
5833 } else {
5834 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005835 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005836 // Concatenate this output onto the outputs list.
5837 RetValRegs.append(OpInfo.AssignedRegs);
5838 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005839
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005840 // Add information to the INLINEASM node to know that this register is
5841 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005842 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005843 InlineAsm::Kind_RegDefEarlyClobber :
5844 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005845 false,
5846 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005847 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005848 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005849 break;
5850 }
5851 case InlineAsm::isInput: {
5852 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005853
Chris Lattner6bdcda32008-10-17 16:47:46 +00005854 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005855 // If this is required to match an output register we have already set,
5856 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005857 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005858
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005859 // Scan until we find the definition we already emitted of this operand.
5860 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005861 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005862 for (; OperandNo; --OperandNo) {
5863 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005864 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005865 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005866 assert((InlineAsm::isRegDefKind(OpFlag) ||
5867 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5868 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005869 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005870 }
5871
Evan Cheng697cbbf2009-03-20 18:03:34 +00005872 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005873 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005874 if (InlineAsm::isRegDefKind(OpFlag) ||
5875 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005876 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005877 if (OpInfo.isIndirect) {
5878 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005879 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005880 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5881 " don't know how to handle tied "
5882 "indirect register inputs");
5883 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005885 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005886 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005887 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005888 MatchedRegs.RegVTs.push_back(RegVT);
5889 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005890 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005891 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005892 MatchedRegs.Regs.push_back
5893 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005894
5895 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005896 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005897 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005898 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005899 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005900 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005901 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005902 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005903
Chris Lattnerdecc2672010-04-07 05:20:54 +00005904 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5905 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5906 "Unexpected number of operands");
5907 // Add information to the INLINEASM node to know about this input.
5908 // See InlineAsm.h isUseOperandTiedToDef.
5909 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5910 OpInfo.getMatchedOperand());
5911 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5912 TLI.getPointerTy()));
5913 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5914 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005915 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005916
Dale Johannesenb5611a62010-07-13 20:17:05 +00005917 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00005918 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
5919 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00005920 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005921
Dale Johannesenb5611a62010-07-13 20:17:05 +00005922 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005923 std::vector<SDValue> Ops;
5924 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Dale Johannesen1784d162010-06-25 21:55:36 +00005925 Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005926 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005927 report_fatal_error("Invalid operand for inline asm constraint '" +
5928 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005929
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005930 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005931 unsigned ResOpType =
5932 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005933 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934 TLI.getPointerTy()));
5935 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5936 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005937 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005938
Chris Lattnerdecc2672010-04-07 05:20:54 +00005939 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005940 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5941 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5942 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005943
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005944 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005945 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005946 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005947 TLI.getPointerTy()));
5948 AsmNodeOperands.push_back(InOperandVal);
5949 break;
5950 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005951
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005952 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5953 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5954 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005955 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005956 "Don't know how to handle indirect register inputs yet!");
5957
5958 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005959 if (OpInfo.AssignedRegs.Regs.empty() ||
Dan Gohman7451d3e2010-05-29 17:03:36 +00005960 !OpInfo.AssignedRegs.areValueTypesLegal(TLI))
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005961 report_fatal_error("Couldn't allocate input reg for constraint '" +
5962 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005963
Dale Johannesen66978ee2009-01-31 02:22:37 +00005964 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005965 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005966
Chris Lattnerdecc2672010-04-07 05:20:54 +00005967 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005968 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969 break;
5970 }
5971 case InlineAsm::isClobber: {
5972 // Add the clobbered value to the operand list, so that the register
5973 // allocator is aware that the physreg got clobbered.
5974 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005975 OpInfo.AssignedRegs.AddInlineAsmOperands(
5976 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005977 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005978 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005979 break;
5980 }
5981 }
5982 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005983
Chris Lattnerdecc2672010-04-07 05:20:54 +00005984 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00005985 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005986 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005987
Dale Johannesen66978ee2009-01-31 02:22:37 +00005988 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005989 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005990 &AsmNodeOperands[0], AsmNodeOperands.size());
5991 Flag = Chain.getValue(1);
5992
5993 // If this asm returns a register value, copy the result from that register
5994 // and set it as the value of the call.
5995 if (!RetValRegs.Regs.empty()) {
Dan Gohman7451d3e2010-05-29 17:03:36 +00005996 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005997 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005998
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005999 // FIXME: Why don't we do this for inline asms with MRVs?
6000 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006001 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006002
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006003 // If any of the results of the inline asm is a vector, it may have the
6004 // wrong width/num elts. This can happen for register classes that can
6005 // contain multiple different value types. The preg or vreg allocated may
6006 // not have the same VT as was expected. Convert it to the right type
6007 // with bit_convert.
6008 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006009 Val = DAG.getNode(ISD::BITCAST, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006010 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006011
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006012 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006013 ResultType.isInteger() && Val.getValueType().isInteger()) {
6014 // If a result value was tied to an input value, the computed result may
6015 // have a wider width than the expected result. Extract the relevant
6016 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006017 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006018 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006019
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006020 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006021 }
Dan Gohman95915732008-10-18 01:03:45 +00006022
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006023 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006024 // Don't need to use this as a chain in this case.
6025 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6026 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006027 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006028
Dan Gohman46510a72010-04-15 01:51:59 +00006029 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006031 // Process indirect outputs, first output all of the flagged copies out of
6032 // physregs.
6033 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6034 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006035 const Value *Ptr = IndirectStoresToEmit[i].second;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006036 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00006037 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006038 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6039 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006041 // Emit the non-flagged stores from the physregs.
6042 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006043 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6044 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6045 StoresToEmit[i].first,
6046 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006047 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006048 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006049 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006050 }
6051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006052 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006053 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006054 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006056 DAG.setRoot(Chain);
6057}
6058
Dan Gohman46510a72010-04-15 01:51:59 +00006059void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006060 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6061 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006062 getValue(I.getArgOperand(0)),
6063 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006064}
6065
Dan Gohman46510a72010-04-15 01:51:59 +00006066void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Rafael Espindola9d544d02010-07-12 18:11:17 +00006067 const TargetData &TD = *TLI.getTargetData();
Dale Johannesena04b7572009-02-03 23:04:43 +00006068 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6069 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006070 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006071 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006072 setValue(&I, V);
6073 DAG.setRoot(V.getValue(1));
6074}
6075
Dan Gohman46510a72010-04-15 01:51:59 +00006076void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006077 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6078 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006079 getValue(I.getArgOperand(0)),
6080 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081}
6082
Dan Gohman46510a72010-04-15 01:51:59 +00006083void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006084 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6085 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006086 getValue(I.getArgOperand(0)),
6087 getValue(I.getArgOperand(1)),
6088 DAG.getSrcValue(I.getArgOperand(0)),
6089 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006090}
6091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006092/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006093/// implementation, which just calls LowerCall.
6094/// FIXME: When all targets are
6095/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006096std::pair<SDValue, SDValue>
6097TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6098 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006099 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006100 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006101 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006102 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00006103 ArgListTy &Args, SelectionDAG &DAG,
6104 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006105 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006106 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00006107 SmallVector<SDValue, 32> OutVals;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006108 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006109 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006110 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6111 for (unsigned Value = 0, NumValues = ValueVTs.size();
6112 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006113 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006114 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006115 SDValue Op = SDValue(Args[i].Node.getNode(),
6116 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006117 ISD::ArgFlagsTy Flags;
6118 unsigned OriginalAlignment =
6119 getTargetData()->getABITypeAlignment(ArgTy);
6120
6121 if (Args[i].isZExt)
6122 Flags.setZExt();
6123 if (Args[i].isSExt)
6124 Flags.setSExt();
6125 if (Args[i].isInReg)
6126 Flags.setInReg();
6127 if (Args[i].isSRet)
6128 Flags.setSRet();
6129 if (Args[i].isByVal) {
6130 Flags.setByVal();
6131 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6132 const Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006133 Flags.setByValSize(getTargetData()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006134 // For ByVal, alignment should come from FE. BE will guess if this
6135 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006136 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006137 if (Args[i].Alignment)
6138 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006139 else
6140 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006141 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006142 }
6143 if (Args[i].isNest)
6144 Flags.setNest();
6145 Flags.setOrigAlign(OriginalAlignment);
6146
Owen Anderson23b9b192009-08-12 00:36:31 +00006147 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6148 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006149 SmallVector<SDValue, 4> Parts(NumParts);
6150 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6151
6152 if (Args[i].isSExt)
6153 ExtendKind = ISD::SIGN_EXTEND;
6154 else if (Args[i].isZExt)
6155 ExtendKind = ISD::ZERO_EXTEND;
6156
Bill Wendling46ada192010-03-02 01:55:18 +00006157 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006158 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006159
Dan Gohman98ca4f22009-08-05 01:29:28 +00006160 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006161 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006162 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
6163 i < NumFixedArgs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006164 if (NumParts > 1 && j == 0)
6165 MyFlags.Flags.setSplit();
6166 else if (j != 0)
6167 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006168
Dan Gohman98ca4f22009-08-05 01:29:28 +00006169 Outs.push_back(MyFlags);
Dan Gohmanc9403652010-07-07 15:54:55 +00006170 OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006171 }
6172 }
6173 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006174
Dan Gohman98ca4f22009-08-05 01:29:28 +00006175 // Handle the incoming return values from the call.
6176 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006177 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006178 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006179 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006180 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006181 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6182 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006183 for (unsigned i = 0; i != NumRegs; ++i) {
6184 ISD::InputArg MyFlags;
Duncan Sands1440e8b2010-11-03 11:35:31 +00006185 MyFlags.VT = RegisterVT.getSimpleVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006186 MyFlags.Used = isReturnValueUsed;
6187 if (RetSExt)
6188 MyFlags.Flags.setSExt();
6189 if (RetZExt)
6190 MyFlags.Flags.setZExt();
6191 if (isInreg)
6192 MyFlags.Flags.setInReg();
6193 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006194 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006195 }
6196
Dan Gohman98ca4f22009-08-05 01:29:28 +00006197 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00006198 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +00006199 Outs, OutVals, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006200
6201 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006202 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006203 "LowerCall didn't return a valid chain!");
6204 assert((!isTailCall || InVals.empty()) &&
6205 "LowerCall emitted a return value for a tail call!");
6206 assert((isTailCall || InVals.size() == Ins.size()) &&
6207 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006208
6209 // For a tail call, the return value is merely live-out and there aren't
6210 // any nodes in the DAG representing it. Return a special value to
6211 // indicate that a tail call has been emitted and no more Instructions
6212 // should be processed in the current block.
6213 if (isTailCall) {
6214 DAG.setRoot(Chain);
6215 return std::make_pair(SDValue(), SDValue());
6216 }
6217
Evan Chengaf1871f2010-03-11 19:38:18 +00006218 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6219 assert(InVals[i].getNode() &&
6220 "LowerCall emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00006221 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006222 "LowerCall emitted a value with the wrong type!");
6223 });
6224
Dan Gohman98ca4f22009-08-05 01:29:28 +00006225 // Collect the legal value parts into potentially illegal values
6226 // that correspond to the original function's return values.
6227 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6228 if (RetSExt)
6229 AssertOp = ISD::AssertSext;
6230 else if (RetZExt)
6231 AssertOp = ISD::AssertZext;
6232 SmallVector<SDValue, 4> ReturnValues;
6233 unsigned CurReg = 0;
6234 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006235 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006236 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6237 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006238
Bill Wendling46ada192010-03-02 01:55:18 +00006239 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00006240 NumRegs, RegisterVT, VT,
6241 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006242 CurReg += NumRegs;
6243 }
6244
6245 // For a function returning void, there is no return value. We can't create
6246 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006247 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006248 if (ReturnValues.empty())
6249 return std::make_pair(SDValue(), Chain);
6250
6251 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6252 DAG.getVTList(&RetTys[0], RetTys.size()),
6253 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006254 return std::make_pair(Res, Chain);
6255}
6256
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006257void TargetLowering::LowerOperationWrapper(SDNode *N,
6258 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006259 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006260 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006261 if (Res.getNode())
6262 Results.push_back(Res);
6263}
6264
Dan Gohmand858e902010-04-17 15:26:15 +00006265SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006266 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006267 return SDValue();
6268}
6269
Dan Gohman46510a72010-04-15 01:51:59 +00006270void
6271SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006272 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006273 assert((Op.getOpcode() != ISD::CopyFromReg ||
6274 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6275 "Copy from a reg to the same reg!");
6276 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6277
Owen Anderson23b9b192009-08-12 00:36:31 +00006278 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006279 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00006280 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006281 PendingExports.push_back(Chain);
6282}
6283
6284#include "llvm/CodeGen/SelectionDAGISel.h"
6285
Eli Friedman23d32432011-05-05 16:53:34 +00006286/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6287/// entry block, return true. This includes arguments used by switches, since
6288/// the switch may expand into multiple basic blocks.
6289static bool isOnlyUsedInEntryBlock(const Argument *A) {
6290 // With FastISel active, we may be splitting blocks, so force creation
6291 // of virtual registers for all non-dead arguments.
6292 if (EnableFastISel)
6293 return A->use_empty();
6294
6295 const BasicBlock *Entry = A->getParent()->begin();
6296 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6297 UI != E; ++UI) {
6298 const User *U = *UI;
6299 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6300 return false; // Use not in entry block.
6301 }
6302 return true;
6303}
6304
Dan Gohman46510a72010-04-15 01:51:59 +00006305void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006306 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00006307 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006308 SelectionDAG &DAG = SDB->DAG;
Dan Gohman2048b852009-11-23 18:04:58 +00006309 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006310 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006311 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006312
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006313 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00006314 SmallVector<ISD::OutputArg, 4> Outs;
6315 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
6316 Outs, TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006317
Dan Gohman7451d3e2010-05-29 17:03:36 +00006318 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006319 // Put in an sret pointer parameter before all the other parameters.
6320 SmallVector<EVT, 1> ValueVTs;
6321 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6322
6323 // NOTE: Assuming that a pointer will never break down to more than one VT
6324 // or one register.
6325 ISD::ArgFlagsTy Flags;
6326 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00006327 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006328 ISD::InputArg RetArg(Flags, RegisterVT, true);
6329 Ins.push_back(RetArg);
6330 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006331
Dan Gohman98ca4f22009-08-05 01:29:28 +00006332 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006333 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006334 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006335 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006336 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006337 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6338 bool isArgValueUsed = !I->use_empty();
6339 for (unsigned Value = 0, NumValues = ValueVTs.size();
6340 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006341 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006342 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006343 ISD::ArgFlagsTy Flags;
6344 unsigned OriginalAlignment =
6345 TD->getABITypeAlignment(ArgTy);
6346
6347 if (F.paramHasAttr(Idx, Attribute::ZExt))
6348 Flags.setZExt();
6349 if (F.paramHasAttr(Idx, Attribute::SExt))
6350 Flags.setSExt();
6351 if (F.paramHasAttr(Idx, Attribute::InReg))
6352 Flags.setInReg();
6353 if (F.paramHasAttr(Idx, Attribute::StructRet))
6354 Flags.setSRet();
6355 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6356 Flags.setByVal();
6357 const PointerType *Ty = cast<PointerType>(I->getType());
6358 const Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006359 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006360 // For ByVal, alignment should be passed from FE. BE will guess if
6361 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006362 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006363 if (F.getParamAlignment(Idx))
6364 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00006365 else
6366 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006367 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006368 }
6369 if (F.paramHasAttr(Idx, Attribute::Nest))
6370 Flags.setNest();
6371 Flags.setOrigAlign(OriginalAlignment);
6372
Owen Anderson23b9b192009-08-12 00:36:31 +00006373 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6374 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006375 for (unsigned i = 0; i != NumRegs; ++i) {
6376 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6377 if (NumRegs > 1 && i == 0)
6378 MyFlags.Flags.setSplit();
6379 // if it isn't first piece, alignment must be 1
6380 else if (i > 0)
6381 MyFlags.Flags.setOrigAlign(1);
6382 Ins.push_back(MyFlags);
6383 }
6384 }
6385 }
6386
6387 // Call the target to set up the argument values.
6388 SmallVector<SDValue, 8> InVals;
6389 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6390 F.isVarArg(), Ins,
6391 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006392
6393 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006394 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006395 "LowerFormalArguments didn't return a valid chain!");
6396 assert(InVals.size() == Ins.size() &&
6397 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006398 DEBUG({
6399 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6400 assert(InVals[i].getNode() &&
6401 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00006402 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00006403 "LowerFormalArguments emitted a value with the wrong type!");
6404 }
6405 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006406
Dan Gohman5e866062009-08-06 15:37:27 +00006407 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006408 DAG.setRoot(NewRoot);
6409
6410 // Set up the argument values.
6411 unsigned i = 0;
6412 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006413 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006414 // Create a virtual register for the sret pointer, and put in a copy
6415 // from the sret argument into it.
6416 SmallVector<EVT, 1> ValueVTs;
6417 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6418 EVT VT = ValueVTs[0];
6419 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6420 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006421 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006422 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006423
Dan Gohman2048b852009-11-23 18:04:58 +00006424 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006425 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6426 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006427 FuncInfo->DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006428 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6429 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006430 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006431
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006432 // i indexes lowered arguments. Bump it past the hidden sret argument.
6433 // Idx indexes LLVM arguments. Don't touch it.
6434 ++i;
6435 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006436
Dan Gohman46510a72010-04-15 01:51:59 +00006437 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006438 ++I, ++Idx) {
6439 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006440 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006441 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006442 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006443
6444 // If this argument is unused then remember its value. It is used to generate
6445 // debugging information.
6446 if (I->use_empty() && NumValues)
6447 SDB->setUnusedArgValue(I, InVals[i]);
6448
Eli Friedman23d32432011-05-05 16:53:34 +00006449 for (unsigned Val = 0; Val != NumValues; ++Val) {
6450 EVT VT = ValueVTs[Val];
Owen Anderson23b9b192009-08-12 00:36:31 +00006451 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6452 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006453
6454 if (!I->use_empty()) {
6455 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6456 if (F.paramHasAttr(Idx, Attribute::SExt))
6457 AssertOp = ISD::AssertSext;
6458 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6459 AssertOp = ISD::AssertZext;
6460
Bill Wendling46ada192010-03-02 01:55:18 +00006461 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006462 NumParts, PartVT, VT,
6463 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006464 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006465
Dan Gohman98ca4f22009-08-05 01:29:28 +00006466 i += NumParts;
6467 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006468
Eli Friedman23d32432011-05-05 16:53:34 +00006469 // We don't need to do anything else for unused arguments.
6470 if (ArgValues.empty())
6471 continue;
6472
Devang Patel0b48ead2010-08-31 22:22:42 +00006473 // Note down frame index for byval arguments.
Eli Friedman23d32432011-05-05 16:53:34 +00006474 if (I->hasByValAttr())
Michael J. Spencere70c5262010-10-16 08:25:21 +00006475 if (FrameIndexSDNode *FI =
Devang Patel0b48ead2010-08-31 22:22:42 +00006476 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
6477 FuncInfo->setByValArgumentFrameIndex(I, FI->getIndex());
6478
Eli Friedman23d32432011-05-05 16:53:34 +00006479 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6480 SDB->getCurDebugLoc());
6481 SDB->setValue(I, Res);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006482
Eli Friedman23d32432011-05-05 16:53:34 +00006483 // If this argument is live outside of the entry block, insert a copy from
6484 // wherever we got it to the vreg that other BB's will reference it as.
Eli Friedman7f33d672011-05-10 21:50:58 +00006485 if (!EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00006486 // If we can, though, try to skip creating an unnecessary vreg.
6487 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00006488 // general. It's also subtly incompatible with the hacks FastISel
6489 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00006490 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
6491 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
6492 FuncInfo->ValueMap[I] = Reg;
6493 continue;
6494 }
6495 }
6496 if (!isOnlyUsedInEntryBlock(I)) {
6497 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00006498 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006499 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006500 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006501
Dan Gohman98ca4f22009-08-05 01:29:28 +00006502 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006503
6504 // Finally, if the target has anything special to do, allow it to do so.
6505 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006506 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006507}
6508
6509/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6510/// ensure constants are generated when needed. Remember the virtual registers
6511/// that need to be added to the Machine PHI nodes as input. We cannot just
6512/// directly add them, because expansion might result in multiple MBB's for one
6513/// BB. As such, the start of the BB might correspond to a different MBB than
6514/// the end.
6515///
6516void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006517SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006518 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006519
6520 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6521
6522 // Check successor nodes' PHI nodes that expect a constant to be available
6523 // from this block.
6524 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006525 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006526 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006527 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006528
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006529 // If this terminator has multiple identical successors (common for
6530 // switches), only handle each succ once.
6531 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006532
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006533 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006534
6535 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6536 // nodes and Machine PHI nodes, but the incoming operands have not been
6537 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006538 for (BasicBlock::const_iterator I = SuccBB->begin();
6539 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006540 // Ignore dead phi's.
6541 if (PN->use_empty()) continue;
6542
Rafael Espindola3fa82832011-05-13 15:18:06 +00006543 // Skip empty types
6544 if (PN->getType()->isEmptyTy())
6545 continue;
6546
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006547 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006548 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006549
Dan Gohman46510a72010-04-15 01:51:59 +00006550 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006551 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006552 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00006553 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006554 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006555 }
6556 Reg = RegOut;
6557 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00006558 DenseMap<const Value *, unsigned>::iterator I =
6559 FuncInfo.ValueMap.find(PHIOp);
6560 if (I != FuncInfo.ValueMap.end())
6561 Reg = I->second;
6562 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006563 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006564 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006565 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00006566 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006567 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006568 }
6569 }
6570
6571 // Remember that this register needs to added to the machine PHI node as
6572 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006573 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006574 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6575 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006576 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006577 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006578 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006579 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006580 Reg += NumRegisters;
6581 }
6582 }
6583 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006584 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006585}